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