Styles and figure sizes

Activate a journal’s look, then make figures at exactly the size the journal prints them. Supported journals describes each journal and Quickstart walks through a first figure.

Activating a style

plotastro.set_style(journal='mnras', *, usetex=False, grid=None, palette=None, cmap=None, **rc_overrides)[source]

Activate the plotting style for a journal.

Parameters:
  • journal (str) – One of "mnras", "rasti", "aanda" (aliases "a&a", "aa"), "apj" (aliases "apjl", "aastex"), "oja", "prd" (aliases "prl", "revtex"), "jcap", "natastro" (alias "nature"), "euclid" (alias "ec"; Euclid Consortium papers in the look of the ECEB’s niceplots), "thesis" or "beamer".

  • usetex (bool, optional) – If True, render all text with a real LaTeX installation using fonts matching the journal (newtx Times for the serif journals, Helvetica for Nature Astronomy, Computer Modern Sans for Euclid). Default False (portable mathtext).

  • grid (bool, optional) – Override the style’s grid setting (the styles default to a subtle grid, except "euclid"; pass grid=False for a clean journal look).

  • palette (str or sequence of colours, optional) – Replace the colour cycle. A name — "default", "okabe_ito", "petroff8", "petroff10", "tol_vibrant", or one of the Euclid niceplots schemes "categorical1", "categorical2", "categorical3", "sequential", "diverging" (see euclid_colors()); "cmr.<name>" for 8 colours sampled from a CMasher colormap (see cmasher_colors()) — or any list or dict of colours.

  • cmap (str, optional) – Default colormap for imshow, pcolormesh, scatter etc. (the styles use viridis): any matplotlib colormap name, or "cmr.<name>" for a CMasher colormap (see cmasher_cmap()). CMasher is optional: it is only imported when a "cmr." name is used here or in palette.

  • **rc_overrides – Any extra rcParams, e.g. set_style("mnras", **{"font.size": 10}).

Examples

>>> pa.set_style("aanda")
>>> pa.set_style("mnras", usetex=True, grid=False)
>>> pa.set_style("euclid", palette="categorical3")
>>> pa.set_style("mnras", palette="cmr.rainforest", cmap="cmr.ocean")
plotastro.use(journal='mnras', *, usetex=False, grid=None, palette=None, cmap=None, **rc_overrides)

Alias of set_style(), for those who prefer pa.use("mnras"), after which everything is plain matplotlib.

plotastro.current_journal()[source]

Name of the journal activated by the last set_style() call.

Returns:

  • str (a key of JOURNALS, e.g. "mnras" (also the value)

  • before any set_style() call). Aliases are resolved, so after

  • set_style("a&a") this is "aanda".

Journal names

journal= takes a key or an alias, in set_style(), figsize(), subplots() and authorlist(). Case, spaces and hyphens are ignored, so "A&A" and "Open Journal" work too.

key

journal

aliases

mnras

Monthly Notices of the RAS

mnras_full (from the original API)

rasti

RAS Techniques and Instruments

aanda

Astronomy & Astrophysics

a&a, aa, astronomy&astrophysics

apj

The Astrophysical Journal (AASTeX)

apjl, aj, aas, aastex

oja

The Open Journal of Astrophysics

openjournal, theoj, openjournalofastrophysics

prd

Physical Review D (REVTeX 4.2)

prl, aps, revtex

jcap

J. of Cosmology and Astroparticle Physics

natastro

Nature Astronomy

nature, natureastronomy, natastron

euclid

Euclid Consortium (A&A, niceplots look)

ec, euclidconsortium, niceplots

thesis

A4 thesis text width (MNRAS look)

beamer

Beamer slide text width (MNRAS look)

With plain matplotlib

Importing plotastro registers its styles with matplotlib, so afterwards they work in any code, without the helpers:

import matplotlib.pyplot as plt
import plotastro                  # registers the styles

plt.style.use("mnras")

The registered names are the style files: mnras, rasti, aanda, apj, oja, prd, jcap, natastro and euclid. Each sets the journal’s fonts, ticks and colour cycle, and a one-column default figure size. Aliases, the thesis and beamer presets, and the extra options (usetex, grid, palette, cmap) are only available through set_style().

Figure sizes

plotastro.figsize(width='column', *, journal=None, fraction=1.0, nrows=1, ncols=1, aspect=None, height=None)[source]

Figure dimensions (inches) that match the journal’s text layout, so the figure is never rescaled (and its fonts shrunk) by LaTeX.

