Plotnine (compat.plotnine)#

Package documentation

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
  1. Create a subclass that overrides Plot.generate(), Plot.basename, and optionally Plot.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")
       ...:         )
  2. add() the class to a Computer directly. The Plot.add_tasks() method handles connecting the Plot.inputs to

    # 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"]
    (<bound method of <__main__.DemoPlot object at 0x7fd62c77d120>>,
  3. get() the node. The result is the path the the saved plot(s).

    In [9]: c.get("plot")
    Out[9]: PosixPath('plotnine-demo.svg')
    Demonstration output from genno.compat.plotnine.
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.


args ( of pandas.DataFrame or other) –

One argument is given corresponding to each of the inputs.

Because plotnine operates on pandas data structures, save() automatically converts any Quantity inputs to pandas.DataFrame before they are passed to generate().

inputs: Sequence[Hashable] = []#

Keys referring to Quantities or other inputs accepted by generate().

classmethod make_task(*inputs)[source]#

Return a task tuple to add to a Computer.

Deprecated since version 1.18.0: Use add_tasks() instead.


*inputs (.Key or str or, optional) – If provided, overrides the inputs property of the class.


  • 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 from config["output_dir"], basename, and suffix. The implementation of generate() in a Plot sub-class may assign any other value, for instance one constructed at runtime from the inputs.

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

suffix = '.pdf'#

File extension; determines file format.