HoloViews defaults


[1]:
import pandas as pd

import bebi103

import holoviews as hv
hv.extension('bokeh')

import bokeh.io
bokeh.io.output_notebook()
Loading BokehJS ...

The bebi103.hv.set_defaults() function (which is essentially the entirety of the hv module) sets some defaults for HoloViews plots that, in my opinion, are prettier than the defaults. To demonstrate, I will make a scatter plot and a box plot using the HoloViews defaults and then again after setting the defaults.

Plots with HoloViews defaults

[2]:
df = pd.read_csv("sample_data.csv")
df['trial'] = df['trial'].astype(str)

def make_plots(df):
    box = hv.BoxWhisker(df, kdims=["trial"], vdims=["x"]).opts(box_color='trial')
    scatter = hv.Points(df, kdims=["x", "y"], vdims="trial").groupby("trial").overlay()

    box = hv.render(box)
    scatter = hv.render(scatter)

    return bokeh.layouts.column(box, bokeh.layouts.Spacer(height=30), scatter)


bokeh.io.show(make_plots(df))

Plots with bebi103 defaults

To set the defaults, we execute bebi103.hv.set_defaults(), and the defaults will be active for all cells in a notebook that follow. We can set the defaults and see the new styling of the plots.

[3]:
bebi103.hv.set_defaults()

bokeh.io.show(make_plots(df))

In both cases, the default plot has larger dimensions and the toolbar is above the plot. Notable differences in the box-and-whisker plot are the use of the Category10 colormap and horizontal grid lines. Notable differences in the scatter plot are the Category10 colormap, larger glyphs, grid lines, and a legend placed outside of the plot.

Caution setting defaults

There are a few caveats to keep in mind when setting the defaults.

  1. In some cases, setting the defaults can lead to errors when doing some compositions using HoloViews. This is because certain properties that are useful for stand-alone plots cannot be set when the plots are in some types of layouts. If you get such an error, the easiest fix is simply not to set the defaults and instead set your styling options using the .opts method of your plotting elements.

  2. The positioning of legends outside of the plot area is convenient, but vertical legend position can be off if there are a large number of entries in the legend because the legend_offset parameter is hard-coded in the defaults. You can reposition the legend by setting the legend_offset in the opts when you make the plotting element. Below, we change the legend position manually, which can be done even after the defaults have been set.

[4]:
scatter = (
    hv.Points(df, kdims=["x", "y"], vdims="trial")
    .groupby("trial")
    .overlay()
    .opts(legend_offset=(10, 0))
)

bokeh.io.show(hv.render(scatter))