如何移除hvplot网格热力图左侧色条并对齐共享坐标轴?
Absolutely, hvplot fully supports both hiding the extra left colorbar and aligning shared axes for your heatmap subplots! Let's walk through the adjustments you need to make to your code:
Step 1: Use Shared Colorbar to Eliminate Redundant Colorbars
By default, each subplot created with col='group' will render its own colorbar. To fix this, use the shared_colorbar parameter to share a single colorbar across all subplots, and you can specify its position to avoid the left-side colorbar.
Step 2: Force Axis Alignment
Ensure your subplots share both x and y axes to match the alignment in the reference documentation using the shared_axes parameter.
Modified Code
import pandas as pd import hvplot.pandas from bokeh.sampledata.unemployment1948 import data # Your original data preprocessing 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 # Updated plot with desired settings df.hvplot.heatmap( x='Year', y='Month', C='rate', col='group', shared_colorbar=True, # Share one colorbar across all subplots colorbar_position='right', # Place colorbar on the right (hides left ones) shared_axes=True, # Align x/y axes perfectly across subplots yaxis='left' # Keep y-axis consistent on the left for alignment )
Parameter Explanations
shared_colorbar=True: This replaces individual colorbars per subplot with a single shared one, eliminating the redundant left colorbar. You can adjust its position withcolorbar_position(options:'right','left','top','bottom').shared_axes=True: Forces all subplots to use identical x and y axis ranges, ticks, and labels, ensuring perfect alignment just like the reference example.yaxis='left': Ensures the y-axis is displayed on the left for all subplots, adding to the consistent aligned look.
Alternative: Fine-Grained Colorbar Control
If you need more control (e.g., hiding a specific subplot's colorbar manually), you can use hvplot's opts method:
# Create the initial plot with colorbars enabled for all subplots plot = df.hvplot.heatmap(x='Year', y='Month', C='rate', col='group', colorbar=True) # Hide colorbar for the first subplot (index 0) and keep it for the second plot = plot.opts( hv.opts.HeatMap(subplot_index=0, colorbar=False), hv.opts.HeatMap(subplot_index=1, colorbar=True) ) plot
Either approach will give you the clean, aligned heatmap subplots with a single colorbar that you're looking for!
内容的提问来源于stack exchange,提问作者Lundi Lin

