Python Data Visualization Transcripts
Chapter: Matplotlib
Lecture: Object Oriented API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I wanted to highlight a couple of changes I made to the notebook just to indicate
0:06
the difference between the two interfaces that we've been talking about. So I have updated the notebook out of the field,
0:12
say this is the state based interface that we talked about. And then down here is the object oriented interface,
0:19
which is what we recommend. And you can always do a kernel restart and run off, get us back to the same spot.
0:28
Now I'll show how to actually use the object oriented API. So now we have the same histogram that we did before, but instead of doing plot.hist,
0:46
we did ax.hist. And on the surface it looks like we didn't really accomplish a whole lot,
0:52
but by creating the figure and the axes we have a lot more control over it and it's a lot more consistent pythonic,
0:59
API and we'll walk through some more examples of that. The other thing I wanted to talk about remember we did matplotlib inline
1:08
appear so that the figures would automatically display. There is another approach I wanted to call out called matplotlib notebook.
1:20
This is going to give a more interactive example and I'm gonna walk through and show it so that you're aware of it.
1:26
I personally don't use it very often, but I think it is helpful to see.
1:29
So I've enabled this notebook interface and I'm gonna do a little more complex example where we will let's copy and paste that.
1:38
So we don't have to re type everything. So we'll create that histogram. But now we want to set the X Label, the Y Label and the title.
1:46
Then we'll show the figure. Let me code that for you.
2:03
So now what we've done is we've established that axis put the histogram based on the combined 08 column that we've been using set the X label and the Y
2:15
label and title on that axis. And then showing this interactive figure that you can move and adjust and different plot types
2:26
is maybe a little more useful than others. And then when you're done interacting with it, you can turn it off. Like I said,
2:35
I don't tend to actually use this format very often I'm going to convert back to using matplot lib in line.
2:46
I'm also going to comment this out because sometimes it gets a little confused when you make multiple changes in the same notebook,
2:56
we start and run it all again. So when I ran it all again, I got this warning here because I have disabled matplot lib notebook.
3:05
The figs show it doesn't like that so I can rerun it without that, it will automatically display and everything's okay.
3:13
So just wanted to kind of walk through that a little bit more.
3:17
So let's give another example of using the object oriented interface where we have a different approach to setting the X Label,
3:26
the Y label and the title. So it's going to start the same way. So we defined our figure to find the axis object,
3:35
put the histogram on that axis and then instead of setting the doing three separate lines, we can use ax.set and pass X label,
3:44
Y label and title as parameters to it.
4:06
And now we have the same histogram with x label, Y label, and title Set. But we have used a slightly different API.
4:13
To do this and one pointed out because you'll see examples of both and it's a little bit of personal preference.
4:19
But I do think using Set is a little more easy to understand and grasp as you're getting started with that matplot lib and just a reminder.
4:29
If you want to get rid of the extra text. We had that semi colon. Rerun it and we have our plot So I'm going to restart and run all again.