backpack.figmanip
Useful functions for everyday use —> Matplotlib figures
- brixs.backpack.figmanip.get_available_colors()
Returns matplotlib available colors.
- brixs.backpack.figmanip.reset_cycler()
Resets the linestyle cycle
- brixs.backpack.figmanip.set_cycler(n='tab10', ls=1)
Set color cycles for matplotlib.
- Parameters:
n (int, str, list, optional) – name of a matplotlib sequential colormap. If n is a number, n colors will be selected in order from the listing: ‘k’, ‘r’, ‘g’, ‘b’, ‘c’, ‘m’, ‘y’, tab20b, tab20c. The maximum value is then 47 colors. n can also be a list of colors by name or by (rgb alpha). Default is ‘tab10’. Use None to use the default.
ls (int, list, optional) – number of different linestyles to iterate from (from 1 to 4) or a list of linestyles by name. Default is 1.
- Returns:
None
- brixs.backpack.figmanip.get_available_colormaps()
returns a list of available colormaps
- brixs.backpack.figmanip.set_cycler_from_colormap(name, n)
Sets a color cycler based on a colormap.
- Parameters:
name (str) – name of the colormap
n (int) – number of colors to pick from the colormap.
- Returns:
None
- brixs.backpack.figmanip.get_colors_from_colormap(name, n)
Returns a list of colors from colormap.
- Parameters:
name (str) – name of the colormap
n (int) – number of colors to pick from the colormap.
- Returns:
None
- brixs.backpack.figmanip.set_window_position(*args)
Change position of a maptplotlib figure on screen.
Typically, (0, 0) is the top left corner of the display.
- Usage:
set_window_position((x, y)) set_window_position(x, y)
- Parameters:
*args – A tuple like (x, y) or two separate x, y values (in px). If None, the default value defined by
set_default_window_position()
will be used. If there is no defined default value, the window’s position will be preserved.
See also
- brixs.backpack.figmanip.get_window_position()
Return the position of a matplotlib position on the screen.
Typically, (0, 0) is the top left corner of your monitor.
- Returns:
Tuple with the x and y position.
See also
- brixs.backpack.figmanip.set_window_size(*args)
Change the size of the window of a matplotlib figure.
- Parameters:
*args – A tuple like (width, height) or two separate width, height values in px.
See also
- brixs.backpack.figmanip.get_window_size()
Returns the size of the window of a matplotlib figure.
- Returns:
Tuple with the width and height values in px.
See also
- brixs.backpack.figmanip.get_window_dpi()
returns figure dpi
- brixs.backpack.figmanip.maximize()
Maximize current fig.
- brixs.backpack.figmanip.figure(*args, **kwargs)
Create figure object. Wrapper for plt.figure().
Mouse click behavior:
- Right click:
x value is copied to the clipboard.
- Left click OR (y + Right click):
y value is copied to the clipboard.
- Middle click:
copies cursor position in terms of figure coordinates.
- Parameters:
*args – args and kwargs are passed to plt.figure().
**kwargs –
args and kwargs are passed to plt.figure().
Note
This function overwrites the behavior of figsize parameters. In plt.figure(figsize=(w, h)), w and h must be given in inches. However, this function gets w and h in cm.
- Returns:
figure object
- brixs.backpack.figmanip.set_onclick_save_defaults(format='svg', resolution=300, folder=None)
Set the default format for saving figures using Middle click press on a figure.
- Parameters:
format (string, optional) – ‘svg’ or ‘png’.
resolution (int, optional) – resolution for png images in dpi.
folder (string or pathlib.Path) – folderpath to save figures.
- Returns:
None
- brixs.backpack.figmanip.subplots(nrows, ncols, sharex=False, sharey=False, hspace=0.3, wspace=0.3, width_ratios=None, height_ratios=None, **fig_kw)
Create a figure and a set of subplots. Wrapper for plt.subplots().
- Parameters:
nrows (int) – Number of rows/columns of the subplot grid.
ncols (int) – Number of rows/columns of the subplot grid.
sharex (bool or str, optional) – Share the x or y axis between axes. Options are: True, False, ‘all’, ‘row’, ‘col’. Default is False
sharey (bool or str, optional) – Share the x or y axis between axes. Options are: True, False, ‘all’, ‘row’, ‘col’. Default is False
hspace (float, optional) – The amount of height/width reserved for space between subplots, expressed as a fraction of the average axis height/width. Default is 0.3.
wspace (float, optional) – The amount of height/width reserved for space between subplots, expressed as a fraction of the average axis height/width. Default is 0.3.
width_ratios (list, optional) – Defines the relative heights/widths of the ows/columns. Each row/column gets a relative height/width of ratios[i] / sum(ratios). If not given, all rows/columns will have the same width.
height_ratios (list, optional) – Defines the relative heights/widths of the ows/columns. Each row/column gets a relative height/width of ratios[i] / sum(ratios). If not given, all rows/columns will have the same width.
**fig_kw – All additional keyword arguments are passed to the plt.figure call
- Returns:
fig, axes
- brixs.backpack.figmanip.publication_font(size=9)
Set font options for publication quality font (cmr10).
Change this function accordingly to your preferences.
- Returns:
None
- brixs.backpack.figmanip.default_font(size=10)
Set font options to Matplotlib’s default.
- Returns:
None
- brixs.backpack.figmanip.rtext(x, s, yoffset=0, xoffset=0, ax=None, copy_color=False, **kwargs)
Create text at x coordinate on the right side of the last curve plotted
- Parameters:
x (number) – x coordinate for the text
s (str) – text string
xoffset (number, optional) – offset between the curve and the text.
yoffset (number, optional) – offset between the curve and the text.
ax (axes, optional) – axes to put the text on. If None, the current axes
None (will be used. Default is)
copy_color (bool, optional) – if True, the color of the text will match
False (the color of the curve. Default is)
**kwargs – kwargs are passed to plt.text()
- Returns:
matplotlib.text.Text
- brixs.backpack.figmanip.ltext(x, s, yoffset=0, xoffset=0, ax=None, copy_color=False, **kwargs)
Create text at x coordinate on the left side of the last curve plotted
- Parameters:
x (number) – x coordinate for the text
s (str) – text string
xoffset (number, optional) – offset between the curve and the text.
yoffset (number, optional) – offset between the curve and the text.
ax (axes, optional) – axes to put the text on. If None, the current axes
None (will be used. Default is)
copy_color (bool, optional) – if True, the color of the text will match
False (the color of the curve. Default is)
**kwargs – kwargs are passed to plt.text()
- Returns:
matplotlib.text.Text
- brixs.backpack.figmanip.note(s, loc='upper left', x=None, y=None, ax=None, **kwargs)
Write text to a pre-defined locations (auto-position text). Wrapper for plt.text().
Text is written in axes coord (i.e. text does not move with the data). The coordinate system of the Axes; (0, 0) is bottom left of the axes, and (1, 1) is top right of the axes.
- Parameters:
s (str) – text string
loc (str, optional) – location of the text. Supported locations are the
plt.legend() (same ones for) – ‘upper right’, ‘upper left’, ‘lower left’,
right' ('center)
'right'
left' ('center)
right'
center' ('lower)
:param : :param ‘upper center’: :param ‘center’. Note that ‘best’ is not a supported value.: :param default is ‘upper left’: :param x: x and y in axes coordinates (0, 1). Overwrites loc. :type x: number :param y: x and y in axes coordinates (0, 1). Overwrites loc. :type y: number :param ax: axes to put the text on. If None, the current axes :type ax: axes, optional :param will be used. Default is None: :param **kwargs: kwargs are passed to plt.text()
- Returns:
matplotlib.text.Text
- brixs.backpack.figmanip.label_axes(axes, loc='upper left')
put letters from (a) to (z) in axes.
- Parameters:
axes (axes object or list) – axes list
locs (str or list, optional) – The location of the text. If list, list must be same length as number of axes. Options are ‘center’, ‘upper left’, ‘upper right’, ‘lower left’, ‘lower right’, ‘upper center’, ‘lower center’, ‘center left’, ‘center right’. Default is ‘best’.
- Returns:
list with text object
- brixs.backpack.figmanip.remove_xlabel(ax)
remove xlabel from axes
- brixs.backpack.figmanip.remove_ylabel(ax)
remove ylabel from axes
- brixs.backpack.figmanip.merge_axes(ax1, ax2)
merge axes created via plt.subplots
- Parameters:
ax1 (axes object) – axes to be merged.
ax2 (axes object) – axes to be merged.
- Returns
merged axes object
- brixs.backpack.figmanip.add_lspace(value, ax=None)
add space to the left of an axes
- Parameters:
value (float) – value in figure units (0 - 1).
ax (axes) – axes to apply change. If None, current axes is selected. Default is None.
- Returns:
None
- brixs.backpack.figmanip.add_rspace(value, ax=None)
add space to the right of an axes
- Parameters:
value (float) – value in figure units (0 - 1).
ax (axes) – axes to apply change. If None, current axes is selected. Default is None.
- Returns:
None
- brixs.backpack.figmanip.add_tspace(value, ax=None)
add space to the top of an axes
- Parameters:
value (float) – value in figure units (0 - 1).
ax (axes) – axes to apply change. If None, current axes is selected. Default is None.
- Returns:
None
- brixs.backpack.figmanip.add_bspace(value, ax=None)
add space to the left of an axes
- Parameters:
value (float) – value in figure units (0 - 1).
ax (axes) – axes to apply change. If None, current axes is selected. Default is None.
- Returns:
None
- brixs.backpack.figmanip.xmove(value, ax=None)
move axes in the x direction
- Parameters:
value (float) – value in figure units (0 - 1).
ax (axes) – axes to apply change. If None, current axes is selected. Default is None.
- Returns:
None
- brixs.backpack.figmanip.ymove(value, ax=None)
move axes in the y direction
- Parameters:
value (float) – value in figure units (0 - 1).
ax (axes) – axes to apply change. If None, current axes is selected. Default is None.
- Returns:
None
Connect x axis. Cannot be used if the x-axis is already being shared with another Axes
Connect y axis. Cannot be used if the y-axis is already being shared with another Axes
- brixs.backpack.figmanip.get_xticks_showing(ax)
get x ticks that are sowing in a plot
- Parameters:
ax (matplotlib.axes.Axes) – The axes of the subplot to set ticks. If None, last ax will be used.
- Returns:
list
- brixs.backpack.figmanip.get_yticks_showing(ax)
get y ticks that are sowing in a plot :param ax: The axes of the subplot to set ticks. If None,
last ax will be used.
- Returns:
list
- brixs.backpack.figmanip.set_xticks(ax=None, start=None, stop=None, n_ticks=None, ticks_sep=None, ticks=None, labels=None, pad=None, n_minor_ticks=None, minor_ticks_sep=None, minor_ticks=None, fontproperties=None, **kwargs)
Set x ticks of a plot.
Major and minor ticks position, location, and direction changes are applied to all shared axis. However, ticklabel changes are only applyied to the selected axes. Ticklabel parameters: labelsize, labelcolor, labelfontfamily, labelbottom, labeltop, labelleft, labelright, labelrotation.
- Usage:
For setting up major ticks:
set_ticks(start, stop, n_ticks) set_ticks(start, stop, ticks_sep) set_ticks(start, n_ticks, ticks_sep) # stop is calculated set_ticks(n_ticks) # start and stop are kept from the plot set_ticks(ticks) # manually define each tick position set_ticks(ticks, labels) # manually define each tick position and label
For setting up minor ticks:
set_ticks(n_minor_ticks) # equally spaced minor ticks set_ticks(minor_ticks_sep) # equally spaced minor ticks set_ticks(minor_ticks) # manually define each minor tick position
- Parameters:
ax (matplotlib.axes, optional) – axes object to set ticks.
ticks* (*Minor)
start (Overwrites) – major ticks start/stop value. Use None to not keep current start/stop value. Default is None.
stop (number, optional) – major ticks start/stop value. Use None to not keep current start/stop value. Default is None.
n_ticks (int/number, optional) – Number of major ticks or separation between ticks. n_ticks overwrites ticks_sep. Default is None.
ticks_sep (int/number, optional) – Number of major ticks or separation between ticks. n_ticks overwrites ticks_sep. Default is None.
ticks (Iterable, optional) – Iterable with tick positions and labels.
labels (Iterable, optional) – Iterable with tick positions and labels.
start
stop
n_ticks
ticks_sep.
*Padding (plotting limits)
pad (number or tuple, optional) – padding between plot edge and closest tick in terms of ticks separation. If tuple, first value applies to the left edge and second value to the right edge. Typically, padding should be between a value between 0 and 1. If pad is None, plot limits remain unchanged. However, if pad is None and start/stop are defined, pad is set to (0, 0). Default is None.
ticks*
n_minor_ticks (ticks between two major ticks or minor tick separation.) – Number of minor
minor_ticks_sep (int/number, optional) – Number of minor
n_minor_ticks
None. (overwrites minor_ticks_sep. Default is)
minor_ticks (Iterable, optional) – Iterable with minor tick positions.
minor_ticks_sep. (Overwrites n_minor_ticks and)
*font*
fontproperties – Label ticks font. Use
matplotlib.font_manager.FontProperties
.parameters* (*ticks)
**kwargs – kwargs are passed to ax.tick_params() that sets tick parameters.
specified (If not)
ax.tick_params() (the following parameters are passed to)
Args –
- which (‘major’, ‘minor’, ‘both’, optional): The group of ticks to which the parameters
are applied. Default is ‘both’.
- direction (‘in’, ‘out’, ‘inout’, optional): Puts ticks inside the
Axes, outside the Axes, or both. Default is ‘in’
- bottom, top (bool, optional): Whether to draw the bottom/top ticks.
Default is True.
- Returns:
None
- brixs.backpack.figmanip.set_yticks(ax=None, start=None, stop=None, n_ticks=None, ticks_sep=None, ticks=None, labels=None, pad=None, n_minor_ticks=None, minor_ticks_sep=None, minor_ticks=None, fontproperties=None, **kwargs)
Set y ticks of a plot.
Major and minor ticks position, location, and direction changes are applied to all shared axis. However, ticklabel changes are only applyied to the selected axes. Ticklabel parameters: labelsize, labelcolor, labelfontfamily, labelbottom, labeltop, labelleft, labelright, labelrotation.
- Usage:
For setting up major ticks:
set_ticks(start, stop, n_ticks) set_ticks(start, stop, ticks_sep) set_ticks(start, n_ticks, ticks_sep) # stop is calculated set_ticks(n_ticks) # start and stop are kept from the plot set_ticks(ticks) # manually define each tick position set_ticks(ticks, labels) # manually define each tick position and label
For setting up minor ticks:
set_ticks(n_minor_ticks) # equally spaced minor ticks set_ticks(minor_ticks_sep) # equally spaced minor ticks set_ticks(minor_ticks) # manually define each minor tick position
- Parameters:
ax (matplotlib.axes, optional) – axes object to set ticks.
ticks* (*Minor)
start (Overwrites) – major ticks start/stop value. Use None to not keep current start/stop value. Default is None.
stop (number, optional) – major ticks start/stop value. Use None to not keep current start/stop value. Default is None.
n_ticks (int/number, optional) – Number of major ticks or separation between ticks. n_ticks overwrites ticks_sep. Default is None.
ticks_sep (int/number, optional) – Number of major ticks or separation between ticks. n_ticks overwrites ticks_sep. Default is None.
ticks (Iterable, optional) – Iterable with tick positions and labels.
labels (Iterable, optional) – Iterable with tick positions and labels.
start
stop
n_ticks
ticks_sep.
*Padding (plotting limits)
pad (number or tuple, optional) – padding between plot edge and closest tick in terms of ticks separation. If tuple, first value applies to the left edge and second value to the right edge. Typically, padding should be between a value between 0 and 1. If pad is None, plot limits remain unchanged. However, if pad is None and start/stop are defined, pad is set to (0, 0). Default is None.
ticks*
n_minor_ticks (ticks between two major ticks or minor tick separation.) – Number of minor
minor_ticks_sep (int/number, optional) – Number of minor
n_minor_ticks
None. (overwrites minor_ticks_sep. Default is)
minor_ticks (Iterable, optional) – Iterable with minor tick positions.
minor_ticks_sep. (Overwrites n_minor_ticks and)
*font*
fontproperties – Label ticks font. Use
matplotlib.font_manager.FontProperties
.parameters* (*ticks)
**kwargs – kwargs are passed to ax.tick_params() that sets tick parameters.
specified (If not)
ax.tick_params() (the following parameters are passed to)
Args –
- which (‘major’, ‘minor’, ‘both’, optional): The group of ticks to which the parameters
are applied. Default is ‘both’.
- direction (‘in’, ‘out’, ‘inout’, optional): Puts ticks inside the
Axes, outside the Axes, or both. Default is ‘in’
- left, right (bool, optional): Whether to draw the left/right ticks.
Default is True.
- Returns:
None
- brixs.backpack.figmanip.remove_xticklabels(ax)
remove x tick labels from axes
- Parameters:
ax (matplotlib.axes.Axes) – The axes of the subplot to set ticks. If None, last ax will be used.
- Returns:
None
- brixs.backpack.figmanip.remove_yticklabels(ax)
remove y tick labels from axes
- Parameters:
ax (matplotlib.axes.Axes) – The axes of the subplot to set ticks. If None, last ax will be used.
- Returns:
None
- brixs.backpack.figmanip.leg(*args, **kwargs)
Place a legend on the Axes. Wrapper for plt.legend()
- Parameters:
*args – args and kwargs passed to plt.legend()
**kwargs –
args and kwargs passed to plt.legend()
ax (axes, optional) – axes. If None, the current axes will be used. Default is None
coord (tuple) – (x, y) coordinates in data coordinates to place legend. (x, y) places the corner of the legend specified by loc at x, y. If None, this is ignored. Default is None. Overwrites bbox_to_anchor.
If not specified, the following parameters are passed to `ax.legend()`_:
- Parameters:
labelspacing (float, optional) – The vertical space between the legend entries, in font-size units. Default is 0.5
frameon (bool, optional) – Whether the legend should be drawn on a patch (frame). Default is False
- Returns:
legend object
- brixs.backpack.figmanip.rectangle(xlim, ylim, ax=None, **kwargs)
plot a rectangle. Wrapper for plt.Rectangle().
- Parameters:
xlim (tuple) – xmin, xmax, ymin, ymax coordinates of the rectangle
ylim (tuple) – xmin, xmax, ymin, ymax coordinates of the rectangle
coordinates. (in data)
ax (axes, optional) – axes. If None, the current axes will be used. Default is None
**kwargs – kwargs are passed to plt.Rectangle()
If not specified, the following parameters are passed to ax.tick_params():
- Parameters:
linewidth (number, optional) – Set the patch linewidth in points. Default is 0.6.
facecolor (color or None, optional) – Set the patch face color. Default is ‘none’
edgecolor (color or None, optional) – Set the patch edge color. Default is gray.
alpha (number, optional) – Set the alpha value used for blending - not supported on all backends. Default is 0.5
zorder (number, optional) – Set the zorder for the artist. Artists with lower zorder values are drawn first. Default is such that rectangle is ploted on top of all other curves.
- Returns:
rectangle object
- brixs.backpack.figmanip.zoom(start, stop, ax=None, ymargin=5)
Zoom up portion of current figure from start to stop.
- Parameters:
start (float) – initial and final x value.
stop (float) – initial and final x value.
ax (axes, optional) – axes to apply zoom. If None, the current axes
None (will be used. Default is)
ymargin (number, optional) – margin value between data and the edges of plot in percentage of the y data range.
- brixs.backpack.figmanip.inset(xlim, ylim, ax=None, xticks_kwargs=None, yticks_kwargs=None, rect=True, rect_xlim=None, rect_ylim=None, ax2putinset=None, **kwargs)
add inset to axes. Inset will have all curves from axes
- Parameters:
xlim (tuple) – (xi, xf), (yi, yf) coordinates of inset in data coordinates.
ylim (tuple) – (xi, xf), (yi, yf) coordinates of inset in data coordinates.
ax (axes, optional) – axes to get inset data from. If None, the current axes will be used. Default is None
xticks_kwargs (dictionary, optional) – kwargs for set_xticks() and set_yticks() functions to set the inset ticks and plot limits. Default is None.
yticks_kwargs (dictionary, optional) – kwargs for set_xticks() and set_yticks() functions to set the inset ticks and plot limits. Default is None.
rect (bool, optional) – if True, rectangle will be draw in the main axes at the plotting limits defined by xticks_kwargs and yticks_kwargs.
rect_xlim (tuple, optional) – if defines the rectangle edges. overwrites limits defined by xticks_kwargs, yticks_kwargs.
rect_ylim (tuple, optional) – if defines the rectangle edges. overwrites limits defined by xticks_kwargs, yticks_kwargs.
ax2putinset (axex, optional) – axes to put inset in. If None, the current axes will be used. Default is None
- Returns:
inset axes object
- brixs.backpack.figmanip.cm2pt(*tupl)
Convert values from cm to pt.
- Parameters:
*Args – single value or a tuple with values to convert
- Returns:
A tuple with values converted
- brixs.backpack.figmanip.cm2px(*tupl, dpi=None)
Convert values from cm to px.
- Parameters:
*Args – A tuple with values to convert
dpi (int) – pixel density or dots per inch.
- Returns:
A tuple with values converted
- brixs.backpack.figmanip.cm2inch(*tupl)
Convert values from cm to inches.
- Parameters:
*Args – single value or a tuple with values to convert
- Returns:
A tuple with values converted
- brixs.backpack.figmanip.mm2inch(*tupl)
Convert values from mm to inches.
- Parameters:
*Args – single value or a tuple with values to convert
- Returns:
A tuple with values converted
- brixs.backpack.figmanip.rgb2hex(rgb, max_rgb_value=1)
Return hex value.
- Parameters:
rgb (tuple) – rgb values ([255 255 255])
max_rgb_value (int, optional) – max rgb possible value. Tipically, 1 or 255.
- Returns:
hex value
- brixs.backpack.figmanip.hex2rgb(string, max_rgb_value=1)
Return rgb value.
- Parameters:
string (string) – hex value as string.
max_rgb_value (int, optional) – max rgb possible value. Tipically, 1 or 255.
- Returns:
rgb value (tuple)
- brixs.backpack.figmanip.axvlines(x, ymin=None, ymax=None, colors='black', linestyles='--', labels='', ax=None, **kwargs)
draw vertical lines. Wrapper for plt.vlines() and plt.axvline()
- Parameters:
x (float or array) – x coordinates where to plot the lines.
ymin (float, optional) – Respective beginning and end of each line. If None, infinite lines are plotted. Default is None.
ymax (float, optional) – Respective beginning and end of each line. If None, infinite lines are plotted. Default is None.
colors (color or list, optional) – line colors. Default is black
linestyles (string or list, optional) – linestyles. Default is ‘–‘.
labels (string or list, optional) – labels. Default is ‘’.
ax (axes, optional) – axes. If None, the current axes will be used. Default is None
**kwargs – kwargs are passed to plt.vlines() or plt.axvline()
- Returns:
list with Line2D objects.
- brixs.backpack.figmanip.axhlines(y, xmin=None, xmax=None, colors='black', linestyles='--', labels='', ax=None, **kwargs)
draw horizontal lines. Wrapper for plt.hlines() and plt.axhline()
- Parameters:
y (float or array) – y coordinates where to plot the lines.
xmin (float, optional) – Respective beginning and end of each line. If None, infinite lines are plotted. Default is None.
xmax (float, optional) – Respective beginning and end of each line. If None, infinite lines are plotted. Default is None.
colors (color or list, optional) – line colors. Default is black
linestyles (string or list, optional) – linestyles. Default is ‘–‘.
labels (string or list, optional) – labels. Default is ‘’.
ax (axes, optional) – axes. If None, the current axes will be used. Default is None
**kwargs – kwargs are passed to plt.hlines() or plt.axhline()
- Returns:
list with Line2D objects.
- brixs.backpack.figmanip.linear_gradient(colors, n, max_rgb_value=1)
Returns a list of colors forming linear gradients between colors.
- Parameters:
colors (tuple) – list with rgb values.
n (int, optional) – number of intermediate colors.
max_rgb_value (int, optional) – max rgb possible value. Tipically, 1 or 255.
- Returns:
list with rgb colors.
- brixs.backpack.figmanip.bezier_gradient(colors, n=100, max_rgb_value=1)
Returns a list of rgb colors forming a bezier gradient between colors.
- Parameters:
colors (tuple) – list with hex values.
n (int, optional) – number of intermediate colors.
max_rgb_value (int, optional) – max rgb possible value. Tipically, 1 or 255.
- Returns:
list with rgb colors.