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

问题描述 投票:1回答:1

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

Example of wanted output

python excel ggplot2 visualization tableau
1个回答
0
投票

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

   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位置。

© www.soinside.com 2019 - 2024. All rights reserved.