Plotting methods in matplotlib

Different methods

Matplotlib is the go to for visualization in Python. It’s simple and well known. But I have had a long continuing confusion with one part of Matplotlib. That is, sometimes you see one of the three in the below segments of code. What are the differences between these methods? What are they exactly for? Sometimes I’ve been able to create basically the same chart or graph with either of these methods. I want to clear up some of this confusion, for you and (mainly) for myself. I have also included a Jupyter Notebook at the bottom that might be easier to read than this.


import matplotlib.pyplot as plt
plt.plot(x, y)

ax = plt.subplot()
ax.plot(x, y)

fig = plt.figure()
visualization = fig.add_subplot(111)
visualization.plot(x, y)


hierarchical object structure

Matplotlib creates objects which fit within a hierarchy. These objects are figures, axes, and subplots. Axes also contain axises. We can think of a figure as a canvas on which you can paint multiple plots. It is the overall window or page on which everything is drawn. The “plots” on the canvas in turn are called axes. And in turn yet again, these axes are made up of two to three axis. Axes are what we generally think of as a plot; each contains two to three axis objects as well as a title, x label, and y label. A figure can have multiple axes. Subplots add more than one plot in one figure, usually taking three arguments of nrows, ncols and index.


a simple plot

The simplest plot sort of ignores the figure and just calls the plot function. One would use this if you were only plotting one thing.


plt.plot(x, y)
plt.title("Simple Plot")



using a figure

One can do the same thing, but adding a figure to it.

plt.plot(x, y)



using subplots

If you want to have multiple plots, you can add a subplot, specifying where in an invisible box it would go.

plt.plot(x, y)
plt.title("Subplot I")
plt.plot(x, y)
plt.title("Subplot II")



using figures, axes, and subplots

Finally, you can bring them all together.

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10,10))
ax[0,1].set_title("Subplot I")
ax[1,0].set_title("Subplot II")



histograms, scatterplots, and 3d plots

Let’s also show how to do these just as references.


plt.scatter(x, y)

from mpl_toolkits import mplot3dax = plt.axes(projection='3d')



saving figures


plt.savefig("example.png", transparent=True)



style options

there are numerous ways to style your plots. here are some examples of how to do so manually.

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10,10), edgecolor="green")ax[0,1].plot(x,y, color='orange', linestyle=':', linewidth=1)
ax[0,1].grid(color='red', linestyle='-', linewidth=1)
ax[0,1].set_title("Plot")ax[0,0].plot(x,y, color='green', linestyle='--', linewidth=1)
ax[0,0].set_title("Plot")ax[1,0].scatter(x,y, color='magenta', marker='^')
ax[1,0].grid(color='purple', linestyle='-', linewidth=1)
ax[1,0].set_title("Scatter")ax[1,1].scatter(x,y, color='cyan', marker='*')



using style sheets

One doesn’t have to style everything manually. There are built in style sheets that you can use. In addition, one can create one’s own style sheets.'fivethirtyeight')plt.hist(data)



other options

There are other methods and styling systems, including Seaborn, which highly recommend, as well as Plotly, Bokeh, and so forth.