Tutorial: Controlling Plot Aesthetics

Controlling plot aesthetics

Changing the y-axes labels.

1  two_groups_unpaired.mean_diff.plot(swarm_label="This is my\nrawdata",
2                                     contrast_label="The bootstrap\ndistribtions!");

Color the rawdata according to another column in the dataframe.

1  multi_2group.mean_diff.plot(color_col="Gender");
1  two_groups_paired_baseline.mean_diff.plot(color_col="Gender");

Changing the palette used with custom_palette. Any valid matplotlib or seaborn color palette is accepted.

1  multi_2group.mean_diff.plot(color_col="Gender", custom_palette="Dark2");
1  multi_2group.mean_diff.plot(custom_palette="Paired");

You can also create your own color palette. Create a dictionary where the keys are group names, and the values are valid matplotlib colors.

You can specify matplotlib colors in a variety of ways. Here, I demonstrate using named colors, hex strings (commonly used on the web), and RGB tuples.

1  my_color_palette = {"Control 1" : "blue",
2                      "Test 1"    : "purple",
3                      "Control 2" : "#cb4b16",     # This is a hex string.
4                      "Test 2"    : (0., 0.7, 0.2) # This is a RGB tuple.
5                     }
7  multi_2group.mean_diff.plot(custom_palette=my_color_palette);

By default, dabest.plot() will desaturate the color of the dots in the swarmplot by 50%. This draws attention to the effect size bootstrap curves.

You can alter the default values with the swarm_desat and halfviolin_desat keywords.

1  multi_2group.mean_diff.plot(custom_palette=my_color_palette,
2                              swarm_desat=0.75,
3                              halfviolin_desat=0.25);

You can also change the sizes of the dots used in the rawdata swarmplot, and those used to indicate the effect sizes.

1  multi_2group.mean_diff.plot(raw_marker_size=3,
2                              es_marker_size=12);

Changing the y-limits for the rawdata axes, and for the contrast axes.

1  multi_2group.mean_diff.plot(swarm_ylim=(0, 5),
2                              contrast_ylim=(-2, 2));

If your effect size is qualitatively inverted (ie. a smaller value is a better outcome), you can simply invert the tuple passed to contrast_ylim.

1  multi_2group.mean_diff.plot(contrast_ylim=(2, -2),
2                              contrast_label="More negative is better!");

The contrast axes share the same y-limits as that of the delta - delta plot and thus the y axis of the delta - delta plot changes as well.

1  paired_delta2.mean_diff.plot(contrast_ylim=(3, -3),
2                               contrast_label="More negative is better!");

You can also change the y-limits and y-label for the delta - delta plot.

1  paired_delta2.mean_diff.plot(delta2_ylim=(3, -3),
2                               delta2_label="More negative is better!");

You can add minor ticks and also change the tick frequency by accessing the axes directly.

Each estimation plot produced by dabest has 2 axes. The first one contains the rawdata swarmplot; the second one contains the bootstrap effect size differences.

 1  import matplotlib.ticker as Ticker
 3  f = two_groups_unpaired.mean_diff.plot()
 5  rawswarm_axes = f.axes[0]
 6  contrast_axes = f.axes[1]
 8  rawswarm_axes.yaxis.set_major_locator(Ticker.MultipleLocator(1))
 9  rawswarm_axes.yaxis.set_minor_locator(Ticker.MultipleLocator(0.5))
11  contrast_axes.yaxis.set_major_locator(Ticker.MultipleLocator(0.5))
12  contrast_axes.yaxis.set_minor_locator(Ticker.MultipleLocator(0.25))
 1  f = multi_2group.mean_diff.plot(swarm_ylim=(0,6),
 2                                 contrast_ylim=(-3, 1))
 4  rawswarm_axes = f.axes[0]
 5  contrast_axes = f.axes[1]
 7  rawswarm_axes.yaxis.set_major_locator(Ticker.MultipleLocator(2))
 8  rawswarm_axes.yaxis.set_minor_locator(Ticker.MultipleLocator(1))
10  contrast_axes.yaxis.set_major_locator(Ticker.MultipleLocator(0.5))
11  contrast_axes.yaxis.set_minor_locator(Ticker.MultipleLocator(0.25))

For mini-meta plots, you can hide the weighted avergae plot by setting show_mini_meta=False in the plot() function.

1  mini_meta_paired.mean_diff.plot(show_mini_meta=False)

Similarly, you can also hide the delta-delta plot by setting show_delta2=False in the plot() function.

1  paired_delta2.mean_diff.plot(show_delta2=False)

Creating estimation plots in existing axes

Implemented in v0.2.6 by Adam Nekimken.

dabest.plot has an ax keyword that accepts any Matplotlib Axes. The entire estimation plot will be created in the specified Axes.

 1  from matplotlib import pyplot as plt
 2  f, axx = plt.subplots(nrows=2, ncols=2,
 3                        figsize=(15, 15),
 4                        gridspec_kw={'wspace': 0.25} # ensure proper width-wise spacing.
 5                       )
 7  two_groups_unpaired.mean_diff.plot(ax=axx.flat[0]);
 9  two_groups_paired.mean_diff.plot(ax=axx.flat[1]);
11  multi_2group.mean_diff.plot(ax=axx.flat[2]);
13  multi_2group_paired.mean_diff.plot(ax=axx.flat[3]);

In this case, to access the individual rawdata axes, use name_of_axes to manipulate the rawdata swarmplot axes, and name_of_axes.contrast_axes to gain access to the effect size axes.

1  topleft_axes = axx.flat[0]
2  topleft_axes.set_ylabel("New y-axis label for rawdata")
3  topleft_axes.contrast_axes.set_ylabel("New y-axis label for effect size")
5  f

Applying style sheets

Implemented in v0.2.0.

dabest can apply matplotlib style sheets to estimation plots. You can refer to this gallery of style sheets for reference.

1  import matplotlib.pyplot as plt
2  plt.style.use("dark_background")
1  multi_2group.mean_diff.plot();