erlab.plotting.general¶
General plotting utilities.
Functions
|
Get the extent of a |
|
|
|
|
|
Clean the labels of the given axes. |
|
Plot a constant energy line to denote the Fermi level. |
|
|
|
Apply a gradient fill to a line plot. |
|
Easy placement of inset axes. |
|
Plot a 2D |
|
Plot a 2D array with associated color array. |
|
Automated comparison plot of slices. |
Classes
|
A crosshair cursor that spans the axes and moves with mouse cursor. |
- class erlab.plotting.general.LabeledCursor(ax, horizOn=True, vertOn=True, textOn=True, useblit=True, textprops=None, **lineprops)[source]¶
Bases:
AxesWidget
A crosshair cursor that spans the axes and moves with mouse cursor.
For the cursor to remain responsive you must keep a reference to it. Unlike
matplotlib.widgets.Cursor
, this also shows the current cursor location.- Parameters:
ax (matplotlib.axes.Axes) – The
matplotlib.axes.Axes
to attach the cursor to.horizOn (bool) – Whether to draw the horizontal line.
vertOn (bool) – Whether to draw the vertical line.
textOn (bool) – Whether to show current cursor location.
useblit (bool) – Use blitting for faster drawing if supported by the backend.
textprops (dict | None) – Keyword arguments to pass onto the text object.
**lineprops –
matplotlib.lines.Line2D
properties that control the appearance of the lines. See alsomatplotlib.axes.Axes.axhline
.
- erlab.plotting.general.clean_labels(axes, remove_inner_ticks=False, **kwargs)[source]¶
Clean the labels of the given axes.
This function removes the labels from the axes except for the outermost axes and prettifies the remaining labels with
fancy_labels
.Changed in version 2.5.0: The function now calls
Axes.label_outer
recursively instead of setting the labels to an empty string.- Parameters:
remove_inner_ticks (bool) – If
True
, remove the inner ticks as well (not only tick labels).**kwargs – Additional keyword arguments to be passed to
fancy_labels
.
- erlab.plotting.general.fermiline(ax=None, value=0.0, orientation='h', **kwargs)[source]¶
Plot a constant energy line to denote the Fermi level.
- Parameters:
ax (Axes | None) – The
matplotlib.axes.Axes
to annotate.value (float) – The coordinate of the line. Defaults to 0, assuming binding energy.
orientation (Literal['h', 'v']) – If ‘h’, a horizontal line is plotted. If ‘v’, a vertical line is plotted.
**kwargs – Keyword arguments passed onto
matplotlib.lines.Line2D
.
- Return type:
- erlab.plotting.general.figwh(ratio=0.6180339887498948, wide=0, wscale=1, style='aps', fixed_height=True)[source]¶
- erlab.plotting.general.gradient_fill(x, y, y0=None, color='C0', cmap=None, transpose=False, reverse=False, ax=None, **kwargs)[source]¶
Apply a gradient fill to a line plot.
- Parameters:
x (Collection[int | float]) – Data of the plot to fill under.
y (Collection[int | float]) – Data of the plot to fill under.
y0 (float | None) – The minimum y value of the gradient. If
None
, defaults to the minimum ofy
.color (str | tuple[float, float, float] | tuple[float, float, float, float]) – A valid matplotlib color to make the gradient from.
cmap (str | matplotlib.colors.Colormap | None) – If given, ignores
color
and fills with the given colormap.transpose (bool) – Transpose the gradient.
reverse (bool) – Reverse the gradient.
ax (matplotlib.axes.Axes | None) – The
matplotlib.axes.Axes
to plot in.**kwargs – Keyword arguments passed onto
matplotlib.axes.Axes.imshow()
.
- Return type:
- erlab.plotting.general.place_inset(parent_axes, width, height, pad=0.1, loc='upper right', **kwargs)[source]¶
Easy placement of inset axes.
- Parameters:
parent_axes (Axes) –
matplotlib.axes.Axes
to place the inset axes.width (float | str) – Size of the inset axes to create. If
float
, specifies the size in inches, e.g.1.3
. Ifstr
, specifies the size in relative units, e.g.'40%'
ofparent_axes
.height (float | str) – Size of the inset axes to create. If
float
, specifies the size in inches, e.g.1.3
. Ifstr
, specifies the size in relative units, e.g.'40%'
ofparent_axes
.pad (float | tuple[float, float]) – Padding between
parent_axes
and inset in inches.loc (Literal['upper left', 'upper center', 'upper right', 'center left', 'center', 'center right', 'lower left', 'lower center', 'lower right']) – Location to place the inset axes.
**kwargs – Keyword arguments are passed onto
matplotlib.axes.Axes.inset_axes
.
- Return type:
- erlab.plotting.general.plot_array(arr, ax=None, *, colorbar=False, colorbar_kw=None, gamma=1.0, norm=None, xlim=None, ylim=None, crop=False, rad2deg=False, func=None, func_args=None, rtol=1.0e-5, atol=1.0e-8, **improps)[source]¶
Plot a 2D
xarray.DataArray
usingmatplotlib.pyplot.imshow()
.- Parameters:
arr (xr.DataArray) – A two-dimensional
xarray.DataArray
with evenly spaced coordinates.ax (matplotlib.axes.Axes | None) – The target
matplotlib.axes.Axes
.colorbar (bool) – Whether to plot a colorbar.
colorbar_kw (dict | None) – Keyword arguments passed onto
erlab.plotting.colors.nice_colorbar()
.xlim (float | tuple[float, float] | None) – If given a sequence of length 2, those values are set as the lower and upper limits of each axis. If given a single
float
, the limits are set as(-lim, lim)
. IfNone
, automatically determines the limits from the data.ylim (float | tuple[float, float] | None) – If given a sequence of length 2, those values are set as the lower and upper limits of each axis. If given a single
float
, the limits are set as(-lim, lim)
. IfNone
, automatically determines the limits from the data.rad2deg (bool | Iterable[str]) – If
True
, converts some known angle coordinates from radians to degrees. If an iterable ofstr
is given, only the coordinates that correspond to the given strings are converted.func (Callable | None) – A callable that processes the values prior to display. Its output must have the same shape as the input.
func_args (dict | None) – Keyword arguments passed onto
func
.rtol (float) – By default, the input array is checked for evenly spaced coordinates.
rtol
andatol
are the tolerances for the coordinates to be considered evenly spaced. The default values are consistent withnumpy.isclose
.atol (float) – By default, the input array is checked for evenly spaced coordinates.
rtol
andatol
are the tolerances for the coordinates to be considered evenly spaced. The default values are consistent withnumpy.isclose
.**improps – Keyword arguments passed onto
matplotlib.axes.Axes.imshow()
.
- Return type:
- erlab.plotting.general.plot_array_2d(larr, carr, ax=None, *, normalize_with_larr=False, xlim=None, ylim=None, cmap=None, lnorm=None, cnorm=None, background=None, colorbar=True, cax=None, colorbar_kw=None, imshow_kw=None, N=256, rtol=1.0e-5, atol=1.0e-8, **indexers_kwargs)[source]¶
Plot a 2D array with associated color array.
The lightness array represents the intensity values, while the color array represents some other property. The arrays must have the same shape.
- Parameters:
larr (xr.DataArray) – The 2D array representing the lightness values.
carr (xr.DataArray) – The 2D array representing the color values.
ax (matplotlib.axes.Axes | None) – The axes on which to plot the array. If None, the current axes will be used.
normalize_with_larr (bool) – Whether to normalize the color array with the lightness array. Default is False.
xlim (float | tuple[float, float] | None) – The x-axis limits for the plot. If a float, it represents the symmetric limits around 0. If a tuple, it represents the lower and upper limits. If None, the limits are determined from the data.
ylim (float | tuple[float, float] | None) – The y-axis limits for the plot. If a float, it represents the symmetric limits around 0. If a tuple, it represents the lower and upper limits. If None, the limits are determined from the data.
cmap (matplotlib.colors.Colormap | str | None) – The colormap to use for the color array. If None, a linear segmented colormap consisting of blue, black, and red is used.
lnorm (matplotlib.colors.Normalize | None) – The normalization object for the lightness array.
cnorm (matplotlib.colors.Normalize | None) – The normalization object for the color array.
background (ColorType | None) – The background color to use for the plot. If None, white is used.
colorbar (bool) – Whether to create a colorbar. Default is
True
.cax (matplotlib.axes.Axes | None) – The axes on which to create the colorbar if
colorbar
isTrue
. If None, a new axes will be created for the colorbar.colorbar_kw (dict | None) – Additional keyword arguments to pass to
matplotlib.pyplot.colorbar
.imshow_kw (dict | None) – Additional keyword arguments to pass to
matplotlib.pyplot.imshow
.N (int) – The number of levels in the colormap. Default is 256.
rtol (float) – By default, the input array is checked for evenly spaced coordinates.
rtol
andatol
are the tolerances for the coordinates to be considered evenly spaced. The default values are consistent withnumpy.isclose
.atol (float) – By default, the input array is checked for evenly spaced coordinates.
rtol
andatol
are the tolerances for the coordinates to be considered evenly spaced. The default values are consistent withnumpy.isclose
.**indexers_kwargs (
dict
) – Additional keyword arguments to pass toqsel
to select the data to plot. Note that the resulting data after the selection must be 2D.
- Returns:
im (
matplotlib.image.AxesImage
) – The plotted image.cb (
matplotlib.colorbar.Colorbar
orNone
) – The colorbar associated with the plot. Ifcolorbar
is False, None is returned.
- Return type:
tuple[matplotlib.image.AxesImage, matplotlib.colorbar.Colorbar | None]
Example
>>> import erlab.plotting.erplot as eplt >>> import matplotlib.pyplot as plt >>> import xarray as xr >>> larr = xr.DataArray([[1, 2, 3], [4, 5, 6]]) >>> carr = xr.DataArray([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]) >>> eplt.plot_array_2d(larr, carr)
- erlab.plotting.general.plot_slices(maps, figsize=None, *, transpose=False, xlim=None, ylim=None, crop=True, same_limits=False, axis='auto', show_all_labels=False, colorbar='none', hide_colorbar_ticks=True, annotate=True, cmap=None, norm=None, order='C', cmap_order='C', norm_order=None, gradient=False, gradient_kw=None, subplot_kw=None, annotate_kw=None, colorbar_kw=None, axes=None, **values)[source]¶
Automated comparison plot of slices.
- Parameters:
maps (xr.DataArray | Sequence[xr.DataArray]) – Arrays to plot.
transpose (bool) – Transpose each map before plotting.
xlim (float | tuple[float, float] | None) – If given a sequence of length 2, those values are set as the lower and upper limits of each axis. If given a single
float
, the limits are set as(-lim, lim)
. IfNone
, automatically determines the limits from the data.ylim (float | tuple[float, float] | None) – If given a sequence of length 2, those values are set as the lower and upper limits of each axis. If given a single
float
, the limits are set as(-lim, lim)
. IfNone
, automatically determines the limits from the data.crop (bool) – If
True
, crops the data to the limits given byxlim
andylim
prior to plotting.same_limits (bool) – If
True
, all images will have the same vmin and vmax.axis (Literal['on', 'off', 'equal', 'scaled', 'tight', 'auto', 'image', 'scaled', 'square']) –
Passed onto
matplotlib.axes.Axes.axis()
. Possible values are:Value
Description
’on’
Turn on axis lines and labels.
’off’
Turn off axis lines and labels.
’equal’
Set equal scaling (i.e., make circles circular) by changing axis limits. This is the same as
ax.set_aspect('equal', adjustable='datalim')
. Explicit data limits may not be respected in this case.’scaled’
Set equal scaling (i.e., make circles circular) by changing dimensions of the plot box. This is the same as
ax.set_aspect('equal', adjustable='box', anchor='C')
. Additionally, further autoscaling will be disabled.’tight’
Set limits just large enough to show all data, then disable further autoscaling.
’auto’
Automatic scaling (fill plot box with data).
’image’
’scaled’ with axis limits equal to data limits.
’square’
Square plot; similar to ‘scaled’, but initially forcing
xmax-xmin == ymax-ymin
.show_all_labels (bool) – If
True
, shows every xlabel and ylabel. IfFalse
, labels on shared axes are minimized. WhenFalse
and theaxes
argument is given, theorder
must be specified to correctly hide shared labels.colorbar (Literal['none', 'right', 'rightspan', 'all']) –
Controls colorbar behavior. Possible values are:
Value
Description
’none’
Do not show colorbars.
’right’
Creates a colorbar on the right for each row.
’rightspan’
Create a single colorbar that spans all axes.
’all’
Plot a colorbar for every axes.
cmap (str | matplotlib.colors.Colormap | Iterable[str | matplotlib.colors.Colormap | Iterable[matplotlib.colors.Colormap | str]] | None) – If supplied a single
str
ormatplotlib.colors.Colormap
, the colormap is applied to all axes. Otherwise, a nested sequence with the same shape as the resulting axes can be provided to use different colormaps for different axes. If the slices are 1D, this argument can be used to supply valid colors as line colors for differnet slices.norm (matplotlib.colors.Normalize | Iterable[matplotlib.colors.Normalize | Iterable[matplotlib.colors.Normalize]] | None) – If supplied a single
matplotlib.colors.Normalize
, the norm is applied to all axes. Otherwise, a nested sequence with the same shape as the resulting axes can be provided to use different norms for different axes.order (Literal['C', 'F']) – Order to display the data. Effectively, this determines if each map is displayed along the same row or the same column. ‘C’ means to flatten in row-major (C-style) order, and ‘F’ means to flatten in column-major (Fortran-style) order.
cmap_order (Literal['C', 'F']) – The order to flatten when given a nested sequence for
cmap
, Defaults toorder
.norm_order (Literal['C', 'F'] | None) – The order to flatten when given a nested sequence for
norm
, Defaults tocmap_order
.gradient (bool) – If
True
, for 1D slices, fills the area under the curve with a gradient. Has no effect for 2D slices.gradient_kw (dict | None) – Extra arguments to
gradient_fill()
.subplot_kw (dict | None) – Extra arguments to
matplotlib.pyplot.subplots()
: refer to thematplotlib
documentation for a list of all possible arguments.annotate_kw (dict | None) – Extra arguments to
erlab.plotting.annotations.label_subplot_properties()
. Only applied whenannotate
isTrue
.colorbar_kw (dict | None) – Extra arguments to
erlab.plotting.colors.proportional_colorbar()
.axes (Iterable[matplotlib.axes.Axes] | None) – A nested sequence of
matplotlib.axes.Axes
. If supplied, the returnedmatplotlib.figure.Figure
is inferred from the first axes.**values – Key-value pair of cut location and bin widths. See examples. Remaining arguments are passed onto
plot_array()
.
- Returns:
fig (
matplotlib.figure.Figure
)axes (
array-like
ofmatplotlib.axes.Axes
)
- Return type:
tuple[matplotlib.figure.Figure, Iterable[matplotlib.axes.Axes]]
Examples
# Two maps: map1, map2 # Create a figure with a 3 by 2 grid. fig, axes = plot_slices([map1, map2], eV=[0, -0.1, -0.2], eV_width=0.05)