【问题标题】:How to create a figure of subplots of grouped bar charts in python如何在python中创建分组条形图的子图
【发布时间】:2020-12-10 18:13:24
【问题描述】:

我想将多个分组的条形图组合成一个图形,如下图所示。 grouped bar charts in a single figure

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

labels = ['G1', 'G2', 'G3']
yesterday_test1_mean = [20, 12, 23]
yesterday_test2_mean = [21, 14, 25]
today_test1_mean = [18, 10, 12]
today_test2_mean = [13, 13, 9]

首先我通过plt.subplots()创建了每个分组条形图

x = np.arange(len(labels))
width = 0.3

fig1, ax = plt.subplots()
rects1 = ax.bar(x-width/2, yesterday_test1_mean, width)
rects2 = ax.bar(x+width/2, yesterday_test2_mean, width)

fig2, ax = plt.subplots()
rects3 = ax.bar(x-width/2, today_test1_mean, width)
rects4 = ax.bar(x+width/2, today_test2_mean, width)

然后,我使用add_subplot 试图将fig1 和fig2 视为新图中的新轴。

fig_all = plt.figure()
fig1 = fig_all.add_subplot(1,2,1)
fig2 = fig_all.add_subplot(1,2,2)
fig_all.tight_layout()
plt.show()

但它没有用。如何将多个分组的条形图组合成一个图形?

提前致谢。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    嗯,我试过了。这是一个粗略的结果。我唯一改变的是,我只是使用随着时间的推移而学习的子图,而不是使用轴。所以以 fig 和 axes 作为输出,也一定有办法。但这就是我用过的全部。我还没有添加图例和标题,但我想你也可以自己尝试一下。

    以下是代码,只需稍作改动:

    import matplotlib.pyplot as plt
    import numpy as np
    
    labels = ['G1', 'G2', 'G3']
    yesterday_test1_mean = [20, 12, 23]
    yesterday_test2_mean = [21, 14, 25]
    today_test1_mean = [18, 10, 12]
    today_test2_mean = [13, 13, 9]
    
    x = np.arange(len(labels))
    width = 0.3
    
    plt.figure(figsize=(12,5))
    
    plt.subplot(121)
    plt.bar(x-width/2, yesterday_test1_mean, width)
    plt.bar(x+width/2, yesterday_test2_mean, width)    
    
    plt.subplot(122)
    plt.bar(x-width/2, today_test1_mean, width)
    plt.bar(x+width/2, today_test2_mean, width)
    
    plt.show()
    

    这是您的初步结果:

    当您看到结果并自己尝试一些东西时,让我尝试添加标签和图例,以及您在示例图像中提供的内容。

    编辑:最终输出

    这就是你要找的东西:

    代码: 将 matplotlib.pyplot 导入为 plt 将 numpy 导入为 np

    labels = ['G1', 'G2', 'G3']
    yesterday_test1_mean = [20, 12, 23]
    yesterday_test2_mean = [21, 14, 25]
    today_test1_mean = [18, 10, 12]
    today_test2_mean = [13, 13, 9]
    
    x = np.arange(len(labels))
    width = 0.3
    
    plt.figure(figsize=(12,5))
    
    plt.subplot(121)
    plt.title('Yesterday', fontsize=18)
    plt.bar(x-width/2, yesterday_test1_mean, width, label='test1', hatch='//', color=np.array((199, 66, 92))/255)
    plt.bar(x+width/2, yesterday_test2_mean, width, label='test2', color=np.array((240, 140, 58))/255)
    plt.xticks([0,1,2], labels, fontsize=15)
    
    
    plt.subplot(122)
    plt.title('Today', fontsize=18)
    plt.bar(x-width/2, today_test1_mean, width, hatch='//', color=np.array((199, 66, 92))/255)
    plt.bar(x+width/2, today_test2_mean, width, color=np.array((240, 140, 58))/255)
    plt.xticks([0,1,2], labels, fontsize=15)
    
    plt.figlegend(loc='upper right', ncol=1, labelspacing=0.5, fontsize=14, bbox_to_anchor=(1.11, 0.9))
    plt.tight_layout(w_pad=6)
    plt.show()
    

    【讨论】:

    • 这正是我想要的。我一直在努力寻找解决方案。非常感谢您的帮助。
    【解决方案2】:

    有两种方法可以制作子图,您可能会尝试不小心组合起来:plt.subplot 和 plt.subplots。

    这里是如何使用plt.subplots 创建两个条形图的示例。

    import numpy as np
    import matplotlib.pyplot as plt
    
    labels = ['G1', 'G2', 'G3']
    yesterday_test1_mean = [20, 12, 23]
    yesterday_test2_mean = [21, 14, 25]
    today_test1_mean = [18, 10, 12]
    today_test2_mean = [13, 13, 9]
    
    x = np.arange(len(labels))
    width = 0.3
    
    fig, (ax1, ax2) = plt.subplots(1, 2)
    ax1.bar(x-width/2, yesterday_test1_mean, width)
    ax1.bar(x+width/2, yesterday_test2_mean, width)
    
    ax2.bar(x-width/2, today_test1_mean, width)
    ax2.bar(x+width/2, today_test2_mean, width)
    
    plt.show()
    

    您的代码只有很少的变化。您可以添加标签:

    ax1.set_title('Yesterday')
    ax2.set_title('Today')
    
    ax1.set_xticks(x)
    ax1.set_xticklabels(labels)
    ax2.set_xticks(x)
    ax2.set_xticklabels(labels)
    

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多