Figurex documentation

Basics plotting

class figurex.figure.Figure(title: str = '', layout: tuple | list = (1, 1), size: tuple = (6, 3), save: str = None, save_dpi: int = 250, save_format: str = None, transparent: bool = True, gridspec_kw: dict = {'hspace': 0.7, 'wspace': 0.3}, backend: str = '', show: bool = True, **kwargs)

Context manager for Figures. It creates the figure and axes for the panels. Cares about saving and showing the figure in the end. Provides axes as context.

Returns:

Provides the axis as context.

Return type:

matplotlib.axes.Axes

Examples

>>> with Figure() as ax:
...     ax.plot([1,2], [3,4])

This is equivalent to:

>>> with Figure():
...    with Panel() as ax:
...        ax.plot([5,6], [7,8])
static as_object(ax: Axes = None, save_format: str = 'png', tight: bool = True, facecolor: str = 'none', dpi: int = 300, transparent: bool = True) BytesIO

Saves a given figure as a BytesIO object. It can be later used as an input for fpdf2 images.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – Any axis of the figure to be saved, by default None (i.e., use latest axis)

  • save_format (str, optional) – Figure storage format, can by “png”, “svg”, “pdf”, by default “png”. Notes: svg format sometimes has issues with colors, use png instead.

  • tight (bool, optional) – Tight plotting without large padding, by default True

  • facecolor (str, optional) – Background color of the figure, by default “none”

  • dpi (int, optional) – Dots per inch of the figure, by default 300

  • transparent (bool, optional) – Transparent background image, by default True

Returns:

An object that could be later used in PDFs, for instance.

Return type:

io.BytesIO

Examples

>>> with Figure() as ax:
...     ax.plot([1,2], [3,4])
... my_figure = Figure.as_object()
create_panel_grid()

Creates a regular grid of axes.

Returns:

A flattened array of axes.

Return type:

List

Examples

>>> with Figure(layout=(1,2)):
...     with Panel() as ax:
...         ax.plot([1,2],[3,4])
...     with Panel() as ax:
...         ax.plot([5,6],[7,8])
create_panel_mosaic()

Creates a mosaic layout from self.layout.

Returns:

The axes are sorted.

Return type:

List of axes.

Examples

>>> with Figure(layout=[[1,2,2],[1,3,3]]):
...     with Panel() as ax:
...         ax.plot([1,2],[3,4])
...     with Panel() as ax:
...         ax.plot([5,6],[7,8])
...     with Panel() as ax:
...         ax.plot([9,0],[1,2])
static get() Figure

Get the current matplotlib figure instance.

Returns:

The current figure instance.

Return type:

matplotlib.figure.Figure

Examples

>>> fig = Figure.get()
static get_axes() ndarray

Get list of axes from the current figure.

Returns:

A list of axes objects

Return type:

numpy.ndarray of matplotlib.axes.Axes

Examples

>>> for ax in Figure.get_axes():
...    ax.set_ylim(0,1)
static get_next_axis() Axes

Get the next ax instance from the current figure

Returns:

Matplotlib axis object which can be used for plotting

Return type:

matplotlib.axes.Axes

Examples

>>> ax.plot([1,2],[3,4])
... ax = Figure.get_next_axis()
... ax.plot([5,6],[7,8])
static set_backend(backend: str)

Sets the current backend, e.g. “agg” to plot into a PDF file. If no interactive output is needed, use together with: Figure(show=False)

Parameters:

backend (str) – Matplotlib backend to be used, e.g.: “agg”. Use “default” to switch back to the original backend.

Examples

>>> Figure.set_backend("agg")
... with Figure(show=False) as ax:
...     ax.plot([1,2],[3,4])
class figurex.figure.Panel(title: str = '', spines: str = None, grid: str = None, x_range: tuple = None, y_range: tuple = None, extent: list | tuple = None, x_major_ticks: str = None, x_minor_ticks: str = None, x_major_fmt: str = None, x_minor_fmt: str = None, colorbar=None)

Context manager for figure panels. Inherited by class Figure. It looks for an active axis and applies basic settings.

Returns:

Provides the axis as context.

Return type:

matplotlib.axes.Axes

Examples

>>> with Figure(layout=(1,2)):
...    with Panel() as ax:
...        ax.plot([1,2], [3,4])
...    with Panel() as ax:
...        ax.plot([5,6], [7,8])
static add_circle(ax: Axes, x=0.0, y=0.0, radius: float = 1.0, fc: str = 'none', color: str = 'black', ls: str = '-')

Draws a circle on the plot.

Parameters:
  • ax (matplotlib.axes.Axes) – Axis object to draw on.

  • x (x axis data type, optional) – Center x location of the circle.

  • y (y axis data type, optional) – Center y location of the circle.

  • radius (float, optional) – Radius of the circle. Defaults to 1.

  • fc (str, optional) – Face color of the circle. Defaults to “none”.

  • color (str, optional) – Border color of the circle. Defaults to “black”.

  • ls (str, optional) – Line style of the circle. Defaults to “-“.

Returns:

Adds a circle patch to ax

Return type:

ax

Examples

>>> with Figure() as ax:
...    add_circle(ax, 1.5, 2.5, 1.0, "w", "k", "--")
static add_colorbar(ax: Axes = None, points=None, label: str = None, ticks: list | ndarray = None, ticklabels: list | ndarray = None, ticks_kw: dict = {}, bar_kw: dict = {'aspect': 20, 'extend': 'both', 'pad': 0.02, 'shrink': 0.6}, label_kw: dict = {'labelpad': 20, 'rotation': 270})

