Assigning colours to groups of rows/columns in Python Plotly heatmaps

Is there a way to use plotly in Python to generate a heatmap (e.g., through imshow ) and assign different colours to different groups of rows/columns? The goal is to generate a plot similar to the clustermap that can be generated with seaborn without dendrograms but with external colours of the rows/columns to indicate distinct groups, as shown in the example attached here below and obtained from seaborn’s documentation where rows are assigned with different colours for indicating species .

In particular, is there a solution to this using and starting from (or using directly) data within a pandas 's DataFrame ? Also, ideally can this column be separated from the main heatmap?

I have found a solution using subplots, which I report here below. However, I believe this is such an important standard plot that is worth to be included as a default plot to be generated through plotly express. Thus, I would like to ask whether you would please consider to include it in the future? Specifically, I think it would be important to add the possibility to include colours to distinguish groups of rows and columns in heatmaps.

This is an example on how distinct groups or clusters of rows/columns in a heatmap can be expressed with side colours using subplots:

fig = make_subplots(rows=2, cols=2, column_widths=[0.9, 0.1], row_heights=[0.9, 0.1], vertical_spacing=0.02)
table = pd.pivot_table(df, index='A', columns='B', values='C', aggfunc='first')
fig.append_trace(go.Heatmap(z=table, colorscale = 'bugn', colorbar={'x' : 1.1, 'y' : 0.5}), row=1, col=1)
fig.append_trace(go.Heatmap(z=df1, colorscale='spectral', showscale=False), row=1, col=2)
fig.append_trace(go.Heatmap(z=df2, colorscale='plasma', showscale=False), row=2, col=1)
fig.update_layout(xaxis2_showticklabels=False, yaxis2_showticklabels=False,
                  xaxis3_showticklabels=False, yaxis3_showticklabels=False,

Can you please add more info regarding df, df1, and df2?

I’m trying to add grouping labels to my heatmap rows (using Plotly) and find it difficult to understand from your answer how to apply it on the iris dataset.

many thanks!

1 Like