gpt4 book ai didi

python - 如何在每个垂直条上添加多个水平条?

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:33 25 4
gpt4 key购买 nike

我想创建类似下图的图表,对于每个垂直条,我想将其前 10 个值添加为水平条>(不是线条),但我不知道应该使用哪些工具。

Example of wanted output

最佳答案

您应该能够将其他图指定为水平条形图,它们将显示在创建的第一个图的顶部。以下是我在垂直条形图上绘制水平条形图的方法。使用的数据弥补了这一点,因此图表不是很漂亮,但代码是相同的。

   N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
y_pos = [10, 20, 30, 40, 50] #locations of the horizontal bars
width = 0.35 # the width of the bars
h_width = 1.0 #width of the horizontal bars
h_x_loc = 1.18 #This will be used to change the x location of the
#horizontal bars.

p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.barh(y_pos, womenStd, width, left=h_x_loc, yerr=0)

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt.show()

请注意,要使每组水平条与正确的相应垂直条对齐,您需要为每个组绘制单独的图并更改该组的 x 位置。

关于python - 如何在每个垂直条上添加多个水平条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55909338/

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