Skip to content

Latest commit

 

History

History
64 lines (45 loc) · 1.83 KB

compat-plotnine.rst

File metadata and controls

64 lines (45 loc) · 1.83 KB

Plotnine (.compat.plotnine)

Package documentation <plotnine:index>

genno.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

  1. Create a subclass that overrides Plot.generate, Plot.basename, and optionally Plot.inputs.

    In [1]: 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:

    python

    # Set up a Computer, including the output path and some data c = Computer(output_dir=Path(".")) t = [("t", [-1, 0, 1])] c.add("x:t", Quantity(xr.DataArray([1.0, 2, 3], coords=t), name="x")) c.add("y:t", Quantity(xr.DataArray([1.0, 4, 9], coords=t), name="y"))

    # Add the plot to the Computer c.add("plot", DemoPlot.make_task("x:t", "y:t"))

    # Show the task that was added c.graph["plot"]

  3. .get the node. The result is the path the the saved plot(s).

    In [1]: c.get("plot") Out[1]: ./test.svg

    Demonstration output from genno.compat.plotnine.

genno.compat.plotnine