简体   繁体   中英

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 .

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. 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). Because you have two axes using imshow , I would recommend using ImageGrid instead, as per this answer to a similar question .

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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