< Previous Page | Home Page | Next Page >
Matplotlib is the go-to library for visualizing stuff in Python. Let's have a look at the basics.
Keep in Mind:
At first sight, it will seem that there are quite some components to consider when you start plotting with this Python data visualization library. Admittedly, it can be confusing and sometimes even discouraging seeing the amount of code that is necessary for some plots, not knowing where to start yourself and which components you should use. However, the course does only expect you to know the basics - so don't worry if some complex visualization might not work out the first few tries.
Before we dive into the details of creating visualizations with Matplotlib, there are a few useful things you should know about using the package.
Just as we use the np
shorthand for NumPy and the pd
shorthand for Pandas, we will use some standard shorthands for Matplotlib imports:
import matplotlib as mpl
import matplotlib.pyplot as plt
The plt
interface is what we will use most often, as we shall see throughout this chapter.
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:
plt.style.use('classic')
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 an IPython notebook.
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.
So, for example, you may have a file called myplot.py containing the following:
# ------- file: myplot.py ------
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
You can then run this script from the command-line prompt, which will result in a window opening with your figure displayed:
$ python myplot.py
The plt.show()
command does a lot under the hood, as it must interact with your system's interactive graphical backend.
The details of this operation can vary greatly from system to system and even installation to installation, but matplotlib does its best to hide all these details from you.
One thing to be aware of: the plt.show()
command should be used only once per Python session, and is most often seen at the very end of the script.
Multiple show()
commands can lead to unpredictable backend-dependent behavior, and should mostly be avoided.
You are currently reading this crashcourse in an IPython notebook. The notebook is a browser-based interactive data analysis tool that can combine narrative, code, graphics, HTML elements, and much more into a single executable document. Because of its flexibility and easy of use, we mainly use notebooks when doing Data Science!
Plotting interactively within an IPython notebook can be done with the %matplotlib
command. Here, you have the option of embedding graphics directly in the notebook, with two possible options:
%matplotlib notebook
will lead to interactive plots embedded within the notebook%matplotlib inline
will lead to static images of your plot embedded in the notebookFor this chapter, we will generally opt for %matplotlib inline
:
%matplotlib inline
After running this command (it needs to be done only once per kernel/session), any cell within the notebook that creates a plot will embed a PNG image of the resulting graphic - no need for the plt.show()
command:
import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--');
< Previous Page | Home Page | Next Page >