matplotlib:如何将多个条形与线组合 [英] matplotlib: How to combine multiple bars with lines

查看:59
本文介绍了matplotlib:如何将多个条形与线组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的分组条形图,其中有5组,每组4条.我使用了 matplotlib文档中的示例,它像这样:

I have a very simple grouped-bar chart with 5 groups of 4 bars each. I used the example from the matplotlib documentation, which goes like this:

import numpy as np
import matplotlib.pyplot as plt

ind = np.arange(5)
avg_bar1 = (81191,79318,57965,60557,14793)
avg_bar2 = (26826,26615,31364,31088,55472)
avg_bar3 = (36232,38038,38615,39014,40812)
avg_bar4 = (26115,25879,25887,28326,27988)

fig, ax = plt.subplots()

rects1 = ax.bar(ind, avg_bar1, 0.15, label='bar1')
rects2 = ax.bar(ind + 0.15, avg_bar2, 0.15, label='bar2')
rects3 = ax.bar(ind + 0.30, avg_bar3, 0.15, label='bar2')
rects4 = ax.bar(ind + 0.45, avg_bar4, 0.15, label='bar2')

plt.xlabel('Distributions')
plt.ylabel('ms')
plt.xticks(ind + 0.15, ('50/50', '60/40', '70/30', '80/20', '90/10'))
plt.legend()

plt.tight_layout()
plt.show()

问题

但是相应的其他组中的bar的某些值(例如,group1中的bar1和group2中的bar1,等等)彼此之间相差不大.

But some values of bars in the corresponding other groups (e.g. bar1 in group1 and bar1 in group2, etc.) do not differ that much from each other.

我想要的

所以我想添加行,以便更清楚地看到每个组的趋势.线条应从一组中每个条的顶部到相应其他组中的条的顶部.

So I want to add lines in order to see the trend of each group more clearly. The lines should go from the top of each bar in one group to the top of the bar in the corresponding other groups.

我在网络上找不到任何类似的内容.

I couldn't find anything similar on the web.

有可能吗?

推荐答案

感谢@arsho的输入.我使它更加紧凑.它也修复了代码中最后一组小节的错误.查看代码中的注释.希望这会有所帮助.

Thank you @arsho for your input. I have made it a little more compact. It fixed also the error on the last group of bars in your code. See comments in code. Hope this helps.

对于像matplotlib这样的新手来说,我们可以简单地在子图上绘制一条线,无论它是否已经包含条形图.

For those like me who are new to matplotlib: we can simple plot a line to the subplot, no matter if it already contains bars.

import numpy as np
import matplotlib.pyplot as plt

# fig, is the whole thing; ax1 is a subplot in the figure, 
# so we reference it to plot bars and lines there
fig, ax1 = plt.subplots()

ind = np.arange(3)
width = 0.15

# per dimension
colors = ['#00ff00', '#0000ff', '#ff00ff']
markers = ['x','o','v']
xticklabels = ['50/50', '60/40', '70/30']

# 
group1 = [12,6,5]
group2 = [6,8,12]
group3 = [2,4,9]

#
all_groups = [ group1, group2, group3 ]

# plot each group of bars; loop-variable bar_values contains values for bars
for i, bar_values in enumerate( all_groups ):

  # compute position for each bar
  bar_position = width*i
  ax1.bar( ind + bar_position, bar_values, width, color=colors[i] )

# plot line for each group of bars; loop-variable y_values contains values for lines
for i, y_values in enumerate( all_groups ):

  # moves the beginning of a line to the middle of the bar
  additional_space = (width*i) + (width/2);
  # x_values contains list indices plus additional space
  x_values = [ x + additional_space for x,_ in enumerate( y_values ) ]

  # simply plot the values in y_values
  ax1.plot( x_values, y_values, marker=markers[i], color=colors[i] )

plt.setp([ax1], xticks=ind + width, xticklabels=xticklabels)

plt.tight_layout()
plt.show()

这篇关于matplotlib:如何将多个条形与线组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