Best practices for Matplotlib plots

Related: Date/time in Matplotlib

The most stable and robust way to use Matplotlib is to use the “object oriented” interface.

Matplotlib: Object-oriented

from matplotlib.pyplot import figure,show

f1 = figure()
a1 = f1.gca()
p1 = a1.plot(x,y)

a1.set_title('my plot')
a1.set_xlabel('x [in]')
a1.set_ylabel('y [out]')

#... (more plots)

show() # program waits here for figure to close or program to end.

You can do virtually everything from the OO interface without the risk of updating the wrong plot as with the state machine.

Matplotlib: state machine

The state machine method is easier, but risks updating the wrong plot, as the plot in focus is updated.

# don't do the following
import matplotlib.pyplot as plt

plt.title('my figure')
plt.xlabel('x [in]')


Useful reference guide for moderately advanced Matplotlib graphs.