'format text cell for empty values and color cell for all negative values
I am generating a plotly heatmap as follow:
@app.callback(
Output('graph', 'figure'),
[
Input('submit', 'n_clicks')
],
prevent_initial_call=True
)
def update_plot(n_clicks):
if n_clicks:
my_row = ['T-3', 'T-2', 'T-1']
col = ['T-2', 'T-1', 'T0']
df_stat = pd.DataFrame([[12, -3.5, 7.8], [np.nan, 0.5, -19], [np.nan, np.nan, 56]], columns=col)
df_stat.index = my_row
fig = go.Figure()
fig.add_trace(go.Heatmap(
x=df_stat.columns,
y=df_stat.index,
z=df_stat.values.tolist(),
# zauto=True,
zmax=0.67,
zmin=0,
hoverongaps=False,
showscale=True,
colorscale='OrRd',
text=df_stat.to_numpy(),
texttemplate="%{text}",
hovertemplate='My number: %{z:.2f}<extra></extra>',
textfont={"color": "black"},
# autocolorscale=True
))
fig.update_yaxes(autorange="reversed", type='category', categoryorder='array', categoryarray=my_row)
fig.update_xaxes(automargin=True, side='top', type='category', categoryorder='array', categoryarray=col)
fig.update_layout(height=600, width=1200)
return fig
The dataframe used as input is triangular (made of np.nan and floats). What I am trying to archieve is the following:
- for the lower triangular part the text should be "" and not nan or null. The background color for these "empty" cells should be transparent.
- for the colorscale, would it be possible to have it applied to only to positive values? Hence having "OrRd" colorscale for the positive values and light grey for any negative value? I tried setting zmin/zmax but then the negative values get the colorscale minimal color. I would be looking for a the colorscale "OrRd" with minimal value set to light grey. Having autocolorscale set to True and zauto to True seem to disregard the chosen colorscale.
- for the positive values, is possible to use the autocolorscale (ie set to True) while keeping the colorscale to "OrRd"? I played around the zauto/zmin-zmax and autocolorscale but couldn't get the desired colorscale.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


