* use ``.plot.facet_grid.hist`` to plot histograms
* use ``.plot.facet_grid.hist`` to plot panels of histograms
.. admonition:: WIP 🚧
* Plotting several panels of histograms with ``.plot.facet_grid.hist`` does not yet work.
* use ``.plot.multiplot`` to plot panels of histograms
Histograms are one of the most commonly used plots to visualise distributions.
Let us run the :ref:`SEIRD <model_SEIRD>` model with a fixed configuration multiple times over different seeds, and plot a histogram of the maximum peak height of the number of infected agents.
@@ -40,4 +38,21 @@ The output will look something like this:
The :py:func:`~dantro.plot.funcs.multiplot.multiplot` is a highly versatile yet simple way of plotting different types of plots onto a single figure.
Essentially, the plot function allows to invoke arbitrary functions on the individual subplots of a figure, with data supplied via the plot configuration or the data transformation framework.
In the example below, we are plotting an errorbar plot and a scatter plot onto a single subplot. The errorbar shows the
density of susceptible agents with a standard deviation, and the scattered dots display the recovered agent density,
Naturally, this requires a sweep to have been performed over a ``transmission rate`` variable.
The ``.squeeze`` transformation is required since we will be using the :py:func`seaborn.kdeplot` function,
which expects a flat array.
Next, distribute the plots onto the axes. This requires setting up the figure accordingly using the :ref:`PlotHelper <plot_helper>`:
.. code-block:: yaml
double_kdeplot:
# Everything as above ...
# Distribute the plots
to_plot:
[0, 0]: # select the axis,
- function: sns.kdeplot # the plot function,
data: !dag_result data_low # the data,
label: $p_\mathrm{transmit}=0.4$ # and add any kwargs the plot function accepts
fill: true
[1, 0]:
- function: sns.kdeplot
data: !dag_result data_high
label: $p_\mathrm{transmit}=0.6$
fill: true
color: darkblue
helper:
setup_figure:
ncols: 2
.. note::
We are using ``!dag_result`` `placeholders <https://dantro.readthedocs.io/en/latest/plotting/plot_data_selection.html?highlight=!dag_result#using-data-transformation-results-in-the-plot-configuration>`_
rather than ``!dag_tag`` to pass data to the plot functions.
This will however raise a small warning saying that many of the calculated dag tags are being ignored. To suppress
this, you can exclude tags that are not used in the transformation DAG by removing them from the ``compute_only``
key in the plot configuration; for instance, above you can do
.. code-block:: yaml
compute_only: []
Multiple figures in a single axis
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can of course also plot multiple plots on a single axis; in this case, there is no need to setup the figure, and you
also do not need to specify the axis on which to plot (since there is only one):
for a list of all available functions. However, you can also *import* callables on the fly by passing a 2-tuple of
``(module, name)`` to ``function``:
.. code-block:: yaml
plot:
to_plot:
[0, 0]:
- function: [xarray.plot, scatter]
# args, kwargs here ...
Modifying figure-level and axis-specific features
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Let us now go about modifying the plot appearance somewhat. To control the color, notice how in the
previous example we set
.. code-block:: yaml
double_kdeplot:
# Everything as above ...
color: darkblue
This makes ``color`` a *shared* keyword argument, one that is passed to *all* plot function calls.
Specifying arguments within the ``to_plot`` entries can still overwrite the entries given on the shared level.
.. code-block:: yaml
double_kdeplot:
# Everything as above ...
to_plot:
- function: sns.kdeplot
data: !dag_result data_low
color: red
linestyle: dashed
- function: sns.kdeplot
data: !dag_result data_low
color: yellow
This gives you complete control over the appearance of each individual plot.
With the :ref:`plot helper framework <plot_helper>`, you can control such things as shared axes, the spacing between subplots, limits, and axis scales.
For instance, to modify horizontal or vertical distancing between plots and have figures share their axes, do
.. code-block:: yaml
helper:
# set the number of rows and columns with shared axes
setup_figure:
ncols: 2
nrows: 3
sharex: true
sharey: true
# adjust horizontal and vertical spacing between subplots
subplots_adjust:
hspace: 0.1
wspace: 0.3
The ``subplots_adjust`` entries are passed to :py:meth:`matplotlib.figure.Figure.subplots_adjust`, whereas
``setup_figure`` is passed to :py:func:`matplotlib.pyplot.subplots`.
The :ref:`PlotHelper <plot_helper>` gives you a variety of options to adjust the plot appearance. You can choose to
apply these to the entire plot, or only individual axes. For instance,
.. code-block:: yaml
multiplot:
helper:
set_limits:
x: [0, 1]
will set the x limits on *all* axes to [0, 1]. You can use the ``axis_specific`` helper to only modify certain axes:
.. code-block:: yaml
multiplot:
helper:
axis_specific:
axis1: # some arbitrary axis name
axis: [0, 0] # the x and y coordinates of the subplot in the plot
set_limits:
x: [0, 1]
axis2:
axis: [1, 0]
set_limits:
x: [-1, 0]
All helper functions are available under ``axis_specific``, giving you complete control over the appearance of the
individual axes. Furthermore, helpers specified on the top level apply to all axes.
@@ -61,10 +61,7 @@ The output then looks like this:
:width: 800
:alt: A facet grid example with subspace selection
.. note::
Even when selecting a single value, the subspace entry needs to be sequence-like, i.e. a list.
That's why we specify ``transmission rate: [0.2]`` above.
.. hint::
Subspace selection happens via the :py:class:`~paramspace.paramspace.ParamSpace` method :py:meth:`~paramspace.paramspace.ParamSpace.activate_subspace`, which also offers some other syntax options.