Chapter 25. General Matplotlib Tips
Before we dive into the details of creating visualizations with Matplotlib, there are a few useful things you should know about using the package.
Importing Matplotlib
Just as we use the np shorthand for NumPy and the pd shorthand for
Pandas, we will use some standard shorthands for Matplotlib imports:
In[1]:importmatplotlibasmplimportmatplotlib.pyplotasplt
The plt interface is what we will use most often, as you shall see
throughout this part of the book.
Setting Styles
We will use the plt.style directive to choose appropriate aesthetic
styles for our figures. Here we will set the classic style, which
ensures that the plots we create use the classic Matplotlib style:
In[2]:plt.style.use('classic')
Throughout this chapter, we will adjust this style as needed. For more information on stylesheets, see Chapter 34.
show or No show? How to Display Your Plots
A visualization you can’t see won’t be of much use, but just how you view your Matplotlib plots depends on the context. The best use of Matplotlib differs depending on how you are using it; roughly, the three applicable contexts are using Matplotlib in a script, in an IPython terminal, or in a Jupyter notebook.
Plotting from a Script
If you are using Matplotlib from within a script, the function
plt.show is your friend. plt.show starts an event loop, looks for
all currently active Figure objects, and opens one or more interactive
windows that display your figure or figures.