Hello everyone and welcome to the Panthers state of visualization exercise solutions lecture this lecture
.
We're going to go over the solutions for the pand the state of his life.
Exercise questions.
Let's go ahead and jump to Jupiter notebook to get started.
All right here I am at the notebook.
I'm going to go ahead and run the first few cells of code to read the D3 CACP file set up all the imports
correctly and check out the head.
All right.
Next thing I wanted to do was to recreate a scatterplot of B versus a.
So just those two columns.
Let's go ahead and do that by saying D-3 dot plot thoughts scatter and then I'm going to go in and specify
that I want on my x axis the A column and on my y axis I want the B column.
Let's just see what that looks like.
And this should be the EF 3.
There we go.
So I'm getting a similar plot except something to know was.
See if you can figure out how to stretch it in a similar fashion to this plot.
And as we went over in the lecture you can actually just passen fig size directly into this argument
as a parameter.
And we can stretch it out.
So and then another thing to note here is that the size of these points is a little bigger.
So you can actually specify that as we saw in the lecture either by another column or just by passing
in S for size.
And I'm going to specify the size to be 50.
Here we have the points are a little larger and for the color it was c we saw in the lecture how we
can specify color by another column.
But you can also just pass on a string as you could for map plot lib and here now we have a recreation
of that plot.
Exactly.
OK.
The main thing I wanted you to get out of this particular question was just the step of plot that scatter
a Versus be so x and y.
Don't worry too much.
If you were able to get the sizing or the coloring size Hopefully you are able to get that.
OK moving on now I want to create a histogram of the A column.
This is actually quite simple.
Just call the column itself and then say either just to see the histogram or you can also say plot dot
hissed and all return the same histogram.
And this is the default number of bins.
But you can also pass in an argument if you want it more Beenz.
All right the next issue was that these plots are OK but they don't look very polished and the instructions
said use style sheets to set the style to G-G plot and redo the histogram from above and also figure
out how to add more bits to it.
This is a topic that we actually didn't cover in the video lecture but it is covered in the note book
so hopefully had either two paths one you recheck the notebook to figure out what style sheets was referring
to or you google search map Cutlip stylesheets.
If not you could have also posted to the Q and A forum to get to what I was referencing there.
If you go ahead and check out the note book lecture for pandas built in data visualization there is
a stylesheet section and this is kind of just to get you in the mood of trying to discover aspects on
your own.
Here you can see that map Cutlip has a stylesheets and there's a little reference link there.
And if you google search this you probably would have also gone to this page.
And this is the gallery and there's a sheet section in the gallery and the basic premise or idea is
that you can use Piel t that style.
Use pass any string of a style and it will automatically change the aesthetics to a particular style
that looks like this.
You just say peel T-Dog style that use G-G plot.
So I'm going to just copy this line of code.
Come over here and paste it.
And now since this has been run you'll notice that if we go ahead and copy and paste the exact line
of code here and pasted in here we get a much better looking plot.
And you'll also notice that here this red is a little more see through.
So you can also pasand Alpha to make it a little more transparent.
And here you can begin to start matching these histograms together.
Hopefully that wasn't too challenging for you but there is that aspect of kind of discovering things
on your own which is probably the most important part of learning data science and learning programming
.
The ability to search for an answer on your own without being given the full context of information
however you can always feel free to post on the Q&A forums if anything's ever confusing to you.
All right let's go ahead and move on to the box plots we wanted to create a box plot comparing the A
and B columns.
We take a look at what's the on that we take a look at just the data frame itself.
Notice we have a b c and d columns so I don't want to get a box plot all the columns I just want a B
.
So I'm actually just going to grab those columns by passing in a list of those column names A and B
and then I can call plot box.
Create a box plot that looks the same as this one.
OK.
Next I want to create a k d plot of the D column.
That's actually pretty straightforward.
You just pass in the D column say plot and then say Katie and they'll return this plot.
And then finally we have a we're here to figure out how to increase the line with and make the line
styled dash and no you would usually not Daschle K the plotline.
All right.
So the way you can do this is actually just take this same plot KDE or density and then you can go ahead
and passen arguments from that plot lib such as line style and line with.
And here we get the same plot.
And you can also do this with density if you want to.
That's the same command that we want to create an area plot of all the columns for just the rooms up
to 30 and then it said Hintz use x.
So hopefully you can discover by calling X D-3 I X you can see that this is a property and it's a primarily
a label location based in indexer with integer position fallback.
And so what we can do is actually just use this for slicing.
And again it's kind of a thing I want you to discover as you go along on your own so you can pass zero
through 30 here and this will return just the first 30 rows or whatever is slicing notation you wanted
.
And then off of that you can call plot area and you get a plot that looks like this.
And if you wanted it to Looks like the plot below you could actually put in an alpha value there.
All right there is a bonus challenge and note you may find this really hard reference's solutions you
can't figure it out but notice how appear the legend was actually overlapping some of the actual diagram
.
They usually wouldn't want that and I want you to figure out how you can display the legend outside
of the plot as shown below over here and again back on that theme of trying to discover things on your
own.
I want to for you to try google searching for a good stack overflow link and if you could find that
on your own there was a link to a stack overflow question.
So if we take a look at this link on stack overflow someone's asking how to put the legend outside the
plot with pandas and if we kind of scroll over here and take a look at this you'll notice that someone
actually answer this question correctly and they said right here that Piel t that legend location center
left and then this beby box to anchor.
That's the key here.
And you actually just copy this code and paste it here and then take the other code of this area plot
and paste it here and then when we want to do is create a figure
for these to be all called and then will say puberty that show.
And this will create a figure with the legend outside.
This would also work if you actually remove the first line and the last line in Jupiter notebook.
In other situations such as other ID errors you may have to actually find the figure and show the figure
.
But since we're just using Jupiter a notebook can actually just run these two lines and it will remove
the legend and place it on the outside.
So the basic idea here is hopefully you can discover on your own how to grab these sort of lines of
code and put them into your own code.
And that's actually a very common thing to do is check on Stack Overflow see what lines of code are
relevant to you and then fit them into your own work.
OK that's it.
Great job everyone and I will see you at the next lecture.
Feel free to post the Q&A forums if anything was confusing here for you.