0401 Simple Line Plots
中文版:简单折线图
Simple Line Plots
Perhaps the simplest ofallplots is the visualization of a single function . Herewewilltakeafirst look at creating a simple plotofthistype. Aswithallthe following sections, we’ll start by setting up the notebook for plotting and importing the packages wewilluse:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as npFor all Matplotlib plots, we start by creating a figure andanaxes. In their simplest form, a figure andaxescanbe created as follows:
fig = plt.figure()
ax = plt.axes()In Matplotlib, the figure (an instance oftheclass plt.Figure) can be thought ofasa single container that contains all the objects representing axes, graphics, text, and labels.
The axes (an instance oftheclass plt.Axes) iswhatweseeabove: a bounding boxwithticks and labels, which will eventually contain the plot elements thatmakeupour visualization.
Throughout this book, we’ll commonly use the variable name fig to refer to a figure instance, and ax to refer toanaxes instance or group of axes instances.
Oncewehave created an axes, wecanusethe ax.plot function toplotsomedata. 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));Ifwewantto create a single figure with multiple lines, we can simply call the plot function multiple times:
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x));That’sallthere is to plotting simple functions in Matplotlib! We’llnowdiveintosomemore details about how to control the appearance oftheaxesandlines.
Adjusting the Plot: Line Colors and Styles
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, youcanusethe 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=':'); # dottedIfyouwould liketobe extremely terse, these 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 redThese single-character color codes reflect the standard abbreviations intheRGB (Red/Green/Blue) and CMYK (Cyan/Magenta/Yellow/blacK) color systems, commonly used for digital color graphics.
There aremanyother keyword arguments thatcanbeusedtofine-tune the appearance oftheplot; for more details, I’d suggest viewing the docstring of the plt.plot() function using IPython’shelptools (See Help and Documentation in IPython).
Adjusting the Plot: Axes Limits
Matplotlib does a decent job of choosing default axes limits foryourplot, but sometimes it’snicetohavefiner control.
Themostbasic way to adjust axis limits istousethe plt.xlim() and plt.ylim() methods:
plt.plot(x, np.sin(x))
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5);Ifforsome reason you’d like either axistobe displayed in reverse, you can simply reverse the order of the arguments:
plt.plot(x, np.sin(x))
plt.xlim(10, 0)
plt.ylim(1.2, -1.2);A useful related method is plt.axis() (noteherethe potential confusion between axes with an e, and axis with an i).
The plt.axis() method allows youtosetthe x and y limits with a single call, by passing alistwhich specifies [xmin, xmax, ymin, ymax]:
plt.plot(x, np.sin(x))
plt.axis([-1, 11, -1.5, 1.5]);