yishairasowsky
yishairasowsky

Reputation: 831

seaborn or plt with mpl cursors?

In my code below, I use

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)

followed by either

# option 1:
sns.heatmap(df_cm, annot=True, linewidths=1, cmap="Blues_r",xticklabels=2, square=True)

or, alternatively

# option 2:
plt.imshow(df_cm)

followed by

# 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()

When I use option 1, I get thisenter image description here, which has a plus, that the cells each have a number label on them; but also has a minus, that the cells are not clickable.

When I use option 2, I get thisenter image description here, which has a plus, that I can click the cells; but has a minus, that I cannot see the number labeled on each cell.

Isn't there a way I can get the best of both worlds? I want numbers on the cells AND clickable cells. Thanks!

Upvotes: 0

Views: 394

Answers (0)

Related Questions