Matplotlib Flashcards
Basic plotting cycle: Plot sin and cos
x = np.linspace(0,10*math.pi,10000) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x,y1) plt.plot(x,y2) plt.show()
What does plot() do?
A very complex function. Extremely versatile
Plot y versus x as lines and/or markers.
Call signatures::
plot([x], y, [fmt], *, data=None, **kwargs) plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
The coordinates of the points or line nodes are given by x, y.
The optional parameter fmt is a convenient way for defining basicformatting like color, marker and linestyle.
Care to be taken with plt.show()
Use only onc3
Multiple show() commands can lead to
Unpredictable backend-dependent behavior
Embedding graphics directly in the notebook
%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
Draw sin and cos’s with ‘-‘ and ‘–’ lines and save it to a file.
fig = plt.figure()
plt. plot(x, np.sin(x), ‘-‘)
plt. plot(x, np.cos(x), ‘–’);
fig. savefig(‘my_figure.png’)
list of supported file types can be found for your system by using the following method of the figure canvas object:
fig.canvas.get_supported_filetypes()
an instance of the class plt.Figure
a single container that contains all the objects representing axes, graphics, text, and labels.
Axes
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.