python - 如何在python中创建分组条形图的子图

标签 python matplotlib

我想将多个分组的条形图组合成一个图形,如下图所示。
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试图治疗 fig1fig2作为新图形中的新轴。
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()
但它没有用。如何将多个分组的条形图组合成一个图形?
提前致谢。

最佳答案

嗯,我尝试了一些东西。这是一个粗略的结果。我唯一改变的是,我只是使用轴,随着时间的推移我只是使用子图。因此,以 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()
这是您的初步结果:
enter image description here
当您看到结果并自己尝试一些东西时,让我尝试向其中添加标签和图例,以及您在示例图像中提供的内容。
编辑:最终输出
所以这就是您正在寻找的确切内容:
enter image description here
代码:
导入 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()

关于python - 如何在python中创建分组条形图的子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65230006/

相关文章:

python - 如何计算列表值在不同数据框的列中的出现次数?

python - django CheckboxSelectMultiple - 如何添加滚动条?

python - 使用 Flask REST API 将三个参数传递给 MySQL

python - 将 matplotlib 保存为最终给定大小(包括标题)

python - 关于在动画期间更改分散标记的问题

python - Matplot Numpy 值错误 : setting an array element with a sequence

python - 如何在 tensorflow 中使用 QueueRunner 将动态创建的输入图像添加到 RandomShuffleQueue

Python:使用命名空间解析 SVG/XML

python - 仅访问嵌套字典列表中的一个键并使用 matplotlib 绘制它

python - 如何获取多个峰值下的面积值