在 matplotlib 中设置分组条形图之间的间距 [英] setting spacing between grouped bar plots in matplotlib

查看:35
本文介绍了在 matplotlib 中设置分组条形图之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照图库中的示例在 matplotlib 中制作分组条形图.我使用以下内容:

I'm trying to make a grouped bar plot in matplotlib, following the example in the gallery. I use the following:

import matplotlib.pyplot as plt
plt.figure(figsize=(7,7), dpi=300)
xticks = [0.1, 1.1]
groups = [[1.04, 0.96],
          [1.69, 4.02]]
group_labels = ["G1", "G2"]
num_items = len(group_labels)
ind = arange(num_items)
width = 0.1
s = plt.subplot(1,1,1)
for num, vals in enumerate(groups):
    print "plotting: ", vals
    group_len = len(vals)
    gene_rects = plt.bar(ind, vals, width,
                         align="center")
    ind = ind + width
num_groups = len(group_labels)
# Make label centered with respect to group of bars
# Is there a less complicated way?
offset = (num_groups / 2.) * width
xticks = arange(num_groups) + offset
s.set_xticks(xticks)
print "xticks: ", xticks
plt.xlim([0 - width, max(xticks) + (num_groups * width)])
s.set_xticklabels(group_labels)

我的问题是:

  1. 如何控制条形组之间的间距?现在间距很大,看起来很傻.请注意,我不想使条形变宽 - 我希望它们具有相同的宽度,但要靠得更近.

  1. How can I control the space between the groups of bars? Right now the spacing is huge and it looks silly. Note that I do not want to make the bars wider - I want them to have the same width, but be closer together.

我怎样才能让标签在条形组下方居中?我试图想出一些算术计算来将 xlabels 定位在正确的位置(见上面的代码),但它仍然有点偏离......感觉有点像编写绘图库而不是使用绘图库.如何解决这个问题?(是否有用于 matplotlib 的包装器或内置实用程序,这是默认行为?)

How can I get the labels to be centered below the groups of bars? I tried to come up with some arithmetic calculations to position the xlabels in the right place (see code above) but it's still slightly off... it feels a bit like writing a plotting library rather than using one. How can this be fixed? (Is there a wrapper or built in utility for matplotlib where this is default behavior?)

回复@mlgill:感谢您的回答.您的代码当然更优雅,但仍然存在相同的问题,即条的宽度和组之间的间距不是单独控制的.您的图表看起来正确,但条形图太宽了——它看起来像 Excel 图表——我想让条形图变细.

Reply to @mlgill: thank you for your answer. Your code is certainly much more elegant but still has the same issue, namely that the width of the bars and the spacing between the groups are not controlled separately. Your graph looks correct but the bars are far too wide -- it looks like an Excel graph -- and I wanted to make the bar thinner.

宽度和边距现在已链接,所以如果我尝试:

Width and margin are now linked, so if I try:

margin = 0.60
width = (1.-2.*margin)/num_items

它使酒吧更瘦,但使小组分开,因此情节再次看起来不正确.

It makes the bar skinnier, but brings the group far apart, so the plot again does not look right.

如何制作一个带两个参数的分组条形图函数:每个条形的宽度和条形组之间的间距,并像您的代码一样正确地绘制它,即 x 轴标签位于团体?

How can I make a grouped bar plot function that takes two parameters: the width of each bar, and the spacing between the bar groups, and plots it correctly like your code did, i.e. with the x-axis labels centered below the groups?

我认为由于用户必须计算特定的低级布局数量,例如边距和宽度,我们基本上仍然在编写绘图库 :)

I think that since the user has to compute specific low-level layout quantities like margin and width, we are still basically writing a plotting library :)

推荐答案

其实我觉得这个问题最好通过调整figsizewidth来解决;这是我使用 figsize=(2,7)width=0.3 的输出:

Actually I think this problem is best solved by adjusting figsize and width; here is my output with figsize=(2,7) and width=0.3:

顺便说一句,如果您使用 pandas 包装器(我也导入了 seaborn,而不是解决方案所必需的,但在我看来使情节更漂亮,更现代):

By the way, this type of thing becomes a lot simpler if you use pandas wrappers (i've also imported seaborn, not necessary for the solution, but makes the plot a lot prettier and more modern looking in my opinion):

import pandas as pd        
import seaborn 
seaborn.set() 

df = pd.DataFrame(groups, index=group_labels)
df.plot(kind='bar', legend=False, width=0.8, figsize=(2,5))
plt.show()

这篇关于在 matplotlib 中设置分组条形图之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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