Introduction to pyplot-unit-05
Introduction to pyplot-unit-05
In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things
like the current figure and plotting area, and the plotting functions are directed to the current Axes
Note
The implicit pyplot API is generally less verbose but also not as flexible as the explicit API. Most of the
function calls you see here can also be called as methods from an Axes object. We recommend
browsing the tutorials and examples to see how this works.
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
You may be wondering why the x-axis ranges from 0-3 and the y-axis from 1-4. If you provide a single
list or array to plot, matplotlib assumes it is a sequence of y values, and automatically generates the
x values for you. Since python ranges start with 0, the default x vector has the same length as y but
starts with 0; therefore, the x data are [0, 1, 2, 3].
plot is a versatile function, and will take an arbitrary number of arguments. For example, to plot x
versus y, you can write:
For every x, y pair of arguments, there is an optional third argument which is the format string that
indicates the color and line type of the plot. The letters and symbols of the format string are from
MATLAB, and you concatenate a color string with a line style string. The default format string is 'b-',
which is a solid blue line. For example, to plot the above with red circles, you would issue
plt.axis((0, 6, 0, 20))
plt.show()
See the plot documentation for a complete list of line styles and format strings. The axis function in
the example above takes a list of [xmin, xmax, ymin, ymax] and specifies the viewport of the Axes.
If matplotlib were limited to working with lists, it would be fairly useless for numeric processing.
Generally, you will use numpy arrays. In fact, all sequences are converted to numpy arrays internally.
The example below illustrates plotting several lines with different format styles in one function call
using arrays.
import numpy as np
plt.show()
Plotting with keyword strings
There are some instances where you have data in a format that lets you access particular variables
with strings. For example, with structured arrays or pandas.DataFrame.
Matplotlib allows you to provide such an object with the data keyword argument. If provided, then
you may generate plots with the strings corresponding to these variables.
'd': np.random.randn(50)}
plt.xlabel('entry a')
plt.ylabel('entry b')
plt.show()
Plotting with categorical variables
It is also possible to create a plot using categorical variables. Matplotlib allows you to pass categorical
variables directly to many plotting functions. For example:
plt.figure(figsize=(9, 3))
plt.subplot()
plt.bar(names, values)
plt.subplot()
plt.scatter(names, values)
plt.subplot()
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()
Controlling line properties
Lines have many attributes that you can set: linewidth, dash style, antialiased, etc;
see matplotlib.lines.Line2D. There are several ways to set line properties
plt.plot(x, y, linewidth=2.0)
Use the setter methods of a Line2D instance. plot returns a list of Line2D objects;
e.g., line1, line2 = plot(x1, y1, x2, y2). In the code below we will suppose that we have only
one line so that the list returned is of length 1. We use tuple unpacking with line, to get the
first element of that list:
Use setp. The example below uses a MATLAB-style function to set multiple properties on a
list of lines. setp works transparently with a list of objects or a single object. You can either
use python keyword arguments or MATLAB-style string/value pairs: