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") ...: ) ...:
Call
make_task()
to get a task tuple suitable for adding to aComputer
:# 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(xr.DataArray([1.0, 2, 3], coords=t), name="x")) Out[5]: <x:t> In [6]: c.add("y:t", Quantity(xr.DataArray([1.0, 4, 9], coords=t), name="y")) Out[6]: <y:t> # Add the plot to the Computer In [7]: c.add("plot", DemoPlot.make_task("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 0x7f1f0f4085b0>>, '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
.- basename = ''#
Filename base for saving the plot.
- abstract generate(*args, **kwargs)[source]#
Generate and return the plot.
Must be implemented by subclasses.
- Parameters:
args (sequence of
pandas.DataFrame
) – Becauseplotnine
operates on pandas data structures,save()
automatically convertsQuantity
before being provided togenerate()
.
- inputs: Sequence[Hashable] = []#
Keys for quantities needed by
generate()
.
- classmethod make_task(*inputs)[source]#
Return a task
tuple
to add to a Computer.- Parameters:
inputs (sequence of
Key
,str
, or other 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:
- save(config, *args, **kwargs)[source]#
Prepare data, call
generate()
, and save to file.This method is used as the callable in the task generated by
make_task()
.
- save_args = {'verbose': False}#
Keyword arguments for
plotnine.ggplot.save()
.
- suffix = '.pdf'#
File extension; determines file format.