pythonhvplotholoviz

Remove left ColorBar of grid heatmap plot in pvplot


I try to make grid heatmap by pvplot. I refer to this link. https://hvplot.pyviz.org/user_guide/Subplots.html

import hvplot.pandas
from bokeh.sampledata.unemployment1948 import data

data.Year = data.Year.astype(str)
data = data.set_index('Year')
data.drop('Annual', axis=1, inplace=True)
data.columns.name = 'Month'

df = pd.DataFrame(data.stack(), columns=['rate']).reset_index()
df = df.tail(40)
df['group'] = [1,2]*20
df.hvplot.heatmap(x='Year', y='Month', C='rate', col='group', colorbar=True)

heatmap

heatmap

I expect left colorbar does not show. And shared axes could be aligned like link page. Can anyone tell if pvplot could support this? Thanks.


Solution

  • The holoviews object you created is a so-called gridspace. It contains a separate plot for every group in your data.
    You can access the plot for every group by the name of the group. The names of the groups in your example are 1 and 2. The idea is to use colorbar=False only on the plot for group 1.

    You can remove the colorbar from the first group like this:

    # create a variable that holds your gridspace plots
    grid_space_groups = df.hvplot.heatmap(x='Year', y='Month', C='rate', col='group')
    
    # checking the names of your groups in your gridspace
    print(grid_space_groups.keys())
    
    # removing the colorbar from the plot with the group that has name '1'
    grid_space_groups[1] = grid_space_groups[1].opts(colorbar=False)
    
    # plot your grid space 
    grid_space_groups
    


    Please note:
    When your colorbars don't have the same range of values, you first have to make sure that they both have the same range. You can do that like this for column rate:
    grid_space_groups.redim.range(rate=(0, 10))

    Alternatively, you could create each plot separately for each group.
    The plot from group 1, you create without colorbar with colorbar=False, and the plot for group 2 you create with colorbar:

    plot_1 = df[df.group == 1].hvplot.heatmap(x='Year', y='Month', C='rate', colorbar=False)
    plot_2 = df[df.group == 2].hvplot.heatmap(x='Year', y='Month', C='rate')
    plot_1 + plot_2
    

    left colorbar removed from gridspace holoviews