Black Lives Matter. Please consider donating to Campaign Zero's mission of ending police violence in America.
https://www.joincampaignzero.org

Insert pandas crostabb into a dash graph

Hi, I am trying to insert a pd.crosstab into a dcc.Graph.

This is what I have:

dfa = pd.DataFrame(data, columns=[‘y_Actual’, ‘y_Predicted’])
confusion_matrixd = pd.crosstab(dfa[‘y_Actual’], dfa[‘y_Predicted’], rownames=[‘Actual’], colnames=[‘Predicted’])

What I wanna do is to plot that confusion matrix. It would be something like this.

dcc.Graph(id=‘confusionMatrix’,
figure={
‘data’: [go.Heatmap(x=dfa[‘y_Actual’].unique(),
y=dfa[‘y_Predicted’].unique(),
z=confusionMatrixd
)
],

                  }

But this doesn’t work. Anyone has any ideas to plot the confusion matrix?

Hi @lm97 I think that the problem here is that x and y are inverted. For the record, the code below works as intended

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
df = px.data.iris()
mat = pd.crosstab(df['petal_width'], df['sepal_width']) 
fig = go.Figure(go.Heatmap(x=mat.columns, y=mat.index, z=mat))
fig.show()

Thanks, that works! And anyone know how to annotate in each square the correspondent value?