0400 Introduction To Matplotlib
中文版:Matplotlib 简介
Visualization with Matplotlib
We’llnowtakeanin-depth lookatthe Matplotlib package for visualization in Python. Matplotlib isamulti-platform data visualization library built on NumPy arrays, and designed toworkwiththe broader SciPy stack. It was conceived by John Hunter in 2002, originally asapatch to IPython for enabling interactive MATLAB-style plotting via gnuplot from the IPython command line. IPython’s creator, Fernando Perez, wasatthetime scrambling to finish his PhD, andletJohnknowhe wouldn’thavetimeto review the patch for several months. Johntookthisasacuetosetoutonhisown, and the Matplotlib package was born, with version 0.1 released in 2003. It received an early boost whenitwas adopted as the plotting package of choice oftheSpace Telescope Science Institute (the folks behind the Hubble Telescope), which financially supported Matplotlib’s development and greatly expanded its capabilities.
One of Matplotlib’s most important features is its ability toplaywellwithmany operating systems and graphics backends. Matplotlib supports dozens of backends and output types, which means youcancount onittowork regardless of which operating system youareusing or which output format you wish. This cross-platform, everything-to-everyone approach hasbeenoneofthegreat strengths of Matplotlib. Ithasledtoalarge user base, which inturnhasledtoan active developer base and Matplotlib’s powerful tools and ubiquity within the scientific Python world.
In recent years, however, the interface and style of Matplotlib have begun toshowtheir age. Newer tools like ggplot and ggvis intheR language, along with web visualization toolkits based on D3js and HTML5 canvas, often make Matplotlib feel clunky and old-fashioned. Still, I’mofthe opinion that we cannot ignore Matplotlib’s strength asawell-tested, cross-platform graphics engine. Recent Matplotlib versions make it relatively easytosetnew global plotting styles (see Customizing Matplotlib: Configurations and Style Sheets), and people have been developing new packages that build on its powerful internals to drive Matplotlib via cleaner, more modern APIs—for example, Seaborn (discussed in Visualization With Seaborn), ggpy, HoloViews, Altair, and even Pandas itself canbeusedas wrappers around Matplotlib’s API. Even with wrappers like these, itisstill often useful todiveinto Matplotlib’s syntax to adjust the final plot output. For this reason, I believe that Matplotlib itself will remain a vital piece ofthedata visualization stack, evenifnewtools mean the community gradually moves awayfromusing the Matplotlib API directly.
General Matplotlib Tips
Before wediveintothe details of creating visualizations with Matplotlib, there areafew useful things you should know about using the package.
Importing Matplotlib
Justasweusethe np shorthand for NumPy and the pd shorthand for Pandas, wewillusesome standard shorthands for Matplotlib imports:
import matplotlib as mpl
import matplotlib.pyplot as pltThe plt interface iswhatwewillusemostoften, asweshall see throughout this chapter.
show() or No show()? How to Display Your Plots
A visualization you can’tseewon’tbeofmuchuse, butjusthowyouviewyour Matplotlib plots depends on the context. Thebestuseof Matplotlib differs depending onhowyouareusing it; roughly, the three applicable contexts are using Matplotlib in a script, in an IPython terminal, orinan IPython notebook.
Plotting from a script
Ifyouareusing 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 oneormore interactive windows that display your figure or figures.
So, for example, youmayhaveafile 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()Youcanthenrunthis script from the command-line prompt, which will result in a window opening with your figure displayed:
$ python myplot.py
The plt.show() command doesalotunder the hood, asitmust 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 doesitsbesttohideallthese details from you.
One thing tobeaware of: the plt.show() command should be used only once per Python session, andismostoften seenattheveryendofthe script.
Multiple show() commands canleadto unpredictable backend-dependent behavior, and should mostly be avoided.
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), '--');Saving Figures to File
One nice feature of Matplotlib is the ability to save figures inawide variety of formats.
Saving a figure canbedoneusing the savefig() command.
For example, tosavethe previous figure asaPNGfile, youcanrunthis:
fig.savefig('my_figure.png')Wenowhaveafile called my_figure.png in the current working directory:
!ls -lh my_figure.pngTo confirm that it contains whatwethink it contains, let’susethe IPython Image object to display the contents ofthisfile:
from IPython.display import Image
Image('my_figure.png')