matplotlib¶
Matplotlib plotting functions
e.g.
set_plot_defaults() axs = create_multiplot(2, 2, title='New figure') plot_line(axs[0], x, y) plot_line(axs[1], x2, y2, label='data 2') plot_lines(ax[2], x, [y, y2]) plot_detector_image(ax[3], image) labels('title', 'x', 'y', legend=True, axes=axs[0])
DEFAULT_CMAP = 'viridis'
module-attribute
¶
----------------------------Plot manipulation--------------------------
generate_subplots(n_plots, subplots=(4, 4), ax_size=(5, 3), suptitle=None)
¶
Generate a grid of n_plots. If n_plots is larger than the number of subplots on a figure, multiple figures will be generated.
fig_ax = generate_subplots(25, subplots=(4, 4), suptitle='plots')
for fig, ax in fig_ax:
ax.plot(x, y)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_plots
|
int
|
number of subplots to generate |
required |
subplots
|
tuple[int, int]
|
[nrows, ncols] number of subplots to generate per figure [vertical, horizontal] |
(4, 4)
|
ax_size
|
tuple[float, float]
|
[horiz, vert] size of each axis in inches (scaled by dpi) |
(5, 3)
|
suptitle
|
str | None
|
title of each figure (the figure number will be appended) |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[Figure, Axes]]
|
list of (fig, axes), length n_plots |
Source code in mmg_toolbox/plotting/matplotlib.py
plot_2d_surface(axes, image, xdata=None, ydata=None, clim=None, axlim='image', **kwargs)
¶
Plot 2D data as colourmap surface
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
Axes
|
matplotlib figure or subplot axes, None uses current axe |
required |
image
|
ndarray
|
2d array image data |
required |
xdata
|
ndarray | None
|
array data, 2d or 1d |
None
|
ydata
|
ndarray
|
array data 2d or 1d |
None
|
clim
|
tuple[float, float]
|
None or [min, max] values for color cutoff from plt.clim |
None
|
axlim
|
str
|
axis limits from plt.axis |
'image'
|
kwargs
|
additional arguments for plt.pcolormesh |
{}
|
Returns:
| Type | Description |
|---|---|
QuadMesh
|
output of plt.pcolormesh |
Source code in mmg_toolbox/plotting/matplotlib.py
plot_3d_lines(axes, zdata, xdata=None, ydata=None, labels=None, **kwargs)
¶
Plot 2D image data as 3d surface
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
Axes3D
|
matplotlib figure or subplot axes, None uses current axe |
required |
zdata
|
list[ndarray]
|
2d array [n, m] |
required |
xdata
|
list[ndarray] | None
|
array data, 2d or 1d |
None
|
ydata
|
list[ndarray] | None
|
array data 2d or 1d |
None
|
labels
|
list[str] | None
|
list of labels for each line, or None |
None
|
kwargs
|
additional arguments for plt.plot |
{}
|
Returns:
| Type | Description |
|---|---|
list[Line2D]
|
output of plt.plot |
Source code in mmg_toolbox/plotting/matplotlib.py
plot_3d_surface(axes, image, xdata=None, ydata=None, samples=None, clim=None, axlim='auto', **kwargs)
¶
Plot 2D image data as 3d surface
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
Axes3D
|
matplotlib figure or subplot axes, None uses current axe |
required |
image
|
ndarray
|
2d array image data |
required |
xdata
|
ndarray | None
|
array data, 2d or 1d |
None
|
ydata
|
ndarray | None
|
array data 2d or 1d |
None
|
samples
|
int | None
|
max number of points to take in each direction, by default does not downsample |
None
|
clim
|
tuple[int, int] | None
|
None or [min, max] values for color cutoff from plt.clim |
None
|
axlim
|
str
|
axis limits from plt.axis |
'auto'
|
kwargs
|
additional arguments for plt.plot_surface |
{}
|
Returns:
| Type | Description |
|---|---|
Poly3DCollection
|
output of plt.plot_surface |
Source code in mmg_toolbox/plotting/matplotlib.py
plot_image(axes, image, clim=None, *args, **kwargs)
¶
Plot 2D image
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
Axes
|
matplotlib figure or subplot axes, None uses current axe |
required |
image
|
ndarray
|
2d array image data |
required |
clim
|
tuple[float, float]
|
None or [min, max] values for color cutoff |
None
|
args
|
additional arguments for plt.pcolormesh |
()
|
|
kwargs
|
additional arguments for plt.pcolormesh |
{}
|
Returns:
| Type | Description |
|---|---|
Axes
|
axes object |
Source code in mmg_toolbox/plotting/matplotlib.py
plot_line(axes, xdata, ydata, yerrors=None, line_spec='-o', *args, **kwargs)
¶
Plot line on given matplotlib axes subplot Uses matplotlib.plot or matplotlib.errorbar if yerrors is not None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
Axes
|
matplotlib figure or subplot axes, None uses current axes |
required |
xdata
|
ndarray
|
array data on x axis |
required |
ydata
|
ndarray
|
array data on y axis |
required |
yerrors
|
ndarray | None
|
array errors on y axis (or None) |
None
|
line_spec
|
str
|
str matplotlib.plot line_spec |
'-o'
|
args
|
additional arguments |
()
|
|
kwargs
|
additional arguments |
{}
|
Returns:
| Type | Description |
|---|---|
list[Line2D]
|
output of plt.plot [line], or plt.errorbar [line, xerrors, yerrors] |
Source code in mmg_toolbox/plotting/matplotlib.py
plot_lines(axes, *plot_data, cmap=DEFAULT_CMAP, line_spec='-o', **kwargs)
¶
Plot lines on given matplotlib axes subplot Uses matplotlib.plot or matplotlib.errorbar if yerrors is not None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
Axes
|
matplotlib figure or subplot axes, None uses current axes |
required |
plot_data
|
PlotData
|
[value, xdata, ydata, yerrors] or [value, xdata, ydata] |
()
|
cmap
|
str
|
name of colormap to generate colour variation in lines |
DEFAULT_CMAP
|
line_spec
|
str
|
str or list[m] of str matplotlib.plot line_spec |
'-o'
|
kwargs
|
additional arguments |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[list[Line2D], ScalarMappable]
|
ScalarMappable for use in colorbar |
Source code in mmg_toolbox/plotting/matplotlib.py
set_plot_defaults(rcdefaults=False)
¶
Set custom matplotlib rcparams, or revert to matplotlib defaults These handle the default look of matplotlib plots See: https://matplotlib.org/stable/tutorials/introductory/customizing.html#the-default-matplotlibrc-file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rcdefaults
|
False*/ True, if True, revert to matplotlib defaults |
False
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in mmg_toolbox/plotting/matplotlib.py
set_span_bounds(span, xmin, xmax, ymin, ymax)
¶
Set bounds for span=ax.axvspan, working for old matplotlib versions