%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
fig = plt.figure()
ax = plt.axes()
plt.Figure
) can be thought of as a single container that contains all the objects representing axes, graphics, text, and labels.
The axes (an instance of the class plt.Axes
) is what we see above: a bounding box with ticks and labels, which will eventually contain the plot elements that make up our visualization.
Throughout this book, we'll commonly use the variable name fig
to refer to a figure instance, and ax
to refer to an axes instance or group of axes instances.ax.plot
function to plot some data. Let's start with a simple sinusoid:fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x));
plt.plot(x, np.sin(x));
plot
function multiple times:plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x));
plt.plot()
function takes additional arguments that can be used to specify these.
To adjust the color, you can use the color
keyword, which accepts a string argument representing virtually any imaginable color.
The color can be specified in a variety of ways:plt.plot(x, np.sin(x - 0), color='blue') # specify color by name
plt.plot(x, np.sin(x - 1), color='g') # short color code (rgbcmyk)
plt.plot(x, np.sin(x - 2), color='0.75') # Grayscale between 0 and 1
plt.plot(x, np.sin(x - 3), color='#FFDD44') # Hex code (RRGGBB from 00 to FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 to 1
plt.plot(x, np.sin(x - 5), color='chartreuse'); # all HTML color names supported
linestyle
keyword:plt.plot(x, x + 0, linestyle='solid')
plt.plot(x, x + 1, linestyle='dashed')
plt.plot(x, x + 2, linestyle='dashdot')
plt.plot(x, x + 3, linestyle='dotted');
# For short, you can use the following codes:
plt.plot(x, x + 4, linestyle='-') # solid
plt.plot(x, x + 5, linestyle='--') # dashed
plt.plot(x, x + 6, linestyle='-.') # dashdot
plt.plot(x, x + 7, linestyle=':'); # dotted
linestyle
and color
codes can be combined into a single non-keyword argument to the plt.plot()
function:plt.plot(x, x + 0, '-g') # solid green
plt.plot(x, x + 1, '--c') # dashed cyan
plt.plot(x, x + 2, '-.k') # dashdot black
plt.plot(x, x + 3, ':r'); # dotted red
plt.plot()
function using IPython's help tools (See Help and Documentation in IPython).plt.xlim()
and plt.ylim()
methods:plt.plot(x, np.sin(x))
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5);
plt.plot(x, np.sin(x))
plt.xlim(10, 0)
plt.ylim(1.2, -1.2);
plt.axis()
(note here the potential confusion between axes with an e, and axis with an i).
The plt.axis()
method allows you to set the x
and y
limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax]
:plt.plot(x, np.sin(x))
plt.axis([-1, 11, -1.5, 1.5]);
plt.axis()
method goes even beyond this, allowing you to do things like automatically tighten the bounds around the current plot:plt.plot(x, np.sin(x))
plt.axis('tight');
x
is equal to one unit in y
:plt.plot(x, np.sin(x))
plt.axis('equal');
plt.axis
method, refer to the plt.axis
docstring.plt.plot(x, np.sin(x))
plt.title("A Sine Curve")
plt.xlabel("x")
plt.ylabel("sin(x)");
plt.legend()
method.
Though there are several valid ways of using this, I find it easiest to specify the label of each line using the label
keyword of the plot function:plt.plot(x, np.sin(x), '-g', label='sin(x)')
plt.plot(x, np.cos(x), ':b', label='cos(x)')
plt.axis('equal')
plt.legend();
plt.legend()
function keeps track of the line style and color, and matches these with the correct label.
More information on specifying and formatting plot legends can be found in the plt.legend
docstring; additionally, we will cover some more advanced legend options in Customizing Plot Legends.plt
functions translate directly to ax
methods (such as plt.plot()
→ ax.plot()
, plt.legend()
→ ax.legend()
, etc.), this is not the case for all commands.
In particular, functions to set limits, labels, and titles are slightly modified.
For transitioning between MATLAB-style functions and object-oriented methods, make the following changes:plt.xlabel()
→ ax.set_xlabel()
plt.ylabel()
→ ax.set_ylabel()
plt.xlim()
→ ax.set_xlim()
plt.ylim()
→ ax.set_ylim()
plt.title()
→ ax.set_title()
ax.set()
method to set all these properties at once:ax = plt.axes()
ax.plot(x, np.sin(x))
ax.set(xlim=(0, 10), ylim=(-2, 2),
xlabel='x', ylabel='sin(x)',
title='A Simple Plot');