anyplotlib.Figure#

class anyplotlib.Figure(**kwargs)[source]#

Bases: AnyWidget

Multi-panel interactive figure widget.

The top-level container for all plots and the only anywidget.AnyWidget subclass in anyplotlib. It owns all traitlets and acts as the Python ↔ JavaScript bridge via the figure_esm.js canvas renderer.

Create via subplots() (recommended) or directly:

fig = Figure(2, 2, figsize=(800, 600))
ax  = fig.add_subplot((0, 0))
v2d = ax.imshow(data)
Parameters:
  • nrows (int, optional) – Grid dimensions. Default 1 row, 1 column.

  • ncols (int, optional) – Grid dimensions. Default 1 row, 1 column.

  • figsize ((width, height), optional) – Figure size in CSS pixels. Default (640, 480).

  • width_ratios (list of float, optional) – Relative column widths. Length must equal ncols.

  • height_ratios (list of float, optional) – Relative row heights. Length must equal nrows.

  • sharex (bool, optional) – Link pan/zoom across all panels on the respective axis. Default False (independent pan/zoom per panel).

  • sharey (bool, optional) – Link pan/zoom across all panels on the respective axis. Default False (independent pan/zoom per panel).

See also

subplots

Recommended factory for creating Figure and Axes grid.

layout_json#

A trait for unicode strings.

event_json#

A trait for unicode strings.

fig_width#

An int trait.

fig_height#

An int trait.

display_stats#

A boolean (True, False) trait.

help_text#

A trait for unicode strings.

set_help(text)[source]#

Set (or clear) the figure-level help text shown in the ? badge.

Parameters:

text (str) – Help string displayed when the user clicks the ? badge. Pass an empty string (or "" ) to remove the badge entirely. Newlines (\n) are respected in the card.

Return type:

None

Examples

>>> fig.set_help("Drag peak: move μ/A\nPress f: least-squares fit")
>>> fig.set_help("")   # hide the badge
add_subplot(spec)[source]#

Add a subplot cell and return its Axes.

Parameters:

spec (SubplotSpec or int or tuple of (row, col)) – Which grid cell(s) to occupy. A SubplotSpec is used directly (e.g. from GridSpec[r, c]). An int is converted via divmod(spec, ncols), matching matplotlib.Figure.add_subplot numbering. A (row, col) tuple selects a single cell.

Returns:

The subplot axes object. Call plotting methods like .imshow(), .plot(), .bar() to attach data.

Return type:

Axes

Raises:

TypeError – If spec is not a SubplotSpec, int, or tuple.

Examples

>>> fig = Figure(2, 2)
>>> ax1 = fig.add_subplot(0)       # top-left (via numbering)
>>> ax2 = fig.add_subplot((0, 1))  # top-right (via tuple)
add_inset(w_frac, h_frac, *, corner='top-right', title='')[source]#

Create and return a floating inset axes.

The inset overlays the figure at the specified corner. Call plot-factory methods on the returned InsetAxes to attach data:

inset = fig.add_inset(0.3, 0.25, corner="top-right", title="Zoom")
inset.imshow(data)    # returns Plot2D
inset.plot(profile)   # returns Plot1D
Parameters:
  • w_frac (float) – Width and height as fractions of the figure size (0–1).

  • h_frac (float) – Width and height as fractions of the figure size (0–1).

  • corner (str, optional) – Positioning corner: "top-right" (default), "top-left", "bottom-right", or "bottom-left".

  • title (str, optional) – Text displayed in the inset title bar.

Return type:

InsetAxes

get_axes()[source]#

Return a list of all Axes, sorted by grid position.

Returns:

Axes sorted by (row_start, col_start) to match typical left-to-right, top-to-bottom iteration order.

Return type:

list of Axes