How to add tooltips to a confusion matrix rendered via a Seaborn heatmap?

断了今生、忘了曾经 提交于 2020-03-23 06:53:06

问题


How can I make my mat plot lib interactive? For example, when I hover the mouse over each cell of a confusion matrix, I'd like to display the instance of that prediction.

confusion_mat_df = pd.DataFrame(confusion_mat,columns = pred_spectrum, index = actual_spectrum)

plt.figure(figsize=(7,5)) # width,height
sns.heatmap(confusion_mat_df, annot=True)

回答1:


Here is an example to illustrate how to use mplcursors for an sklearn confusion matrix.

Unfortunately, mplcursors doesn't work with seaborn heatmaps. Seaborn uses a QuadMesh for the heatmap, which doesn't support the necessary coordinate picking.

In the code below I added the confidence at the center of the cell, similar to seaborn's. I also changed the colors the texts and arrows to be easier to read. You'll need to adapt the colors and sizes to your situation.

from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
import mplcursors

y_true = ["cat", "ant", "cat", "cat", "ant", "bird", "dog"]
y_pred = ["ant", "ant", "cat", "cat", "ant", "cat", "dog"]
labels = ["ant", "bird", "cat", "dog"]
confusion_mat = confusion_matrix(y_true, y_pred, labels=labels)

fig, ax = plt.subplots()
heatmap = plt.imshow(confusion_mat, cmap="jet", interpolation='nearest')

for x in range(len(labels)):
    for y in range(len(labels)):
        ax.annotate(str(confusion_mat[x][y]), xy=(y, x),
                    ha='center', va='center', fontsize=18, color='white')

plt.colorbar(heatmap)
plt.xticks(range(len(labels)), labels)
plt.yticks(range(len(labels)), labels)
plt.ylabel('Predicted Values')
plt.xlabel('Actual Values')

cursor = mplcursors.cursor(heatmap, hover=True)
@cursor.connect("add")
def on_add(sel):
    i, j = sel.target.index
    sel.annotation.set_text(f'{labels[i]} - {labels[j]} : {confusion_mat[i, j]}')
    sel.annotation.set_fontsize(12)
    sel.annotation.get_bbox_patch().set(fc="papayawhip", alpha=0.9, ec='white')
    sel.annotation.arrow_patch.set_color('white')

plt.show()

PS: The annotation can be multiline, for example:

sel.annotation.set_text(f'Predicted: {labels[i]}\nActual: {labels[j]}\n{confusion_mat[i, j]:5}')


来源:https://stackoverflow.com/questions/59810130/how-to-add-tooltips-to-a-confusion-matrix-rendered-via-a-seaborn-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!