两个混淆矩阵图有不同的大小
Two confusion matrix plots have different sizes
我试图在同一图像中绘制混淆矩阵,但它们的大小不同。
代码如下:
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()
这就是我得到的:
如何更改第二个绘图的大小?
另外要是能把多余的彩条删掉就好了
您应该定义需要放置颜色条的轴。您可以查看作为参考。
将这些概念应用到您的案例中会产生类似这样的结果:
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()
如果您只想要一个颜色条,明智的做法是根据两个矩阵的值对唯一颜色条进行归一化:
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()
我试图在同一图像中绘制混淆矩阵,但它们的大小不同。
代码如下:
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()
这就是我得到的:
如何更改第二个绘图的大小?
另外要是能把多余的彩条删掉就好了
您应该定义需要放置颜色条的轴。您可以查看
将这些概念应用到您的案例中会产生类似这样的结果:
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()
如果您只想要一个颜色条,明智的做法是根据两个矩阵的值对唯一颜色条进行归一化:
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()