3.14. Matplotlib¶
reference: Python Data Science Handbook
Matplotlib is a multi-platform data visualization library built on NumPy arrays, and designed to work with the broader SciPy stack.
It was conceived by John Hunter in 2002,
originally as a patch to IPython for enabling interactive MATLAB-style plotting via gnuplot from the IPython command line.
One of Matplotlib’s most important features is its ability to play well with many operating systems and graphics backends.
3.14.1. Importing Matplotlib¶
3.14.2. Setting Styles¶
Here we will set the classic style, which ensures that the plots we create use the classic Matplotlib style:
3.14.2.1. Plotting from a script¶
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
3.14.2.2. Plotting from an IPython notebook¶
Plotting interactively within an IPython notebook can be done with the %matplotlib command,
and works in a similar way to the IPython shell. In the IPython notebook,
you also 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 notebook
For this book, we will generally opt for %matplotlib inline:
3.14.3. Saving Figures to File¶
To confirm that it contains what we think it contains, let’s use the IPython Image object to display the contents of this file:
3.14.4. Two Interfaces¶
MATLAB-style Interface
Matplotlib was originally written as a Python alternative for MATLAB users, and much of its syntax reflects that fact.
The MATLAB-style tools are contained in the pyplot (plt) interface.
For example, the following code will probably look quite familiar to MATLAB users:
Object-oriented interface
The object-oriented interface is available for these more complicated situations,
and for when you want more control over your figure.
Rather than depending on some notion of an “active” figure or axes,
in the object-oriented interface the plotting functions are methods of explicit Figure and Axes objects.
To re-create the previous plot using this style of plotting, you might do the following:
3.14.5. Line Plots¶
3.14.6. Scatter Plot¶
3.14.7. Visualizing Errors¶
errorbar(x, y, yerr=None, xerr=None, fmt=’’, ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, *, data=None, **kwargs)
3.14.7.1. Continuous Error¶
3.14.8. Contour Plots¶
matplotlib.pyplot.contour(*args, data=None, **kwargs)
3.14.9. Histograms, Binnings, and Density¶
hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, *, data=None, **kwargs)
3.14.9.1. Two-Dimensional Histograms and Binnings¶
hist2d(x, y, bins=10, range=None, density=False, weights=None, cmin=None, cmax=None, *, data=None, **kwargs)