Plotnine (compat.plotnine
)#
To use Plot
:
In [1]: from pathlib import Path
...:
...: import xarray as xr
...: import plotnine as p9
...:
...: from genno import Computer, Quantity
...: from genno.compat.plotnine import Plot
...:
Create a subclass that overrides
Plot.generate()
,Plot.basename
, and optionallyPlot.inputs
.In [2]: class DemoPlot(Plot): ...: basename = "plotnine-demo" ...: suffix = ".svg" ...: ...: def generate(self, x, y): ...: data = x.merge(y, on="t") ...: return ( ...: p9.ggplot(data, p9.aes(x="x", y="y")) ...: + p9.geom_line(color="red") ...: + p9.geom_point(color="blue") ...: ) ...:
add()
the class to aComputer
directly. ThePlot.add_tasks()
method handles connecting thePlot.inputs
toPlot.save()
:# Set up a Computer, including the output path and some data In [3]: c = Computer(output_dir=Path(".")) In [4]: t = {"t": [-1, 0, 1]} In [5]: c.add("x:t", Quantity([1.0, 2, 3], coords=t, name="x")) Out[5]: <x:t> In [6]: c.add("y:t", Quantity([1.0, 4, 9], coords=t, name="y")) Out[6]: <y:t> # Add the plot to the Computer In [7]: c.add("plot", DemoPlot, "x:t", "y:t") Out[7]: 'plot' # Show the task that was added In [8]: c.graph["plot"] Out[8]: (<bound method Plot.save of <__main__.DemoPlot object at 0x7f69c0147df0>>, 'config', 'x:t', 'y:t')
get()
the node. The result is the path the the saved plot(s).In [9]: c.get("plot") Out[9]: PosixPath('plotnine-demo.svg')
- class genno.compat.plotnine.Plot[source]#
Class for plotting using
plotnine
.- classmethod add_tasks(c: Computer, key: Key | str, *inputs, strict: bool = False) Key | str [source]#
Add a task to c to generate and save the Plot.
Analogous to
Operator.add_tasks()
.
- basename = ''#
File name base for saving the plot.
- abstract generate(*args, **kwargs)[source]#
Generate and return the plot.
A subclass of Plot must implement this method.
- Parameters:
args (
collections.abc.Sequence
ofpandas.DataFrame
orother
) –One argument is given corresponding to each of the
inputs
.Because
plotnine
operates on pandas data structures,save()
automatically converts anyQuantity
inputs topandas.DataFrame
before they are passed togenerate()
.
- inputs: Sequence[Hashable] = []#
Keys
referring toQuantities
or other inputs accepted bygenerate()
.
- classmethod make_task(*inputs)[source]#
Return a task
tuple
to add to a Computer.Deprecated since version 1.18.0: Use
add_tasks()
instead.- Parameters:
*inputs (.Key or
str
orcollections.abc.Hashable
, optional) – If provided, overrides theinputs
property of the class.- Returns:
The first, callable element of the task is
save()
.The second element is
"config"
, to access the configuration of the Computer.The third and following elements are the inputs.
- Return type:
- path: Path | None = None#
Path for file output. If it is not set,
save()
will populate it with a value constructed fromconfig["output_dir"]
,basename
, andsuffix
. The implementation ofgenerate()
in a Plot sub-class may assign any other value, for instance one constructed at runtime from theinputs
.
- save(config, *args, **kwargs) Path | None [source]#
Prepare data, call
generate()
, and save to file.This method is used as the callable in the task generated by
add_tasks()
.Added in version 1.24.1: This method uses
disable_copy_on_write()
to work around has2k1/mizani#38. This may cause issues if other computations (for instance, of the inputs to the Plot) rely on Pandas’ copy-on-write behaviour being enabled.
- save_args: Dict[str, Any] = {'verbose': False}#
Keyword arguments for
plotnine.ggplot.save
.
- suffix = '.pdf'#
File extension; determines file format.