Matplotlib.pyplot 右侧的子图被切断

Matplotlib.pyplot subplot is being cut off on right side

我非常接近最终完成我的卷积神经网络项目,但我很难尝试显示一个显示给定新交通标志的前 k class化概率的图图片。我想在左侧显示概率的散点图,在右侧显示相关图像。这是我定义的函数:

def plot_probability_per_class(probabilities, classes, image):
    figure = plt.figure()
    figure.subplots_adjust(right=1.5, hspace=0.5)

    subplot = figure.add_subplot(1,2,1)
    subplot.set_title('Probability per class')
    plt.plot(classes, probabilities, 'ro')  
    plt.ylabel('Probability')
    plt.xlabel('Class ID')

    subplot = figure.add_subplot(1,2,2)
    subplot.set_title('Image')
    plt.imshow(image)

    plt.show()

下面是生成的图形示例:

在我提问的同时,有人能告诉我如何在散点图的右侧(或下方)放置图例以显示 class ID 的描述。类似于以下内容:

  1. =限速(20km/h)
  2. =限速(30km/h)
  3. =限速(50km/h)
  4. =限速(60km/h)
  5. =限速(70km/h)
  6. =限速(80km/h)
  7. = 限速结束(80km/h)
  8. =限速(100km/h)
  9. =限速(120km/h)
  10. =没有通过
  11. = 超过 3.5 公吨的车辆禁止超车

我是 Matplotlib 的新手,我发现 API 文档难以阅读。任何帮助将不胜感激。

从我的代码中删除以下行修复了截止问题:

figure.subplots_adjust(right=1.5, hspace=0.5)

此外,使用 text box 就可以为情节添加注释。再次感谢@wflynny 和@ImportanceOfBeingEarnest 的反馈