Skip to content

Figure(layout, style=None) Source

Several Plots placed on a grid of cells, shown and saved as one image.

A Figure is immutable: place returns a new Figure. Each Plot keeps its own axes and its own PlotStyle, except its size: the Figure gives it the size of its cells. A cell without Plot stays blank.

Added in version 0.1.0

Parameters:

  • layout (Layout)

    The grid of cells.

  • style (FigureStyle | None, default: None)

    The title and the size of the Figure, the default FigureStyle when omitted.

Raises:

Examples:

import numdaplot as ndp

months = ndp.Array.from_list(["Jan", "Feb", "Mar", "Apr"])
sales = ndp.Array.from_list([120.0, 150.0, 90.0, 170.0])
visits = ndp.Array.from_list([900.0, 1100.0, 800.0, 1300.0])
shop = ndp.Table.from_arrays({"month": months, "sales": sales, "visits": visits})
sales_plot = ndp.Plot.bar(shop, ndp.BarChart(x="month", y=["sales"]))
visits_plot = ndp.Plot.line(shop, ndp.LineChart(x="month", y=["visits"]))
figure = ndp.Figure(ndp.Layout(rows=1, columns=2), ndp.FigureStyle(title="Shop"))
figure = figure.place(sales_plot, ndp.Cell(row=0, column=0))
figure = figure.place(visits_plot, ndp.Cell(row=0, column=1))
figure.show()

Plot drawn by the example

Methods:

place(plot, cell) Source

Return a new Figure with a Plot placed on a cell, and on the cells it spans.

Added in version 0.1.0

Parameters:

  • plot (Plot)

    The Plot placed, which keeps its PlotStyle but takes the size of its cells.

  • cell (Cell)

    Its first cell, from row 0 and column 0, and the number of cells it spans.

Returns:

  • Self

    A new Figure; this one is left unchanged.

Raises:

Examples:

import numdaplot as ndp

prices = ndp.Array.from_list([12.0, 15.5, 14.0, 17.0])
plot = ndp.Plot.line(prices, style=ndp.PlotStyle(title="Prices"))
figure = ndp.Figure(ndp.Layout(rows=1, columns=2))
placed_figure = figure.place(plot, ndp.Cell(row=0, column=1))
placed_figure.show()

Plot drawn by the example

to_matplotlib() Source

Return a new matplotlib figure of the Figure, to change it with matplotlib.

Each Plot is drawn on a matplotlib subfigure. Changing the figure never changes the Figure.

Added in version 0.1.0

Returns:

  • matplotlib.figure.Figure

    The figure, with one subfigure per Plot.

Examples:

import numdaplot as ndp

plot = ndp.Plot.line(ndp.Array.from_list([12.0, 15.5]))
figure = ndp.Figure(ndp.Layout(rows=2, columns=2)).place(plot, ndp.Cell(row=1, column=1))
matplotlib_figure = figure.to_matplotlib()
len(matplotlib_figure.subfigs), matplotlib_figure.get_size_inches().tolist()
# (1, [9.6, 7.2])

save(path, options=None, on_conflict=Conflict.ERROR) Source

Save the Figure as an image, in the format named by the extension of the path.

.png gives an image made of pixels, .svg and .pdf images made of lines and texts that stay sharp at any zoom.

Added in version 0.1.0

Parameters:

  • path (FilePath)

    The path of the image, a FilePath such as "chart.png" or Path("images/chart.svg"), ending with .png, .svg or .pdf.

  • options (ImageOptions | None, default: None)

    The resolution of a PNG image, 100 dots per inch when omitted. SVG and PDF images take none.

  • on_conflict (Conflict, default: Conflict.ERROR)

    ERROR by default, refusing to overwrite an existing file, or REPLACE to overwrite it.

Raises:

Examples:

import tempfile
from pathlib import Path
import numdaplot as ndp

path = Path(tempfile.mkdtemp()) / "shop.pdf"
plot = ndp.Plot.line(ndp.Array.from_list([12.0, 15.5]))
figure = ndp.Figure(ndp.Layout(rows=1, columns=2)).place(plot, ndp.Cell(row=0, column=0))
figure.save(path)
path.read_bytes()[:4]
# b'%PDF'

show() Source

Show the Figure: as an image in a Jupyter notebook, in a matplotlib window anywhere else.

In a script, the window stays open until it is closed. When no window can be opened, because Python has no tkinter or runs on a server, nothing is shown and a DisplayWarning suggests saving the Figure instead.

Added in version 0.1.0

Examples:

import numdaplot as ndp

prices = ndp.Array.from_list([12.0, 15.5, 14.0, 17.0])
line_plot = ndp.Plot.line(prices, style=ndp.PlotStyle(title="Line"))
bar_plot = ndp.Plot.bar(prices, style=ndp.PlotStyle(title="Bars"))
figure = ndp.Figure(ndp.Layout(rows=1, columns=2), ndp.FigureStyle(title="Prices"))
figure = figure.place(line_plot, ndp.Cell(row=0, column=0))
figure = figure.place(bar_plot, ndp.Cell(row=0, column=1))
figure.show()

Plot drawn by the example