首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在同一图形中绘制多个条形图

如何在同一图形中绘制多个条形图
EN

Stack Overflow用户
提问于 2016-06-27 09:00:42
回答 2查看 532关注 0票数 0

我想要为每个组绘制6个不同的条形图(AWA,Rem,S1,S2,SWS,stades)。有4个组。

我知道问题出在: fig,ax=plt.subplots()我该怎么解决这个问题呢?

代码语言:javascript
运行
复制
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline

    N = 4 # groups: EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)

    AWA = (99, 98, 98, 95)  #EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)

    ind = np.arange(N)  # the x locations for the groups
    width = 0.35       # the width of the bars

    fig, ax = plt.subplots()
    rects1 = ax.bar(ind, AWA, width, color='r')

    Rem = (100, 99, 97, 94)  #EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)
    rects2 = ax.bar(ind + width, Rem, width, color='y')

    S1 = (98, 97, 95, 93)  #EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)
    rects3 = ax.bar(ind + width, S1, width, color='b')

    S2 = (99, 99, 95, 92)  #EEG+EMG+EOG+twins(28), EEG+EMG+EOG(26), EEG+twins(22), EEG(20)
    rects4 = ax.bar(ind + width, S2, width, color='g')

    SWS = (99, 100, 95, 94)  #EEG+EMG+EOG+twins(28), EEG+EMG+EOG(26), EEG+twins(22), EEG(20)
    rects5 = ax.bar(ind + width, SWS, width, color='y')

    stades = (99, 98, 92, 86)  #EEG+EMG+EOG+twins(28), EEG+EMG+EOG(26), EEG+twins(22), EEG(20)
    rects6 = ax.bar(ind + width, stades, width, color='b')

    # add some text for labels, title and axes ticks
    ax.set_ylabel('Accuracy')
    ax.set_title('Accuracy by group - RF')
    ax.set_xticks(ind + width)
    ax.set_xticklabels(('G1', 'G2', 'G3', 'G4'))

    ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0], rects6[0]), ('AWA', 'Rem', 'S1', 'S2', 'SWS', 'stades'))

    plt.show()

EN

回答 2

Stack Overflow用户

发布于 2016-06-27 14:20:51

如果是你想要的子图,你可以这样做:

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt

N = 4 # groups: EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)

data = ((99, 98, 98, 95),
        (100, 99, 97, 94),
        (98, 97, 95, 93),
        (99, 99, 95, 92),
        (99, 100, 95, 94),
        (99, 98, 92, 86))
        #EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)

labels = ('AWA', 'Rem', 'S1', 'S2', 'SWS', 'stades')

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig = plt.figure()
fig.suptitle('Accuracy by group - RF')

for i in range(len(labels)):
    ax = plt.subplot(3,2,i+1)
    ax.bar(ind + width, data[i], width, color='y')
    ax.set_ylabel('Accuracy')
    ax.set_title(labels[i])
    ax.set_xticks(ind + width)
    ax.set_xticklabels(('G1', 'G2', 'G3', 'G4'))

plt.show()
票数 1
EN

Stack Overflow用户

发布于 2016-06-27 16:52:24

另一种方法是将所有的栏放在一起:

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt

N = 4 # groups: EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)

data = ((99, 98, 98, 95),
        (100, 99, 97, 94),
        (98, 97, 95, 93),
        (99, 99, 95, 92),
        (99, 100, 95, 94),
        (99, 98, 92, 86))
        #EEG_EMG_EOG_twins(28), EEG_EMG_EOG(26), EEG_twins(22), EEG(20)

labels = ('AWA', 'Rem', 'S1', 'S2', 'SWS', 'stades')
k = len(data)
colors = ('r', 'y', 'b', 'g', 'y', 'b')
ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

indshift = np.linspace(-width/2,width/2,k)

fig = plt.figure()
fig.suptitle('Accuracy by group - RF')
for i in range(k):
    plt.bar(ind + indshift[i], data[i], width/k, color=colors[i])
ax = gca()
ax.set_ylabel('Accuracy')
ax.set_xticks(ind + width)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4'))
plt.legend(labels)

plt.show()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38044593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档