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
FigureStylewhen omitted.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
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()
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:
-
SelfA new Figure; this one is left unchanged.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
CellOutOfRangeErrorWhen a cell covered is outside the Layout.
-
OccupiedCellErrorWhen a cell covered already holds a Plot.
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()
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.FigureThe 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
FilePathsuch as"chart.png"orPath("images/chart.svg"), ending with.png,.svgor.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)ERRORby default, refusing to overwrite an existing file, orREPLACEto overwrite it.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
UnsupportedImageFormatErrorWhen the path does not end with
.png,.svgor.pdf. -
RefusedSettingErrorWhen options are given for an SVG or PDF image.
-
FileExistsErrorWhen a file exists at the path and
on_conflictisERROR. -
FileAccessErrorWhen the file system refuses the file: missing folder, missing permission and so on.
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()