在下面的代码中,我使用
import mplcursors
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
labels_true = [1,2,3,4,5,6]
labels_pred = [1,2,3,1,2,3]
# confusion_matrix
cm = confusion_matrix(labels_true, labels_pred, normalize='true')
# dataframe
df_cm = pd.DataFrame(cm)
其次是
# option 1:
sns.heatmap(df_cm, annot=True, linewidths=1, cmap="Blues_r",xticklabels=2, square=True)
或者,或者,
# option 2:
plt.imshow(df_cm)
其次是
# clickable cells
# cursor = mplcursors.cursor(heatmap, hover=False)
cursor = mplcursors.cursor(hover=False)
@cursor.connect("add")
def on_add(sel):
i,j = sel.target.index
text = 'Test message!'
sel.annotation.set_text(text)
plt.show()
当我使用选项 1 时,我得到这个,它有一个加号,即每个单元格上都有一个数字标签;但也有一个减号,即单元格不可点击。
当我使用选项 2 时,我得到了这个,它有一个加号,我可以单击单元格;但有一个减号,我看不到每个单元格上标记的数字。
难道没有办法让我两全其美吗?我想要单元格和可点击单元格上的数字。谢谢!