0

Hello :) Does anybody know how to make a matplotlib matrix figure clickable?

For instance, if I click a cell of the matrix, it should highlight the row and the column of this cell (see the before and after pictures below, where the background of the corresponding row and column turn green after clicking the middle cell in this example). The figure should be clickable after exporting it (so we can click it when imported in a pdf for instance), but not necessarily when displayed in jupyter notebook.

Here is the code I am using to plot the confusion matrix :

def plotConfusionMatrix(y_test,y_pred,list_name_classes,ax=None):
'''
Plot row normed confusion matrix. Rows are normalized to sum to 100% throughout true classes. The number of observations is also visible on the matrix

Parameters
----------
y_test  : list or array
    true encoded labels
    
y_pred : list or array
    predicted encded labels  
    
list_name_classes : list or array of str
    names of the classified classes. Must be in the same order as encoded labels

Returns
-------
matplotlib axis  
'''
if ax is None:
    ax=plt.gca()
cf_matrix = confusion_matrix(y_test, y_pred)
matrix_percent=[]
for row in range(cf_matrix.shape[0]):
    matrix_percent.append(cf_matrix[row]/sum(cf_matrix[row]))
matrix_percent=np.array(matrix_percent)
group_counts = ["{0:0.0f}".format(value) for value in cf_matrix.flatten()]
group_percentages = ["{0:.2%}".format(value) for value in matrix_percent.flatten()]
labels = [f"{v1}\n{v2}" for v1, v2 in zip(group_counts,group_percentages)]
labels = np.asarray(labels).reshape(len(list_name_classes),len(list_name_classes))    
mat = sns.heatmap(matrix_percent, annot=labels, fmt='', cmap="magma", cbar=True,vmin=0, vmax=1,ax=ax)
labels=list_name_classes 
mat.set_xticklabels(labels)
mat.set_yticklabels(labels)
mat.set(ylabel="True Label", xlabel="Predicted Label")
return(ax)

Before
enter image description here

After
enter image description here

Yoann
  • 1
  • 1
  • This is a task, not a question. Have you tried anything? Have you read related chapters in the [matplotlib documentation](https://matplotlib.org/stable/users/explain/interactive.html), studied similar examples in their [example gallery](https://matplotlib.org/stable/gallery/index.html#event-handling), or looked at related [SO questions](https://stackoverflow.com/search?q=matplotlib+heatmap+clickable)? – Mr. T Jan 11 '22 at 13:24
  • Thank you @Mr.T for your answer. Yes you are right, this is more a task than a question, because I didn´t found anything working in this way, even when looking on the links you shared. I have reviewed my needs, and looked for labels displaying when moving my mouse over a given cell of the matrix. – Yoann Jan 12 '22 at 13:34
  • A collegue of mine gave me a useful tool called holoviews (https://holoviews.org/reference/elements/bokeh/HeatMap.html#elements-bokeh-gallery-heatmap), which is based on Bokeh package. However I didn´t succeed to load my seaborn heatmap with Holoviews. I also tried mplcursors, but it is very slow and I am more interested to get the dynamic labels on a picture, rather than in a matplotlib window, because the matrix is very huge (and so easier to manipulate as an image). – Yoann Jan 12 '22 at 13:34
  • I understand your frustration but this type of question is not the scope of SO. Don't ask "which program should I used for interactive display". Instead ask, "my code [insert code here] tries to load a seaborn heatmap. I get the following error message/no output/my computer explodes. How should I change the code to load the heatmap?". Please read [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Mr. T Jan 12 '22 at 13:41
  • Ok I see, thank you @Mr.T – Yoann Jan 17 '22 at 09:05

0 Answers0