import seaborn as sb
import numpy as np
from matplotlib import pyplot as plt
A = np.array([[10, 5], [3, 10]], dtype=np.int32)
plt.figure()
sb.heatmap(
A,
square=True,
annot=True,
xticklabels=["False", "Positive"],
yticklabels=["False", "Positive"],
cbar=False,
fmt="2d",
)
plt.title("Example plot")
plt.show()
Shows example of an heatmap. I wish to add accuracy of each row to left side of the image.
The plot should be similar to
Can this be achived?
CodePudding user response:
You can add the following lines to you code between the heatmap
call and plt.title(...
:
# Compute the values to added to the plot
row_accuracies = [A[i][i] * 100 / A[i].sum() for i in range(A.shape[0])]
# Get axes
ax = fig.axes
# [OPTIONAL] Add ticks on the right side
ax.tick_params(axis='y', which='major', left=True, right=True, labelleft=True, labelright=False)
# Add text where the ticks are (roughly)
for i, acc in enumerate(row_accuracies):
ax.text(ax.get_xlim()[1] * 1.05, ax.get_yticks()[i] * 1.01, f'{acc:.2f}%')
This is the result: