Issue
I'm trying to plot to confusion matrix in the same image, but they come out in different sizes.
Here's the code:
fig, ax = plt.subplots(nrows=1, ncols=2, figsize = (18,8))
fig.suptitle('Matriz de Confusão')
skplt.metrics.plot_confusion_matrix(y_test, y_pred_log, normalize=True, ax=ax[0], title=('Regressão Logística'))
skplt.metrics.plot_confusion_matrix(y_test, y_pred_tree, normalize=True, ax=ax[1], title=('Árvore de decisão'))
ax[0].xaxis.set_ticklabels(['Normal', 'Fraude']); ax[0].yaxis.set_ticklabels(['Normal', 'Fraude']);
ax[1].xaxis.set_ticklabels(['Normal', 'Fraude']); ax[1].yaxis.set_ticklabels(['Normal', 'Fraude']);
plt.show()
And this is what I'm getting:
How can I change the size of the second plot?
Also if I could delete the extra color bar would be nice.
Solution
You sould define the axes where colorbar need to be placed. You can check this answer as a reference.
Applying those concept to your case would result in something similar to this:
import matplotlib.pyplot as plt
import numpy as np
M1 = np.random.rand(2, 2)
M2 = np.random.rand(2, 2)
fig, ax = plt.subplots(1, 2, figsize = (18, 8))
plt.subplots_adjust(right = 0.77)
cbar_ax_1 = fig.add_axes([0.8, 0.1, 0.04, 0.8])
cbar_ax_2 = fig.add_axes([0.9, 0.1, 0.04, 0.8])
im_1 = ax[0].imshow(M1, cmap = 'magma')
im_2 = ax[1].imshow(M2, cmap = 'magma')
plt.colorbar(im_1, cax = cbar_ax_1)
plt.colorbar(im_2, cax = cbar_ax_2)
plt.show()
If you want one colorbar only, it is wiser to normalize the unique colorbar based on both matrices' values:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize
from matplotlib import cm
M1 = np.random.rand(2, 2)
M2 = np.random.rand(2, 2)
fig, ax = plt.subplots(1, 2, figsize = (18, 8))
plt.subplots_adjust(right = 0.87)
cbar_ax = fig.add_axes([0.9, 0.1, 0.04, 0.8])
norm = Normalize(vmin = min(np.min(M1), np.min(M2)), vmax = max(np.max(M1), np.max(M2)))
cmap = cm.magma
im_1 = ax[0].imshow(M1, cmap = cmap)
im_2 = ax[1].imshow(M2, cmap = cmap)
plt.colorbar(cm.ScalarMappable(norm = norm, cmap = cmap), cax = cbar_ax)
plt.show()
Answered By - Zephyr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.