Stacked bins for 4 labels :只出现两个标签

Stacked bins for 4 labels : only two labels appear

我堆放了 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()

我想在我的情节中看到的是 stacked bins for the four group '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)

谢谢

目前,您正在将条形图直接叠加在一起绘制。您可以通过设置 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)