arviz_plots.PlotCollection.generate_aes_dt

arviz_plots.PlotCollection.generate_aes_dt#

PlotCollection.generate_aes_dt(aes, data=None, **kwargs)[source]#

Generate the aesthetic mappings.

Populate and store the DataTree attribute .aes of the PlotCollection.

Parameters:
aesmapping of {strlist of hashable or False}

Dictionary with aesthetics as keys and as values a list of the dimensions it should be mapped to. The pseudo-dimension __variable__ is also valid to indicate the variable should be part of the aesthetic mapping.

It can also take False as value to indicate that no mapping should be considered for that aesthetic key.

dataxarray.Dataset, optional

Data for which to generate the aesthetic mappings.

**kwargsmapping, optional

Dictionary with aesthetics as keys and as values a list of the values that should be taken by that aesthetic.

Notes

Mappings are applied only when all variables defined in the mapping are found. Thus, a mapping for ["chain", "hierarchy"] would be applied if both dimensions are present in the variable, otherwise it is completely ignored.

It can be the case that a mapping is ignored for a specific variable because it has none of the dimensions that define the mapping or because it doesn’t have all of them. In such cases, out of the values in the property cycle, the first one is taken out and reserved as neutral_element. Then, the cycle excluding the first element is used when applying the mapping, and the neutral element is used when the mapping can’t be applied.

It is possible to force the inclusion of the neutral element from the property value cycle by providing the same value in both the first and second positions in the cycle, but this is generally not recommended.

Examples

Initialize a PlotCollection with the rugby dataset as data. faceting and aesthetics mapping are independent. Thus, as we are limiting ourselves to the use of this method, we can provide an empty DataTree as viz_dt.

from arviz_base import load_arviz_data
from arviz_plots import PlotCollection
import xarray as xr
idata = load_arviz_data("rugby_field")
pc = PlotCollection(idata.posterior, xr.DataTree(), backend="matplotlib")
aes_dt = pc.generate_aes_dt(
    aes={
        "color": ["__variable__", "team"],
        "y": ["field", "team"],
        "marker": ["field"],
        "linestyle": ["chain"],
    },
    color=[f"C{i}" for i in range(6)],
    y=list(range(13)),
    linestyle=["-", ":", "--", "-."],
)

The generated aes_dt has one group per aesthetic. Within each group There can be the variables from the Dataset used to initialize the PlotCollection or the variables “mapping” and “neutral_element”.

Let’s inspect its contents for each aesthetic. We’ll start with the color which had __variable__, team as dimensions to encode.

aes_dt["color"]
<xarray.DatasetView> Size: 424B
Dimensions:       (team: 6)
Coordinates:
  * team          (team) <U8 192B 'Wales' 'France' ... 'Italy' 'England'
Data variables:
    intercept     <U2 8B 'C0'
    atts_team     (team) <U2 48B 'C1' 'C2' 'C3' 'C4' 'C5' 'C0'
    defs_team     (team) <U2 48B 'C1' 'C2' 'C3' 'C4' 'C5' 'C0'
    atts          (team) <U2 48B 'C1' 'C2' 'C3' 'C4' 'C5' 'C0'
    defs          (team) <U2 48B 'C1' 'C2' 'C3' 'C4' 'C5' 'C0'
    sd_att        <U2 8B 'C1'
    sd_def        <U2 8B 'C2'
    sd_att_field  <U2 8B 'C3'
    sd_def_field  <U2 8B 'C4'

In this case, each unique combination of variable and coordinate value of the team dimension gets a different color. They only end up being repeated once the provided cycler runs out of elements. In the cases where __variable__ is used, the data subset ds[var_name].sel(coords) gets the aesthetic values in aes_dt[aes_key][var_name].sel(coords), however, this isn’t always as straightforward; thus, the recommended way to get the corresponding aes for a specific subset is using get_aes_kwargs

Next let’s look at the marker. We didn’t provide any defaults for the marker, but as we specified the backend, some default values were generated for us. Here, we asked to encode the “field” dimension information only:

aes_dt["marker"]
<xarray.DatasetView> Size: 44B
Dimensions:          (field: 2)
Coordinates:
  * field            (field) <U4 32B 'home' 'away'
Data variables:
    neutral_element  <U1 4B 'o'
    mapping          (field) <U1 8B '+' '^'

We have a “neutral_element” variable which will be used for variables where the field dimension is not present and a “mapping” variable with a different marker value per coordinate in the field dimension, with all these values being different to the “neutral_element” one. The “y” aesthetic is very similar.

Lastly, the “linestyle” aesthetic, which we asked to use to encode the chain information.

aes_dt["linestyle"]
<xarray.DatasetView> Size: 64B
Dimensions:  (chain: 4)
Coordinates:
  * chain    (chain) int64 32B 0 1 2 3
Data variables:
    mapping  (chain) <U2 32B '-' ':' '--' '-.'

As all variables have the “chain” dimension, there is no “neutral_element” here, and the first element in the property cycle (here the solid line “-“) is used as part of the “chain” mapping instead of being reserved for variables without a “chain” dimension. Note that in such cases, trying to use a data variable without “chain” as dimension would result in an error, the mapping is not defined.