Heatmap Text is Not the Same as Z Value

Please see the image attached. I’m not sure why the Text argument is not showing the same value as the Z value.

Here is the code used.

corr_matrix = stock_data.corr()
print(stock_data)
print(corr_matrix)

fig = go.Figure()
fig.add_trace(
    go.Heatmap(
        x=corr_matrix.columns,
        y=corr_matrix.index,
        z=np.array(corr_matrix),
        text=corr_matrix.values,
        texttemplate="%{text:.2f}",
        colorscale="RdYlGn",
    )
)

fig.update_layout(
    plot_bgcolor=app_color["graph_bg"],
    paper_bgcolor=app_color["graph_bg"],
    title="Correlation Matrix",
    font=dict(color="white"),
)

Hi, @riskfree,

is corr_matrix a Pandas DataFrame with a column named values? If so, you can’t use dot notation to address this column, use corr_matrix['values'] instead.

Hello,

No the dataframe does not have columns named “values.” The column titles are a bunch of stock tickers.

Is your question why np.array(corr_matrix) != corr_matrix.values ?

Yes exactly. Why is the z value not the same as the text value when I hover above one of the boxes in the heatmap? In the image, you can see that it is not the same. I want the z value reflected in the text value.

This may be a stupid question, but if you want it to be the same, why do you specify it in a different way?

In other words, what happens, if you use

text=np.array(corr_matrix)

Instead of what you are using right now?

I tried what you said but it still shows up as different from the z value. I specified it this way because I saw one of the examples that uses the syntax.

I have no idea why the z value and text is different. Some of the cells in the heatmap are showing the same value but some don’t. Uggggh.

Hi,

I can’t reproduce this, must be something in your data (maybe duplicates?):

import plotly.graph_objects as go
import pandas as pd
import numpy as np

np.random.seed(42)
corr_matrix = pd.DataFrame(np.random.randint(0,100,size=(7, 7)), columns=list('ABCDEFG'))

fig = go.Figure()
fig.add_trace(
    go.Heatmap(
        x=corr_matrix.columns,
        y=corr_matrix.index,
        z=np.array(corr_matrix),
        text=corr_matrix.values,
        texttemplate="%{text:.2f}",
        colorscale="RdYlGn",
    )
)

fig.update_layout(
    title="Correlation Matrix",
    font=dict(color="white"),
    width=500,
    height=500
)
fig.show()

creates:

Thanks so much for the help. Will have to check my data again.

Thanks so much for your help. I’ll have to figure it out.