matplotlib 如何在Python中创建分组条形图的子图

zqry0prt  于 2023-11-22  发布在  Python
关注(0)|答案(2)|浏览(149)

我想将多个分组条形图合并合并到一个图中,如下图所示。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()


但是它不起作用。我怎么能把几个分组的条形图合并成一个图形呢?
先谢了。

vaj7vani

vaj7vani1#

好吧,我尝试了一些东西。这是一个粗略的结果。我唯一改变的是,而不是使用轴,我只是使用子图,因为我学到了很多。所以与figaxes作为输出,一定有一种方法。但这是我曾经使用过的。我还没有添加图例和标题,但我想你也可以自己尝试。
下面是代码,只有很小的变化:

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()

字符串
这是初始结果:

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

编辑:最终输出

这就是你要找的东西



代码: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.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()

kyxcudwk

kyxcudwk2#

有两种方法来做子图,你可能会尝试合并意外:plt.subplotplt.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)

相关问题