57d938a3b3
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> |
||
---|---|---|
.. | ||
image0.png | ||
README.md |
Basics of matplotlib.pyplot
{:.no_toc}
* TOC {:toc}The goal
Questions to David Rotermund
pylab is a historic interface and its use is strongly discouraged. The equivalent replacement is matplotlib.pyplot
Simple example with plot
import numpy as np
import matplotlib.pyplot as plt # type: ignore
f: float = 10
t = np.linspace(0, 1.0, 10000)
y = np.sin(t * 2 * np.pi * f)
z = np.cos(t * 2 * np.pi * f)
plt.figure(1)
plt.plot(t, y, label="Sin")
plt.plot(t, z, label="Cos")
plt.xlabel("t")
plt.ylabel("f(t)")
plt.title("Some Functions")
plt.legend()
plt.savefig("Test.pdf")
plt.show() # This is optinal in Interactive Cell mode
Format Strings
A format string consists of a part for color, marker and line:
fmt = '[marker][line][color]'
Each of them is optional. If not provided, the value from the style cycle is used. Exception: If line is given, but no marker, the data will be a line without markers.
Other combinations such as [color][marker][line] are also supported, but note that their parsing may be ambiguous.
Managing Figure and Axes
axes | Add an Axes to the current figure and make it the current Axes. |
cla | Clear the current axes. |
clf | Clear the current figure. |
close | Close a figure window. |
delaxes | Remove an Axes (defaulting to the current axes) from its figure. |
fignum_exists | Return whether the figure with the given id exists. |
figure | Create a new figure, or activate an existing figure. |
gca | Get the current Axes. |
gcf | Get the current figure. |
get_figlabels | Return a list of existing figure labels. |
get_fignums | Return a list of existing figure numbers. |
sca | Set the current Axes to ax and the current Figure to the parent of ax. |
subplot | Add an Axes to the current figure or retrieve an existing Axes. |
subplot2grid | Create a subplot at a specific location inside a regular grid. |
subplot_mosaic | Build a layout of Axes based on ASCII art or nested lists. |
subplots | Create a figure and a set of subplots. |
twinx | Make and return a second axes that shares the x-axis. |
twiny | Make and return a second axes that shares the y-axis. |
Basic
more Basic
plot | Plot y versus x as lines and/or markers. |
errorbar | Plot y versus x as lines and/or markers with attached errorbars. |
scatter | A scatter plot of y vs. |
step | Make a step plot. |
loglog | Make a plot with log scaling on both the x- and y-axis. |
semilogx | Make a plot with log scaling on the x-axis. |
semilogy | Make a plot with log scaling on the y-axis. |
bar | Make a bar plot. |
barh | Make a horizontal bar plot. |
bar_label | Label a bar plot. |
pie | Plot a pie chart. |
vlines | Plot vertical lines at each x from ymin to ymax. |
hlines | Plot horizontal lines at each y from xmin to xmax. |
fill | Plot filled polygons. |
polar | Make a polar plot. |
Spans
Spectral
Binned
more Binned
hist | Compute and plot a histogram. |
hist2d | Make a 2D histogram plot. |
Contours
clabel | Label a contour plot. |
contour | Plot contour lines. |
contourf | Plot filled contours. |
2D arrays
more 2D arrays
imshow | Display data as an image, i.e., on a 2D regular raster. |
Unstructured triangles
Text and annotations
annotate | Annotate the point xy with text text. |
text | Add text to the Axes. |
figtext | Add text to figure. |
table | Add a table to an Axes. |
arrow | Add an arrow to the Axes. |
figlegend | Place a legend on the figure. |
legend | Place a legend on the Axes. |
Vector fields
Axis configuration
autoscale | Autoscale the axis view to the data (toggle). |
axis | Convenience method to get or set some axis properties. |
box | Turn the axes box on or off on the current axes. |
grid | Configure the grid lines. |
locator_params | Control behavior of major tick locators. |
minorticks_off | Remove minor ticks from the Axes. |
minorticks_on | Display minor ticks on the Axes. |
rgrids | Get or set the radial gridlines on the current polar plot. |
thetagrids | Get or set the theta gridlines on the current polar plot. |
tick_params | Change the appearance of ticks, tick labels, and gridlines. |
ticklabel_format | Configure the ScalarFormatter used by default for linear Axes. |
xlabel | Set the label for the x-axis. |
xlim | Get or set the x limits of the current axes. |
xscale | Set the xaxis' scale. |
xticks | Get or set the current tick locations and labels of the x-axis. |
ylabel | Set the label for the y-axis. |
ylim | Get or set the y-limits of the current axes. |
yscale | Set the yaxis' scale. |
yticks | Get or set the current tick locations and labels of the y-axis. |
title | Set a title for the Axes. |
Layout
more Layout
margins | Set or retrieve autoscaling margins. |
subplots_adjust | Adjust the subplot layout parameters. |
tight_layout | Adjust the padding between and around subplots. |
Colormapping
more Colormapping
clim | Set the color limits of the current image. |
colorbar | Add a colorbar to a plot. |
sci | Set the current image. |
get_cmap | Get a colormap instance, defaulting to rc values if name is None. |
set_cmap | Set the default colormap, and applies it to the current image if any. |
imread | Read an image from a file into an array. |
imsave | Colormap and save an array as an image file. |
Configuration
more Configuration
rc | Set the current rcParams. group is the grouping for the rc, e.g., for lines.linewidth the group is lines, for axes.facecolor, the group is axes, and so on. Group may also be a list or tuple of group names, e.g., (xtick, ytick). kwargs is a dictionary attribute name/value pairs, e.g.,::. |
rcdefaults | Restore the rcParams from Matplotlib's internal default style. |
Output
more Output
draw | Redraw the current figure. |
ioff | Disable interactive mode. |
ion | Enable interactive mode. |
pause | Run the GUI event loop for interval seconds. |
savefig | Save the current figure. |
show | Display all open figures. |
Other
more Other
waitforbuttonpress | Blocking call to interact with the figure. |