Pyplot and PyLab
Before you can start coding your graphics, you need to know a bit about the theory behind matplotlib
:
- Pyplot and PyLab
- Object hierarchy
- Stateful vs stateless approaches
The understand Pyplot and PyLab, you need to look at the history of matplotlib
, which was originally developed by a neurobiologist named John D. Hunter in the early 2000s. It was inspired by MATLAB and is now a community effort.
00:00 Before we can start coding our graphics, we need to talk about some of the theory behind Matplotlib. This includes pyplot and PyLab, object hierarchy, and stateful versus stateless approaches.
00:16 This video will cover pyplot and PyLab. To help understand that, we first need to look at the history of Matplotlib. Matplotlib was originally developed by a neurobiologist named John D. Hunter in the early 2000s.
00:35 His original inspiration for the project came from the popular MATLAB software. He wanted to emulate MATLAB’s plotting commands, but within Python, effectively combining the power of both Python and MATLAB.
00:51 Unfortunately, John passed away in 2012. But now Matplotlib is a full-fledged community effort developed and maintained by a host of talented individuals.
01:03 One area where Matplotlib draws inspiration from MATLAB is in its global import style. The Python concept of importing is not heavily used in MATLAB, meaning that most of MATLAB’s functions are readily available to the user at the top level.
01:22
These functions can be called on their own, instead of on some object or a module alias. These functions all exist inside of a submodule of matplotlib
called pyplot
.
01:37
pyplot
contains a collection of command style functions that mimic the behavior of MATLAB. pyplot
is the high-level interface we can use to perform common actions on our plot. And, as we’ll learn soon, it does a lot of work for us behind the scenes. pylab
, on the other hand, is a module within matplotlib
that exists only to bring a number of functions and classes from both numpy
and pyplot
into the global namespace, making it easy for MATLAB users who were not used to imports to use Matplotlib.
02:20
This means that all they needed was a good old from pylab import *
at the top of their script, and they could use all the functions as if they were defined right there in their script at the global level, just as they would in MATLAB.
02:36 Well, we Pythonistas know that this is a bad practice, especially when the library contains tons of functions. Having these functions accessible in a global namespace caused conflicts with Python’s own built-in global functions and it made it really hard to track down certain bugs.
02:57
The bottom line is Matplotlib has abandoned this convenience model and now explicitly recommends against using pylab
, bringing things more in line with one of Python’s key notions: explicit is always better than implicit.
03:15
pylab
can still be useful when creating simple graphics within an interactive shell like IPython, but we shouldn’t use it in our scripts. So, then what about pyplot
? Well, we shouldn’t rely on that alone either.
03:32
This is because it masks a lot of the inner workings of matplotlib
, inner workings that we want to have access to. In the next video, we’ll learn what some of these inner workings are, and then we’ll see why relying on pyplot
alone is usually a bad idea.
Become a Member to join the conversation.