Hello everyone and welcome to the plotline cufflinks lecture where we actually show how we can program
using these libraries to create interactive data visualizations.
Let's go ahead and jump straight to Jupiter notebook to get started.
All right.
Here I am at the Jupiter notebook.
I'm going to go ahead and do a couple of import and then some setup in order for this all to work.
When I say import is PD important I'm PI and P and then I'm going to go ahead and say from plot the
imports underscore underscore version underscore underscore and then I'm going to go ahead and say print
version.
And notice here I get one point one to point nine.
That's the current version I'm using.
Everything we're going to be doing requires a version that's newer than one point nine.
So here I'm already on one point twelve.
That means I'm good.
If you have a version that's one point eight or below you'll need to upgrade plotless.
If you just install plotline you should be totally fine on this.
Next I'm going to go in and say import cufflinks as C-f And then finally I'm going to import some libraries
from plot lead off line.
So partly as a company can support your online and can host your online data visualizations.
But since we're going to be using this as an open source library and we're going to you're using it
off line we'll need to do some imports from the offline in order for that to work.
I want to go ahead and import download plotless underscore plotless J.S. and you can just type D and
then tab and it should autocomplete for you.
I'm also going to say in it I'm just doing tab's autocomplete in underscore notebook underscore mode
.
And then also plot and plots run those.
And then we're going to do a couple of more commands in order to have everything work in the notebook
.
Just call it underscore a notebook underscore mode and passen connected equals true.
And this is going to connect the javascript to your notebook because plotless essentially just connects
pandas and Python to it interactive javascript library.
And this is going to allow your notebooks to access those visualizations.
And then finally we're going to say C-f go off line as a method and close print seized and run this
and this will allow CS cufflinks off line.
All right.
Quite a few steps here to make sure everything works out.
But hopefully you're able to follow those.
Let's go ahead and now get some data and create it.
I'm going to go ahead and say ZF is equal to PD dot data frame.
I'm going to say N.P. that random
Rand and so normal distribution and just passen 100 by 4.
So 100 rows for columns and I'm going to go in and say columns is equal to a b c d as a string split
.
And that way we get a list of the columns ABC and D.
And then if I check DF just ahead of it here I have my data frame and I'm going to go ahead and create
one more data frame called DL 2 PD data frame and I'll use a dictionary for this.
I'm going to say category is a column and then the values are just going to be a b and c or any three
strings you want and then the other column is going to be values.
And this just going to be a string of some random values.
Let's go ahead and just say 32 43 and 50
and this is going to be used later for like a bar plot style.
All right.
Now just go over how to use cufflinks and I plot.
It's actually extremely straightforward.
If we take a look at our data frame we have ABC and Z here.
If I just call the plot off of this data frame panderers will plot something with MAP plot live and
if actually I want to see that plot I can say map plot live in line and then if I run this again let
me just cut this and put it up top here.
Whoops there we go.
And I'm going to come back down here and run this plot one more time.
And here I see the line plot that Pandurs would have made automatically using matte plotless.
I can just change this too I plot and we're going to see almost a little bit of magic happen.
That plot has now been converted to a plotline interactive image.
And here I can kind of scroll along and actually tell me the values at that particular index point.
So it's the exact same plot line plot.
Now it's interactive.
I can zoom in.
Check out values.
I can double click to zoom back out and they also have tools here.
I can save and edit the plot.
I can download the plot as a PNB file so if I just want a static image I can pan.
So if I wanted to I could pan around I could zoom in zoom out reset the axes choose the hover so closest
on hover or compare data on hover.
By default you probably usually want to compare data and then I can even click on and off certain columns
to view.
So if I just want to view and see if it can be a and.
Right.
Hopefully you can already tell this is an extremely powerful tool because basically you have to add
one letter plot and now suddenly you look like an amazing interactive data visualisation programmer
.
When your secret was just adding I plot and using cufflinks and politely.
All right.
So that's the most basic example of cufflinks and I plop.
And let's go ahead and explore now the various types of plots we can do.
We can do many types like scatter bar box ratio heat maps and more.
So all we're going to do is just go through them and show you how to use plot to create other plot types
that are not just line plots like this one.
Let's start off by showing you how to do a scatterplot.
I'm going to go ahead and say DMF dot plot.
And then in order to switch the type of plot you specify kind equals and then for scatter the string
is scatter and scatterplot to specify the x and y axes.
You can go in and specify this by the column name.
Let's go in and show a scatter of a column versus B column and if we just run this you'll see something
funny pops up.
You get something like this.
And by default plotless is going to try to make a line to connect all these points.
So you have to do specify mode and passing the string markers and then you'll actually get some markers
here and you can check out the markers and then you can actually compare the A and B columns.
You can zoom in and check on information on each of these points.
All right.
So that's how you can use a scatterplot using DFA A-plot person x and y arguments as the column names
specify kind is equal to scatter and then pass and the mode equals markers.
You can also do other things such as passing arguments for size.
You want these points to be bigger.
You can go ahead and specify larger size and I'll go ahead and make those points larger.
Let's move on to a bar plots or bar plots and say.
Thought I plot and say kind passing the string bar.
And then you specify X and we're going to go out and switch to decaf too since that's a better fit for
this plot.
So as a reminder you have to have a categorical column and a numerical column meaning for a bar plot
you want the X to be the categorical column to make this obvious.
I'm just going to say x is equal to the category column.
And then why is going to be to the values column.
Now if we go and check that out.
Now we have a bar plot where if I zoom out a little bit I can see the value for each of these categories
so C is 50 B is 43 and was 32.
All right.
And you can zoom in if you want but that may not be too helpful for a particular marplot.
Go ahead and continue on.
Now as we know your data is not always going to be conveniently placed such as the 2 if a category in
values column but which you can do is called group by an aggregate function on your data to actually
get it into some sort of form where it makes sense to use a bar plot using A-plot.
Let me go to show you what I mean by that.
Take a look at our original DMF.
We can see that we have a B C and D.
Now we wouldn't be able to call bar plot off of this.
Otherwise we'll just get a bunch of bars and I'll show you what I mean by that.
Just that kind is equal bar you'll get a bunch of bars for every single data point and that's not exactly
what you want.
You would need to do some sort of aggregate function on top of this for instance let's see you want
to count the instances for each column.
And here we have a bar plot for each instance which in this case is just 100.
Since they're all evenly distributed but you can do other things such as take the sum of each column
and now we have a bar plot showing the sum of each column.
All right.
Pretty interesting stuff.
But keep in mind that this bar plot with plot becomes really powerful when you call some sort of aggregate
function or a group by a function of your data frame.
More likely your data frame is not going to be in this kind of nice formatting.
OK that's it marplot.
Let's discuss.
Box plots now Box plots your data can be something that looks like this box plots will automatically
do that sort of aggregation for that distribution.
And you just do this by specifying kind is equal to box.
And here it will automatically make a box plot for each of the columns and you can turn columns off
or on either bypassing them.
Here with DMF or just by switching them on and off here of plot plotline for instance I only wanted
to see and see I can just click on a and c and they can click on Biondi as well and see what I want
and Ill give you information on the core tiles here for the box which is quite convenient.
Now let's talk about a 3D surface plot in order to really talk about the three of the surface plot.
I'm going to make a new data frame will say if three is equal to PD data frame and we'll go ahead and
say X is a column and we'll just say one two three four five we'll specify y as a column
devalues 10 20 30 2010 so it goes up and down and then I will specify Z is a column.
And let's go ahead and specify this to be 500 400 300 200 100.
OK.
So I have my data frames take a look at it and this is basically three mentions of values are three
variables we can use a surface plot.
If we wanted to create a three dimensional surface plot of these three variables in say three plot and
specify the kind to be a surface plot and this will create a surface plot of what we have here.
So you can see we have a surface plot.
Now let me show you another example of a surface plot by kind of changing in these values around instead
of saying 100 through 500 I'm going to say 5 4 3 2 1.
And let's plot this.
We can see now we have kind of this peak outline.
Now we can actually change the color scales here in case we don't like this rainbow colors scale.
You can say something like color scale and say Ardie for red or yellow for white y l for yellow and
B for blue.
And you can check the plotless documentation for all the color scale codes.
They don't exactly match up with the map plot color maps.
So keep that in mind.
And here we have a really nice surface plot directly made off our data frame.
We can rotate it and play around with it in order to get all the information out of it.
All right.
Those are three the surface.
And finally going to talk about a couple of more plots.
A very basic one that you'll probably find yourself doing as a histogram you can say if call a single
column and then say I plot's kind of equal to hist and you will get a histogram and you can actually
specify the number of bins of another argument.
For example if you want 25 bins you can specify that and get more bins.
Let me go and make this more obvious by specifying a lot of bids.
OK.
And here we have more bins.
There weren't that many data points so keep them in mind if you go ahead and passen the entire data
frame.
Let's just call DFA A-plot kind equals Just you will get a overlapping histogram of all the columns
meaning you can compare the histograms of columns and you can turn them on and off to compare them to
each other.
Now these are randomly normally distributed.
So they're kind of overlapping each other which makes sense given the way we created this.
But you can turn them often on to view histograms as you please for all the columns in the data frame
.
So it's up to you whether you want to just pass in one column or pass the whole data frame and then
switch them on and off.
Next one I want to show you is a spread spread type visualisations are used a lot for stock data.
So compared to stocks I'm going to go ahead and just do them with our random values here.
Later on in the financial day to cap some project we'll actually see some more technical analysis Blotz
.
But if I just say these two columns and be off my data frame I can say A-plot kind is equal to spread
and we'll get a plot and then a subplot I'll get my line plot which shows them against each other and
then will get my spread plop which shows the spread of them against each other.
So for instance if we go ahead and zoom in here on the 20 we can zoom in here.
Let's see where it corresponds to 20.
Well notice the spread is negative point 3:08 between them.
And here we can verify that as a result.
So negative point 3 8 minus point 1.
We'll take a 2.5 6 about there.
So you can go ahead and compare these results and this is essentially really useful for stock data to
see the spread between two stocks.
We have two more plush types to show you the next one is a bubble plot and a bubble plot is very similar
to a scatterplot except that it will change size of the points based off of another variable.
So if I say kind is equal to bubble I'll specify X and Y just like that for a scatterplot and then I
will specify the size of the bubbles based off another column.
Let's go ahead and just choose.
See and here now I have a scatterplot except the size of the points is based off in other column value
and you see these kind of plots a lot for things like world GDP in comparison to population and maybe
happiness factor etc..
So these are quite common plots especially for things like United Nations reports.
Right.
So that's the bubble plot.
And then finally we'll get to the scatter matrix the matrix plot is essentially very similar to seaborne
pair plot.
It just creates a scatter matrix of all the columns it can.
So you say DFA that scatter underscore matrix you'll mean you may need to make sure all your columns
are numerical in order for this to work.
Let's just say scatter matrix and you can kind of zoom in on each plot individually.
Keep in mind you have a lot of points here.
This will take a while to load and may be very slow as far as zooming in and out.
So use this of care on very large datasets.
Otherwise you may crash your Python kernel.
OK that's it for the basic plot types that we're going to go over in this lecture.
If you want more information and some more details about the documentation I would recommend actually
going to the web page and that's available just by searching hub and cufflinks should take you to this
page it's hosted by the Cesar Santos Jorge slash cufflinks and I've contributed to it as you've seen
before and you can check out right here these cufflinks tutorial links where you have a chart gallery
.
Let you know what colors are available online Pandurs like completely.
There's a lot of really good information here.
So for instance if you go ahead and say pandas like we've already gone over quite a bit of this information
there's actually some more basic plotting methods such as bar plot's histograms maybe stacked histograms
some more detail grouping values on box plot's area plots so she didn't go over.
But it's basically just saying kind is equal to area and there's some more information.
Now we've actually done a lot of this already but if you want some more details you can always reference
this documentation page which is pretty nice.
Okay.
Lastly I just wanted to mention that there is some technical analysis capabilities with cufflinks that
are built in.
They're still under beta.
They're not fully released but you can check them out in the technical analysis section of this library
you can find out more about these technical analysis plot's by clicking here on cufflinks folder and
then checking out CA pi.
And this is the technical analysis that pi file.
Again this is kind of advanced stuff and it specifically is dedicated to financial analysis.
So if you're not interested in financial analysis at all don't worry about this.
They can check a couple of the functions here and a couple of the types you can do.
It allows you to do things such as moving averages or correlation between plots or stocks etc..
We'll explore some of these plot types later on in an optional section of the financial data capstone
project.
But if you're not interested in this sort of stuff don't worry about it.
I just want you to be aware that it's there for you in case you are interested in financial analysis
and creating interactive plots for that.
Okay.
Thanks everyone.
I hope you enjoyed learning about plotline cufflinks.
It's a really powerful tool.
And the good news is you don't have to adjust your knowledge that much for it.
You essentially just add an eye and remember what kind of plot you want to pass.
OK.
Thanks everyone and I'll see at the next lecture.