Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Stateful vs Stateless Approach

One last piece of theory you need to understand is the differences between stateless and stateful interfaces. Pyplot is the stateful interface of matplotlib.

This means that, behind the scenes, Pyplot stores the state of your figure and axes objects, so you don’t have to. Pyplot is great for creating quick plots in an interactive shell.

This code example uses Pyplot to plot a basic line in a plot:

Python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 1, 0.1)
y = np.arange(10)
plt.title("pyplot demo")

00:00 One last piece of theory we need to understand is the difference between stateful and stateless interfaces. pyplot is Matplotlib’s stateful interface. This means that, behind the scenes, pyplot stores the state of our Figure and Axes objects so we don’t have to. Almost all functions from pyplot are implicitly referring to either existing, current Figure and Axes objects, or they’re creating them if they don’t already exist.

00:33 Methods we call in the future are automatically called on the correct objects, and we don’t have to keep referencing the implicit Figure and Axes ourselves.

00:44 This means that pyplot alone is great for creating quick plots within an interactive shell, and we don’t have to worry about naming conflicts like we would with PyLab. To make this more clear, let’s look at a code example.

01:00 This example uses pyplot to plot a basic line in a plot. First, we import pyplot as plt as well as numpy as np. Then, we’re using NumPy’s arange() function to create an ndarray of evenly spaced values.

01:21 This x variable is an ndarray starting from 0 and growing at an interval of 0.1 until it reaches 1.0.

01:31 The y variable is just an ndarray counting from 0 to 10, or really, 0 to 9 inclusive.

01:40 Then we call the pyplot.plot() function, passing in our ndarrays. Behind the scenes, pyplot is creating our Figure, Axes, and all the other objects that belong to the Axes. Deep within that code is a title object that we can set by calling the title() function, passing in a string.

02:04 Finally, in order to actually display the plot on the screen, we call the show() function, which will work with the backend to spawn a new window containing the Figure and everything else.

02:18 We could have typed these commands in one by one into an interactive shell, but things get messy when we need to change specific attributes of specific objects. To do that, we need a stateless, or object-oriented, approach that will allow us to create and modify plot objects manually, rather than letting pyplot do everything for us.

02:44 This is a shortened peek at some of the code behind pyplot. We have two functions, plot() and gca(), short for get current axes.

02:57 The important takeaway here is that the plot() function calls the gca() function, which is responsible for returning the current Axes we’re working with.

03:07 This shows how the plot() function that we call implicitly tracks the plot that we’re working with. It modifies its underlying objects. The problem is, things get messy when we try to create multiple Axes or modify very specific attributes of specific objects deep down in that object hierarchy—for example, changing the color of a very specific tick mark on the x-axis.

03:38 Instead, we need a stateless approach, or the object-oriented approach. This will allow us to get object references for both our Figure and our Axes objects, and then modify those manually using methods we call on the Axes object. In the next video, we’ll see how to do just that. Before I wrap up, let’s do a quick recap of all of the theory we learned about matplotlib.

04:08 matplotlib contains a submodule called pyplot that is an example of a stateful interface. We can call functions from that submodule, and pyplot works behind the scenes to manage our axes for us by tracking the state of the plot and updating it.

04:28 It operates on the object hierarchy, starting with the overarching Figure object and digging deeper into the Axes and individual Axis, axis labels, and much more. pylab combines all of the imports we need for both numpy and pyplot, which makes for a MATLAB-like coding experience.

04:52 We just call a bunch of global functions without having to worry about things like imports or object references.

05:00 We shouldn’t use pyplot alone, and we definitely shouldn’t use pylab. Relying on pylab will bring forth import conflicts and relying on pyplot will make it harder to create complex figures with more than one highly customizable axes. Instead, we’re going to use a stateless, object-oriented approach that will let us customize one or more axes manually. pyplot will be used, but just for some basic functionality. Let’s see how to do that.

Become a Member to join the conversation.