Search code examples
pythonholoviewsdatashaderholovizhvplot

How to keep or set width and height of my holoviews plot when using datashade?


I'm trying to create a plot with multiple categories, and have a plot for every category.
Since there are so many datapoints, I'm using datashade. But datashade ignores the width and height that I set for those plots.

How can I keep the width and height that I already set for my plot when using datashader?

Below is example code for this:

# import libraries
import numpy as np
import pandas as pd

import hvplot
import hvplot.pandas

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

from holoviews.operation.datashader import datashade

# create some sample data
sample_scatter1 = np.random.normal(loc=0.0, size=50)
sample_scatter2 = np.random.normal(loc=300., size=50)
sample_category = np.random.choice(2, size=50)

demo_df = pd.DataFrame({
    'col1': sample_scatter1,
    'col2': sample_scatter2,
    'category': sample_category,
})

hv_demo_df = hv.Dataset(demo_df, kdims=['col1', 'category'], vdims=['col2'])

# when i plot without datashade, width works fine
# but with using datashade here i lose the width that i set
datashade(hv_demo_df.to.scatter().opts(width=1000).layout('category')).cols(1)


Plot when not using datashade: not using datashade then width and height can be easily adjusted


Plot when using datashade where I lose width and height that I set: now using datashade how can i set width and height


Solution

  • The issue here is that applying an operation can perform any transform on an element, which means many of the options aren't necessarily valid after the transform has been applied. Therefore operations usually end up dropping the options applied to an element, making it necessary to reapply them after the fact. In your example that means you have to do:

    hv_demo_df = hv.Dataset(demo_df, kdims=['col1', 'category'], vdims=['col2'])
    
    datashade(hv_demo_df.to.scatter().layout('category')).opts(hv.opts.RGB(width=1000)).cols(1)
    

    I agree this is not ideal and we have discussed making sure that at least all options shared by the input and output elements are transferred. This is also related to this issue, which suggests that operation (like datashade) should also use any options applied to the element.