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. Call make_task() to get a task tuple suitable for adding to a Computer:

    # 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, "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 0x7fc7466f0e50>>,
     'config',
     'x:t',
     'y:t')
    
  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 = ''#

Filename base for saving the plot.

abstract generate(*args, **kwargs)[source]#

Generate and return the plot.

Must be implemented by subclasses.

Parameters:

args (collections.abc.Sequence of pandas.DataFrame) – Because plotnine operates on pandas data structures, save() automatically converts Quantity before they are passed to generate().

inputs: Sequence[Hashable] = []#

Keys for quantities needed 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.

Parameters:

*inputs (.Key or str or collections.abc.Hashable, optional) – If provided, overrides the inputs 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:

tuple

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.