Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The chart... the myth... the Legend. Without adding legends and labels to a chart, the data can appear mythical. Let's see how easy it is to add these in matplotlib.
-
0:00
When we left off, we had a couple of lines plotted on our chart, but
-
0:03
no reference as to what either line was.
-
0:06
This is where legends can come in handy.
-
0:09
Fortunately, matplotlib makes this easy to implement as well.
-
0:14
We add labels to our data series and
-
0:16
call the legend method on the plot object before we show it.
-
0:23
So our label, we'll just call this dash-dot,
-
0:31
And dashed, and then call a legend method and run our cell.
-
0:41
And there we have a nice legend for our chart.
-
0:44
There are several options available for the legend.
-
0:47
We can change the location and
-
0:48
styling on the legend with some attributes in the legend method.
-
0:53
I won't go into all those right now, but
-
0:55
we'll see some of them implemented throughout this course.
-
0:58
I'll put a link in the teacher's notes for more information as well.
-
1:02
One thing I do want to show you here is the idea of subplots.
-
1:06
This allows for multiple plots to be displayed in the same window.
-
1:09
We use the subplot method on our plot object, and pass in the number of rows,
-
1:14
columns and which panel a particular plot is going to reside in.
-
1:18
Let's try this in a new notebook cell.
-
1:29
So we'll create the first panel.
-
1:34
Plot subplot, again, rows, columns.
-
1:37
So two rows, one column, and this is the first panel.
-
1:46
We'll use the same x and y values,
-
1:54
And 16 color, we'll keep as green.
-
2:02
And we'll keep the first panel to be dashdot.
-
2:13
And for the second panel, We want,
-
2:18
again, two rows, one column, and this is the second panel.
-
2:31
Two, three, four and five.
-
2:37
Let's use that same hex value, 2B5B84
-
2:51
And call the show method, and run our cell.
-
2:58
Very nice, there's one thing to consider with this image though and
-
3:02
that's chart scale.
-
3:04
Both images look like they have the same slope,
-
3:07
and hereby represent themselves as being of similar importance.
-
3:12
However, when we look at the numeric values of their axis,
-
3:16
we see they are very different.
-
3:18
Let's update the scale of out two plots here to make their
-
3:21
axis have the same values.
-
3:23
We do that by setting limits on our x and y axis with the set_xlim and
-
3:28
set_ylim methods.
-
3:31
We pass in a list of the starting and ending values for our axes,
-
3:35
this constrains our charts to those dimensions along the x and, or y axis.
-
3:41
Let's see this in action.
-
3:45
Let's make this a variable.
-
3:49
Save some typing,
-
3:54
panel_1 will set xlim 0 to 6.
-
4:08
Ylim 0 and 20.
-
4:12
So again, the x limit will now be between 0 and 6 and the y limit will be 0 to 20.
-
4:21
Let's do this same thing for second panel.
-
4:28
Panel_2,
-
4:38
0 and 6.
-
4:45
0 and 20.
-
4:52
And run our cell.
-
4:57
Now that we've normalized the scale of the plot, we see that the slope of line in
-
5:02
the upper plot is actually much steeper than that of the lower line.
-
5:07
We'll continue to discuss scale throughout the course.
-
5:11
It's an important aspect of data [INAUDIBLE] reporting.
-
5:14
With some matplotlib basics down, I think this is a great place for a break.
-
5:19
Next, we'll discuss some of the chart options available and why and
-
5:23
when we would use each one.
You need to sign up for Treehouse in order to download course files.
Sign up