Parameters:
  • width ({"column", "full"} or float) – "column" for a one-column figure, "full" for the full text width, or a number = a custom width in LaTeX points (get yours with \the\columnwidth in your .tex file).

  • journal (str, optional) – Journal to size for; defaults to the one from the last set_style() call.

  • fraction (float, optional) – Fraction of that width to occupy (e.g. 0.5 for half a column).

  • nrows (int, optional) – Subplot grid shape; the height scales so each panel keeps the requested aspect ratio.

  • ncols (int, optional) – Subplot grid shape; the height scales so each panel keeps the requested aspect ratio.

  • aspect (float, optional) – Height/width ratio of one panel. Default: the journal’s own — the golden ratio (0.618) everywhere except "euclid", which follows niceplots’ 4:3. Use aspect=1 for square panels.

  • height (float, optional) – Explicit figure height in inches (overrides aspect).

Returns:

(width_in, height_in)

Return type:

tuple of float

plotastro.subplots(nrows=1, ncols=1, *, width='column', journal=None, fraction=1.0, aspect=None, height=None, **kwargs)[source]

plt.subplots with the figure size computed by figsize().

Parameters:
  • nrows (int, optional) – Subplot grid shape.

  • ncols (int, optional) – Subplot grid shape.

  • width – Passed to figsize(), together with nrows and ncols, so each panel keeps the requested aspect ratio.

  • journal – Passed to figsize(), together with nrows and ncols, so each panel keeps the requested aspect ratio.

  • fraction – Passed to figsize(), together with nrows and ncols, so each panel keeps the requested aspect ratio.

  • aspect – Passed to figsize(), together with nrows and ncols, so each panel keeps the requested aspect ratio.

  • height – Passed to figsize(), together with nrows and ncols, so each panel keeps the requested aspect ratio.

  • **kwargs – Forwarded to plt.subplots (e.g. sharex=True). Passing figsize= yourself overrides the computed size.

Returns:

  • fig (Figure)

  • ax (Axes or array of Axes) – As returned by plt.subplots.

Examples

>>> fig, ax = pa.subplots()                          # one-column figure
>>> fig, axes = pa.subplots(2, 2, width="full")      # full-width 2x2 grid
>>> fig, ax = pa.subplots(aspect=1)                  # square panel
plotastro.savefig(name, fig=None, formats=('pdf',), **kwargs)[source]

Save a figure under one or more formats at once.

Parameters:
  • name (str or Path) – Output path without extension (a known extension is stripped).

  • fig (Figure, optional) – Defaults to the current figure.

  • formats (sequence of str, optional) – e.g. ("pdf", "png") to get both a vector file for the paper and a raster preview.

  • **kwargs – Forwarded to fig.savefig (e.g. dpi=600).

Returns:

list of str

Return type:

the files written.

Journal data

plotastro.JOURNALS: dict

Layout of every journal and preset, keyed by journal name (see Journal names). Each entry has:

"column", "full"

One-column and full text width, in LaTeX points (1 pt = 1/72.27 in). They are equal for single-column layouts (jcap, thesis, beamer).

"style"

The .mplstyle file the journal uses, in STYLE_DIR.

"tex"

The LaTeX preamble set_style(usetex=True) uses, matching the journal’s fonts.

"name"

The journal’s full name.

"aspect" (optional)

Default height/width ratio, if not GOLDEN. Only euclid has one (0.75).

>>> pa.JOURNALS["mnras"]["column"], pa.JOURNALS["mnras"]["full"]
(240.0, 504.0)
plotastro.GOLDEN: float

The golden ratio, (√5 − 1)/2 ≈ 0.618: the default height/width ratio of a panel in figsize() (except for euclid, which uses 4:3).

plotastro.STYLE_DIR: pathlib.Path

The folder holding the bundled .mplstyle files. To use the styles without importing plotastro, copy them into matplotlib’s style library, matplotlib.get_configdir()/stylelib/.

Legacy

plotastro.set_size(width='mnras', fraction=1, subplots=(1, 1), hight_ratio=1)[source]

Deprecated — use figsize() instead. Kept so old scripts run.

set_size('mnras') == figsize('column', journal='mnras') and set_size('mnras_full') == figsize('full', journal='mnras').

Parameters:
  • width (str or float) – A journal key (one-column width), "mnras_full" for the full MNRAS text width, or a width in LaTeX points.

  • fraction (float) – Fraction of that width to occupy.

  • subplots ((int, int)) – Subplot grid shape, (rows, columns).

  • hight_ratio (float) – Multiplies the golden-ratio height (spelled as in the original API).

Returns:

(width_in, height_in)

Return type:

tuple of float