1

I am using Plotly to create a heatmap which reflects a correlation matrix. I would like to know if it's possible to toggle which items are displayed in the plot using the legend.

For example, the below creates a 10x10 matrix and respective heatmap. My aim is to be able to select only "A", "B" & "C" in the plot itself, and it correctly show a 3x3 heatmap only including those items.

import plotly.offline as pyo
import plotly.express as px
import plotly.graph_objs as go
import numpy as np
import pandas as pd

arr = np.random.rand(10,10)
np.fill_diagonal(arr,np.nan)

corr = pd.DataFrame(arr)
corr.columns = ['A','B','C','D','E','F','G','H','I','J']
corr.index = ['A','B','C','D','E','F','G','H','I','J']

trace = go.Heatmap(z=corr.values,x=corr.index.values,y=corr.columns.values,colorscale='RdBu_r',
                   zmax=1,zmin=-1,text=corr.values)
fig = go.Figure(data=[trace])
pyo.plot(fig)

The plot can also be created using plotly express imshow as per the below

fig = px.imshow(corr.values, text_auto='.2f',
                zmin=-1, zmax=1,
                color_continuous_scale='RdBu_r',aspect="auto",
                labels=dict(color="Correlation"),
                x=corr.index.values,
                y=corr.columns.values)
pyo.plot(fig)

For visualization, I'd expect to go from this; enter image description here

to this;

enter image description here

by using the legend (or equivalent)

petezurich
  • 9,280
  • 9
  • 43
  • 57
EdH
  • 240
  • 3
  • 9

1 Answers1

2

An approach to controlling by a legend is create a trace for each combination of keys you want

  • limited to subset of powerset
  • for additional combinations, set to legend only
import plotly.offline as pyo
import plotly.express as px
import plotly.graph_objs as go
import numpy as np
import pandas as pd
import more_itertools

arr = np.random.rand(10, 10)
np.fill_diagonal(arr, np.nan)

corr = pd.DataFrame(arr)
corr.columns = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
corr.index = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]

fig = px.imshow(
    corr.values,
    text_auto=".2f",
    zmin=-1,
    zmax=1,
    color_continuous_scale="RdBu_r",
    aspect="auto",
    labels=dict(color="Correlation"),
    x=corr.index.values,
    y=corr.columns.values,
)

fig.update_traces(name="All", showlegend=True)

ps = [c for c in more_itertools.powerset(corr.columns) if len(c) == 3]

for c in ps:
    fig.add_traces(
        px.imshow(
            corr.loc[list(c), list(c)].values,
            x=list(c),
            y=list(c),
            color_continuous_scale="RdBu_r",
            text_auto=".2f",
        )
        .update_traces(name=str(c), showlegend=True, visible="legendonly")
        .data
    )

fig.update_layout(coloraxis_colorbar_x=-0.2)

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30