【问题标题】:Stacked bins for 4 labels : only two labels appear4 个标签的堆叠箱:仅出现两个标签
【发布时间】:2017-09-05 08:49:43
【问题描述】:

我有 7 个箱子的堆叠箱,它们是:

labels = ['Digits', 'digits+spec-char', 'letters', 'letters+digit','letters+special-char','All-alphabet','All-alphabet(lower+upper)']

对于 4 个标签,它们是:

'CRNN', 'CRNN+transfer learning','CRNN+Image aug','CRNN+ Img Aug+ transfer learning'

我的情节有效,但仅显示 CRNN+transfer learningCRNN+ Img Aug+ transfer learning 的垃圾箱,如下图所示。

这是我的代码:

def bins_accuracy():
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.ticker import FormatStrFormatter



    N = 7
    fig, ax = plt.subplots()

    ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
    CRNN = (94.52,93.27, 94.34, 94.51,93.43,94.26,90)

    CRNN_transfer = (96.40,96.61,97.40,96.99,93.50,95.69,92.65)
    CRNN_img_aug=(0,0,0,0,0,94,92)
    CRNN_img_aug_transfer=(0,0,0,0,0,95,93)


    ind = np.arange(N)  # the x locations for the groups
    width = 0.35  # the width of the bars: can also be len(x) sequence
    labels = ['Digits', 'digits+spec-char', 'letters', 'letters+digit','letters+special-char','All-alphabet','All-alphabet(lower+upper)']
    p1 = plt.bar(ind, CRNN, width)
    p2 = plt.bar(ind, CRNN_transfer, width)
    p3 = plt.bar(ind, CRNN_img_aug, width)
    p4 = plt.bar(ind, CRNN_img_aug_transfer, width)

    plt.ylabel('Accuracy %')
    plt.title('Accuracy by group')
    plt.xticks(ind, ('Digits', 'digits+spec-char', 'letters', 'letters+digit','letters+special-char','All-alphabet','All-alphabet(lower+upper)'))
    plt.ylim(ymax=99, ymin=91)
    plt.legend((p1[0], p2[0],p3[0],p4[0]), ('CRNN', 'CRNN+transfer learning','CRNN+Image aug','CRNN+ Img Aug+ transfer learning'))

    plt.show()

我想在我的情节中看到四个组的堆叠箱 'CRNN', 'CRNN+transfer learning','CRNN+Image aug','CRNN+ Img Aug+ transfer learning' 所以 4 种颜色,而不是只有两种

EDIT1 我添加后得到以下内容:

OCR_Engine = (97.12, 97.68, 96.76, 96.64, 96.30, 96.51, 96.11) p5 = plt.bar(ind + 4 * width, OCR_Engine, width)

谢谢

【问题讨论】:

  • 您必须提供有关数据格式的更多信息。现在,如果您要堆叠条形图,则这些值加起来不会达到 100%。例如,第一个柱是 94.52+96.40+0+0 = 190.92%?
  • @DizietAsahi,每个值都代表准确度,以 % 为单位。
  • 我知道,但是如果你把这些条叠在一起,它们的高度加起来会超过 100%

标签: python matplotlib histogram stacked-chart


【解决方案1】:

目前,您将条形直接绘制在彼此之上。您可以通过设置bottom 关键字来堆叠条形:

import numpy as np
import matplotlib.pyplot as plt

N=7

fig, ax = plt.subplots()

CRNN = np.array([94.52,93.27, 94.34, 94.51,93.43,94.26,90])
CRNN_transfer = np.array([96.40,96.61,97.40,96.99,93.50,95.69,92.65])
CRNN_img_aug = np.array([0,0,0,0,0,94,92])
CRNN_img_aug_transfer= np.array([0,0,0,0,0,95,93])

ind = np.arange(N)  # the x locations for the groups
width = 0.35  # the width of the bars: can also be len(x) sequence
p1 = plt.bar(ind, CRNN, width)
p2 = plt.bar(ind, CRNN_transfer, width, bottom=CRNN)
p3 = plt.bar(ind, CRNN_img_aug, width, bottom=CRNN+CRNN_transfer)
p4 = plt.bar(ind, CRNN_img_aug_transfer, width, bottom=CRNN+CRNN_transfer+CRNN_img_aug)

plt.show()

编辑:

如果您希望条形图并排,您只需调整 x 位置即可。

# side-by-side
fig, ax = plt.subplots()
ind = np.arange(0, 2*N, 2)  # the x locations for the groups
p1 = plt.bar(ind, CRNN, width)
p2 = plt.bar(ind+width, CRNN_transfer, width)
p3 = plt.bar(ind+2*width, CRNN_img_aug, width)
p4 = plt.bar(ind+3*width, CRNN_img_aug_transfer, width)

【讨论】:

  • 嗨@Paul。谢谢,但 y 轴值最多应为 100%,因为值在 90% 和 100% 之间
  • 我告诉你如何堆叠酒吧。我无法修复你的数据。也许,您不想实际堆叠条形图,而是将它们并排绘制?
  • 我是这么认为的。对于这个问题,将它们并排放置更合适
  • 仅供参考:“堆叠条形图”是指条形相互重叠的图形。
  • 谢谢@Paul,它有效。最后一个问题,如果我想添加第五组 Engine = (97.12, 97.68, 96.76, 96.64, 96.30, 96.51, 96.11), p5 = plt.bar(ind + 4 * width, Engine, width)。我把这组和第四组分开,有一个空格
猜你喜欢
  • 2014-05-29
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
相关资源
最近更新 更多