I am trying to generate a subplot with heat maps which I obtained with seaborn. When I try to create the subplots, I get a figure with three empty boxes and then the heat maps. I haven't been able to figure out how to assing the maps to the boxes. Here is my code:
def plot_cf_mat(matrix, save, figure_name):
fig, ax = plt.subplots()
ax = sns.heatmap(matrix/np.sum(matrix), annot=True, fmt = '.2%', cmap=sns.light_palette((.376, .051, .224)))
#ax.set_title('Confusion Matrix\n\n');
ax.set_xlabel('\nPredicted Values')
ax.set_ylabel('Actual Values ');
## Ticket labels - List must be in alphabetical order
ax.xaxis.set_ticklabels(['False','True'])
ax.yaxis.set_ticklabels(['False','True'])
if save:
plt.savefig("".join(["cf_mat_", figure_name, ".jpg"]), bbox_inches='tight')
return ax
#Plot
fig, axes = plt.subplots(1, 3)
i = 0
for row in axes:
fun.plot_cf_mat(matrix = cf_mat_x_clssifr[i][-1], save = False, figure_name = None)
i =1
plt.show()
CodePudding user response:
You're not passing the created axes to your function or to sns.heatmap
. Pass the row to your function (notice the ax=row
part):
#Plot
fig, axes = plt.subplots(1, 3)
i = 0
for row in axes:
fun.plot_cf_mat(matrix = cf_mat_x_clssifr[i][-1], save = False, figure_name = None, ax=row)
i =1
plt.show()
And now plot your heatmap on that ax object (notice the ax
argument in the function and passing the object to sns.heatmap
):
def plot_cf_mat(matrix, save, figure_name, ax=None):
ax = ax or plt.gca()
# fig, ax = plt.subplots()
ax = sns.heatmap(matrix/np.sum(matrix), annot=True, fmt = '.2%', cmap=sns.light_palette((.376, .051, .224)), ax=ax)