Adds a color bar to the current panel.

Parameters:
  • ax (matplotlib.axes.Axes, optional) – Axis to draw on., by default None

  • points – Line2D object to be described, by default None

  • optional – Line2D object to be described, by default None

  • label (str, optional) – Axis label of the colorbar, by default None

  • ticks (list, optional) – Ticks of the colorbar, by default None

  • ticklabels (list, optional) – Tick labels, by default None

  • ticks_kw (dict, optional) – Other tick keywords, by default dict()

  • bar_kw (dict, optional) – Bar keywords, by default dict(shrink=0.6, pad=0.02, aspect=20, extend=”both”)

  • label_kw (dict, optional) – Label keywords, by default dict(rotation=270, labelpad=20)

Examples

>>> points = ax.scatter([1,2], [3,4], c=[0.2,0.8], cmap="Spectral")
... add_colorbar(points, ax, label="Energy (eV)")
static set_grid(ax: Axes, dimension: str = 'xy', color: str = 'k', alpha: float = 1, **kwargs)

Set a grid for the axis.

Parameters:
  • ax (matplotlib.axes.Axes) – Axis to draw on.

  • dimension (str, optional) – Dimension, e.g. x, y, or xy===both, by default “xy”

  • color (str, optional) – Color of the grid lines, by default “k”

  • alpha (float, optional) – Opacity of the lines, by default 1

Examples

>>> set_grid(ax, "xy")
static set_range(ax: Axes, extent: list | tuple = None, x_range: tuple = (None, None, None), y_range: tuple = (None, None, None))

Applies x and y axis ranges or bounding box to axis.

Parameters:
  • ax (matplotlib.axes.Axes) – Axis to change.

  • extent (list | tuple, optional) – Bounding box [x0,x1,y0,y1], by default None

  • x_range (tuple, optional) – tuple of either (x_min, x_max) or (x_min, x_max, x_steps), by default (None, None, None)

  • y_range (tuple, optional) – tuple of either (y_min, y_max) or (y_min, y_max, y_steps), by default (None, None, None)

Examples

>>> set_range(ax, x_range=(0, 1, 0.1), y_range=(10, 20, 1))
static set_spines(ax: Axes, spines: str = 'lb')

Show or hide axis spines

Parameters:
  • ax (matplotlib.axes.Axes) – Axis to draw on.

  • spines (str, optional) – Location of visible spines, a combination of letters “lrtb” (left right, top, bottom), by default “lb”

Examples

>>> set_spines(ax, "lb")
static set_time_ticks(ax: Axes = None, how: str = None, which: str = 'major', fmt: str = None)

Format time axis.

axmatplotlib.axes.Axes, optional

Axis to change, by default None

howstr, optional

Label every minutes, hours, days, weeks, months, or years, by default None Can have a number at the start to indicate intervals

whichstr, optional

Label major or minor ticks, by default “major”

fmtstr, optional

Format the date, e.g. “%b %d, %H_%M”, by default None

>>> set_time_ticks(ax, "2weeks", "major", "%d

%b)

static set_title(ax: Axes, text: str = '', fontsize: int = 10)

Set title of the panel, e.g.: “a) Correlation”

Parameters:
  • ax (matplotlib.axes.Axes) – Axis to draw on.

  • text (str, optional) – text for the title, by default “”

  • fontsize (int, optional) – font size, by default 10

Examples

>>> set_title(ax, "a) Correlation")

Geographical mapping

class figurex.cartopy.Cartopy(*args, projection=None, tiles: str = None, tiles_cache: bool = False, zoom: int = 10, features: list | None = None, features_kw: dict | None = {}, grid=True, **kwargs)

Context manager for figure panels with geographic capabilities. Like class Panel, but with more features and dependencies.

Examples

>>> from figurex.cartopy import Cartopy
... import cartopy.crs as ccrs
... crs = ccrs.EuroPP()
...
... with Figure():
...     with Cartopy(
...         extent=[5,15,46,55],
...         projection=crs,
...         tiles="OSM",
...         zoom=6,
...         features=["rivers", "ocean", "countries"]
...     ) as ax:
...         ax.scatter(10,51, transform=ccrs.PlateCarree())
add_basemap(ax=None, extent=None, tiles='OSM', zoom=12, cache=False)

Add a basemap to a plot.

Examples

>>> with Figure() as ax:
...     ax.plot(x, y)
...     add_basemap(ax, extent=[9, 11, 49, 51], tiles='OSM', zoom=12)
set_features()

Applies map features

set_grid()

Applies x and y axis ranges or bounding box to axis.

class figurex.basemap.Basemap(*args, map_type: str = 'normal', projection: str = 'merc', tiles: str = None, tiles_cache: bool = False, zoom: int = 1, center=None, resolution: str = 'i', anchor='NW', suppress_ticks=False, grid_kw=None, features: list | None = None, features_kw: dict | None = {}, epsg=None, **kwargs)

Context manager for figure panels with geographic capabilities. Like class Panel, but with more features and dependencies.

Examples

>>> from figurex.basemap import Basemap
... with Figure():
...     with Basemap(
...     extent=(5,15,46,55),
...     x_range=(5,15,3),
...     y_range=(47,56,2),
...     features = ["ocean", "countries", "rivers"],
...     tiles="relief"
... ) as Map:
...     x,y = Map(12.385, 51.331)
...     Map.scatter(x, y,  marker="x", zorder=20,)
set_features()

Applies map features

set_grid()

Applies x and y axis ranges or bounding box to axis.