简体   繁体   English

2个混淆矩阵,1个色条

[英]2 confusion matrix, 1 colorbar

I would like to produce two confusion matrix and only show one colorbar. 我想产生两个混淆矩阵,只显示一个色标。 I am basically trying to merge this scikit-learn code with this answer . 我基本上是试图将此scikit-learn代码此答案合并。

My code looks like this: 我的代码如下所示:

import numpy as np
import matplotlib.pyplot as plt


fig, axes = plt.subplots(nrows=1, ncols=2)
classes = ["A", "B"]
for i, ax in enumerate(axes.flat):
    cm = np.random.random((2,2))
    im = ax.imshow(cm, vmin=0, vmax=1)
    plt.title("Title {}".format(i))
    tick_marks = np.arange(2)
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], '.5f'),
                 horizontalalignment="center",
                 color="white")

    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.tight_layout()

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.88, 0.15, 0.05, 0.6])
fig.colorbar(im, cax=cbar_ax)

plt.show()

but this is the result: 但这是结果: 在此处输入图片说明

So everything is being plotted on the last image. 因此,所有内容都绘制在最后一张图像上。 Two questions: 两个问题:

  • how can I separate the two? 如何将两者分开?
  • how can i start the colorbar where the matrix start, even if it has no label? 我如何从矩阵开始的颜色条开始,即使它没有标签?

All your elements are plotted on the last image because you are mixing up the pyplot ( plt.xxxxx() ) interface with the object-oriented interface. 您将所有元素绘制在最后一张图像上,因为您将pyplotplt.xxxxx() )接口与面向对象的接口混合在一起。 Refer to this question or this one for some explanations. 请参考这个问题,或者这个对于一些解释。

For the colorbar, there are many ways to get a properly sized colorbar (eg playing with GridSpec , AxisDivider as suggested by @DavidG). 对于颜色条,有很多方法可以获取适当大小的颜色条 (例如,使用@DavidG建议的GridSpec和AxisDivider进行播放)。 Because you have two axes using imshow , I would recommend using ImageGrid instead, as per this answer to a similar question . 因为您有两个使用imshow轴,所以根据类似问题的答案 ,我建议您改用ImageGrid

Your code should read: 您的代码应为:

import itertools
from mpl_toolkits.axes_grid1 import ImageGrid


classes = ["A", "B"]

fig = plt.figure()
grid = ImageGrid(fig, 111,          # as in plt.subplot(111)
                 nrows_ncols=(1,2),
                 axes_pad=0.15,
                 cbar_location="right",
                 cbar_mode="single",
                 cbar_size="7%",
                 cbar_pad=0.15,
                 )


for i, ax in enumerate(grid[:2]):
    cm = np.random.random((2,2))
    im = ax.imshow(cm, vmin=0, vmax=1)
    ax.set_title("Title {}".format(i))  # ax.___ instead of plt.___
    tick_marks = np.arange(2)
    ax.set_xticks(tick_marks)  # Warning: different signature for [x|y]ticks in pyplot and OO interface
    ax.set_xticklabels(classes, rotation=45)
    ax.set_yticks(tick_marks)
    ax.set_yticklabels(classes)

    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        ax.text(j, i, format(cm[i, j], '.5f'),
                 horizontalalignment="center",
                 color="white")

    ax.set_ylabel('True label')
    ax.set_xlabel('Predicted label')

fig.tight_layout()
fig.subplots_adjust(right=0.8)
fig.colorbar(im, cax=ax.cax)

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM