gpt4 book ai didi

python - 在 matplotlib 中分离两组条形图

转载 作者:行者123 更新时间:2023-12-01 08:46:38 25 4
gpt4 key购买 nike

您知道是否可以将条形图分成两组不同大小的组,但将它们保留在同一图中?我有这个代码:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

objects = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
y_pos = np.arange(len(objects))
performance = [15.3, 25.8, 37.1, 50.0, 15.0, 18.5, 28.9]
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Reduction Error')
plt.title("")

plt.show()

我想让 A 和 B 靠近在一起,然后是一些空间,然后是所有其他条。我发现这个问题Function to create grouped bar plot ,但我想将每个名称保留在栏下,而不是像示例中那样将它们分组。感谢您的帮助!

最佳答案

如果我理解正确的话,你可以这样做:

objects = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
x = [1,1.8,5,6,7,8,9]
performance = [15.3, 25.8, 37.1, 50.0, 15.0, 18.5, 28.9]

plt.bar(x, performance, align='center', alpha=0.5)
plt.xticks(x, objects)
plt.ylabel('Reduction Error')
plt.title("")
plt.show()

enter image description here

或者在 x 中使用 2 而不是 1.8,以便在 AB 之间留出一些空间

关于python - 在 matplotlib 中分离两组条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53278838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com