Skip to content
Projects

plasma_plots.styles

Named plot styles and a colour-blind-safe palette: plasma_plots.set_style.

Every Matplotlib plot of plasma-plots draws with the current style. "default" is the style the plots always had; the others change it:

  • "colorblind": the default with the Okabe–Ito palette ([PALETTE][PALETTE]) as the line colours, which stay distinct for the common forms of colour blindness.
  • "paper": small figures with small text for a journal column, in the same palette.
  • "dark": light lines and text on a dark background, for slides and dark docs pages.
  • "bare": no axes, ticks, labels or titles, a transparent background and the accent colour first, for header pictures and front pages (see plasma_plots.hero.from_array() for an animated SVG and plasma_plots.plotly_backend.bare() for a Plotly figure).

set_style() sets the style of every later plot; style() sets it for a with block only, where it also applies to plain Matplotlib figures created inside the block.

Examples

>>> import plasma_plots
>>> plasma_plots.set_style("colorblind")
>>> with plasma_plots.style("dark"):
... phi.plasma.plot.slice(t=-1, eta3=0).save("phi_dark.png")

Attributes

NameDescription
ACCENTThe accent colour of the max-models docs, the first line colour of the "bare" style.
DEFAULTNo description.
PALETTEThe Okabe–Ito palette: eight colours that colour-blind readers tell apart.
STYLESThe named styles: Matplotlib rcParams per name.

Functions

NameDescription
get_styleReturn the name of the current style.
set_styleSet the style of every later plot.
styleDraw with a style inside a with block.
style_paramsReturn the Matplotlib rcParams of a style.
to_darkRecolour a drawn Matplotlib figure for a dark page, in place.

ACCENTattributemodule attribute#

ACCENT = '#2a78d6'

The accent colour of the max-models docs, the first line colour of the "bare" style.

DEFAULTattributemodule attribute#

DEFAULT = {
  'figure.figsize': (8.0, 5.0),
  'figure.dpi': 110,
  'axes.grid': True,
  'grid.alpha': 0.3,
  'axes.titlesize': 'medium',
  'legend.frameon': False,
  'image.cmap': 'viridis'
}

PALETTEattributemodule attribute#

PALETTE = ('#0072B2', '#E69F00', '#009E73', '#D55E00', '#CC79A7', '#56B4E9', '#F0E442', '#000000')

The Okabe–Ito palette: eight colours that colour-blind readers tell apart.

STYLESattributemodule attribute#

STYLES = {
  'default': DEFAULT,
  'colorblind': _COLORBLIND,
  'paper': {
    : _COLORBLIND,
    'figure.figsize': (3.4, 2.4),
    'figure.dpi': 200,
    'font.size': 8,
    'axes.titlesize': 8,
    'axes.labelsize': 8,
    'xtick.labelsize': 7,
    'ytick.labelsize': 7,
    'legend.fontsize': 7,
    'lines.linewidth': 1.2,
    'savefig.dpi': 300
  },
  'dark': {
    : DEFAULT,
    'axes.prop_cycle': cycler(color=(*PALETTE[:7], _DARK_TEXT)),
    'figure.facecolor': _DARK_BACKGROUND,
    'axes.facecolor': _DARK_BACKGROUND,
    'savefig.facecolor': _DARK_BACKGROUND,
    'axes.edgecolor': '#8a8d96',
    'axes.labelcolor': _DARK_TEXT,
    'axes.titlecolor': _DARK_TEXT,
    'text.color': _DARK_TEXT,
    'xtick.color': _DARK_TEXT,
    'ytick.color': _DARK_TEXT,
    'grid.color': '#ffffff',
    'grid.alpha': 0.15,
    'legend.labelcolor': _DARK_TEXT
  },
  'bare': {
    : DEFAULT,
    'axes.prop_cycle': cycler(color=(ACCENT, *PALETTE[1:])),
    'axes.grid': False,
    'axes.spines.left': False,
    'axes.spines.right': False,
    'axes.spines.top': False,
    'axes.spines.bottom': False,
    'xtick.bottom': False,
    'xtick.labelbottom': False,
    'ytick.left': False,
    'ytick.labelleft': False,
    'axes.labelcolor': 'none',
    'axes.titlecolor': 'none',
    'figure.facecolor': 'none',
    'axes.facecolor': 'none',
    'savefig.transparent': True,
    'lines.linewidth': 2.0,
    'legend.frameon': False
  }
}

The named styles: Matplotlib rcParams per name.

get_stylefunction#

def get_style() -> str

Return the name of the current style.

Returns

str
One of the keys of STYLES.

set_stylefunction#

def set_style(name: str = 'default', **overrides) -> None

Set the style of every later plot.

Parameters

NameTypeDefaultDescription
name('default', 'colorblind', 'paper', 'dark', 'bare')"default"The style (see STYLES). Default: "default", the plots’ usual style.
**overrides{}Matplotlib rcParams to change on top of the style, with dots written as underscores, e.g. figure_dpi=150.

Raises

ValueError
If there is no style of that name, or an override is not a Matplotlib rcParams name.

Examples

>>> plasma_plots.set_style("colorblind")
>>> plasma_plots.set_style("paper", figure_figsize=(3.4, 3.0))

stylefunction#

def style(name: str, **overrides)

Draw with a style inside a with block.

Inside the block the style also applies to plain Matplotlib figures, e.g. a colorbar or an annotation added to a plot of plasma-plots.

Parameters

NameTypeDefaultDescription
name('default', 'colorblind', 'paper', 'dark', 'bare')"default"The style (see STYLES).
**overrides{}Matplotlib rcParams to change on top of the style, with dots written as underscores, e.g. figure_dpi=150.

Yields

dict
The rcParams in effect.

Raises

ValueError
If there is no style of that name, or an override is not a Matplotlib rcParams name.

Examples

>>> with plasma_plots.style("dark"):
... energy.plasma.plot.timeseries().save("energy_dark.png")

style_paramsfunction#

def style_params(name: str | None = None) -> dict

Return the Matplotlib rcParams of a style.

Parameters

NameTypeDefaultDescription
namestrNoneThe style. Default: the current one, with the overrides given to set_style() or style().

Returns

dict
The parameters, to pass to matplotlib.pyplot.rc_context.

Raises

ValueError
If there is no style of that name.

to_darkfunction#

def to_dark(fig, *, text: str = _DARK_TEXT, axes: str = '#8a8d96', background: str = _DARK_BACKGROUND)

Recolour a drawn Matplotlib figure for a dark page, in place.

For a dark variant of a figure that was drawn for a white page (e.g. x.dark.png next to x.png), without drawing it again: black and dark grey text, lines, markers, edges and hatches become text, the spines and ticks axes, white becomes background, and the figure and axes backgrounds become transparent. Colormapped artists (images, meshes, scatters coloured by a value) and other colours stay as they are. Save it with transparent=True to show the page behind it.

Parameters

NameTypeDefaultDescription
figmatplotlib.figure.FigurerequiredThe figure; it is changed.
textstr_DARK_TEXTThe colour of text and of black lines. Default: the "dark" style’s text colour.
axesstr'#8a8d96'The colour of spines and ticks. Default: a mid grey.
backgroundstr_DARK_BACKGROUNDThe colour for white (e.g. white markers or masks). Default: the "dark" style’s background.

Returns

matplotlib.figure.Figure
The same figure.

Examples

>>> result = energy.plasma.plot.timeseries()
>>> result.fig.savefig("energy.png")
>>> to_dark(result.fig).savefig("energy.dark.png", transparent=True)