【发布时间】:2021-09-17 05:23:48
【问题描述】:
我正在尝试使用带注释的热图在 Plotly 中绘制混淆矩阵。现在我注意到,由于某种原因,Y 轴会根据标签的数据类型翻转,即使我将数据类型强制为字符串也是如此。
下面的代码将提供两个图,其中第二个图与第一个图相比具有倒置的 Y 轴。第一个是我希望它显示的方式,无论类名的数据类型如何。
顺序在哪里更改,我如何强制它保持在提供的列表顺序中?
'''
import numpy as np
confm = array([[10, 4, 0, 1, 0],
[1, 20, 10, 1, 0],
[0, 7, 30, 3, 0],
[0, 1, 1, 40, 2],
[1, 1, 2, 2, 50]])
labels = [1, 2, 3, 4, 5]
import plotly.graph_objects as go
import plotly.figure_factory as ff
def plot_matrix(confm, class_names):
z = np.round(confm, 2) # for readability
class_names = [str(c) for c in class_names] # force all to string
fig = ff.create_annotated_heatmap(z,
colorscale='reds',
xtype='array',
ytype='array',
x=class_names,
y=class_names[::-1], # invert order
)
fig.update_layout(
title='Confusion matrix',
width=600,
height=600,
margin=dict(pad=20),
plot_bgcolor='white',
xaxis=dict(title='Prediction',
showticklabels = True,
side='bottom',
tickmode='array',
tickvals=class_names,
ticktext=class_names),
yaxis=dict(title='Truth',
tickmode='array',
tickvals=class_names,
ticktext=class_names[::-1])
)
fig.show()
plot_matrix(confm, [1, 2, 3, 4, 5])
plot_matrix(confm, ['a', 'b', 'c', 'd', 'e'])
'''
【问题讨论】:
标签: python plotly heatmap factory figure