Mohammad Alshehri
Mohammad Alshehri

Reputation: 63

Plotly: How to determine direction of confusion matrix?

I'm using Plotly for plotting the below confusion matrix:

cf_matrix= confusion_matrix(y_true, y_pred)
print(cf_matrix)

[[1595  545 2240]
 [ 788  722 2870]
 [ 181  118 4081]]

import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(cf_matrix)
fig.show()

but for some reason the plot is in the wrong direction "starting from the down left corner" as can be seen below:

Any workaround to get it the Matrix the usual direction and also show the X and Y axis?enter image description here

Upvotes: 1

Views: 1214

Answers (1)

vestland
vestland

Reputation: 61094

The answer:

fig.update_layout(yaxis = dict(categoryorder = 'category descending'))

Some details in your example:

If the data you've presented is in fact your dataset, then you haven't specified any names. You can do that with, for example, names = list('ABC'), and then do:

fig = ff.create_annotated_heatmap(z, x = names, y = names)

In order to change the order of the heatmap, you can do:

fig.update_layout(yaxis = dict(categoryorder = 'category descending'))

Plot:

enter image description here

If this was not exactly the way you wanted it to appear, you can change the xaxis in the same way. Also, other options for categoryorder are:

['trace', 'category ascending', 'category descending',
'array', 'total ascending', 'total descending', 'min
ascending', 'min descending', 'max ascending', 'max
descending', 'sum ascending', 'sum descending', 'mean
ascending', 'mean descending', 'median ascending', 'median
descending']

Complete code:

import plotly.figure_factory as ff

z = [[1595, 545, 2240],
 [ 788,   722,  2870],
 [ 181,   118,  4081]]

names = list('ABC')

fig = ff.create_annotated_heatmap(z, x = names, y = names)
fig.update_layout(yaxis = dict(categoryorder = 'category descending'))
fig.show()

Upvotes: 3

Related Questions