Python Matplotlib:如何在条形之间获得空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25793731/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib: How to get space between bars?
提问by Tom
Hi I have started using matplotlib and have been trying to adapt the example code on the website to suit my needs. I have the code below which does what I want apart from the 3rd bar in each group overlaps the first of the next group of bars. Internet isnt good enough to add picture but any help would be great and if you could explain what my error is that would be appreciated.
嗨,我已经开始使用 matplotlib,并且一直在尝试调整网站上的示例代码以满足我的需求。我有下面的代码,除了每组中的第 3 个条与下一组条中的第一个重叠之外,它可以执行我想要的操作。互联网不足以添加图片,但任何帮助都会很棒,如果您能解释我的错误是什么,我们将不胜感激。
Thanks, Tom
谢谢,汤姆
"""
Bar chart demo with pairs of bars grouped for easy comparison.
"""
import numpy as np
import matplotlib.pyplot as plt
n_groups = 3
means_e1 = (20, 35, 30)
std_e1 = (2, 3, 4)
means_e2 = (25, 32, 34)
std_e2 = (3, 5, 2)
means_e3 = (5, 2, 4)
std_e3 = (0.3, 0.5, 0.2)
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor': '0.3'}
rects1 = plt.bar(index , means_e1, bar_width,
                 alpha=opacity,
                 color='b',
                 yerr=std_e1,
                 error_kw=error_config,
                 label='Main')
rects2 = plt.bar(index + bar_width + 0.1, means_e2, bar_width,
                 alpha=opacity,
                 color='r',
                 yerr=std_e2,
                 error_kw=error_config,
                 label='e2')
rects3 = plt.bar(index + bar_width + bar_width + 0.2, means_e3, bar_width,
                 alpha=opacity,
                 color='g',
                 yerr=std_e3,
                 error_kw=error_config,
                 label='e3')
plt.xlabel('Dataset type used')
plt.ylabel('Percentage of reads joined after normalisation to 1 million reads')
plt.title('Application of Thimble on datasets, showing the ability of each stitcher option.')
plt.xticks(index + bar_width + bar_width, ('1', '2', '3'))
plt.legend()
plt.tight_layout()
plt.show()
采纳答案by Shashank Agarwal
The bar_width + bar_width + 0.2is 0.9. Now you add another bar of bar_width(0.35), so overall you have 1.25, which is greater than 1. Since 1is the distance between subsequent points of the index, you have overlap. 
该bar_width + bar_width + 0.2是0.9。现在您添加另一个bar_width( 0.35) 条,因此总体上您有1.25,它大于1。由于1是索引的后续点之间的距离,因此您有重叠。
You can either increase the distance between index (index = np.arange(0, n_groups * 2, 2)), or reduce the bar width to something smaller, say 0.2.
您可以增加索引 ( index = np.arange(0, n_groups * 2, 2))之间的距离,或者将条形宽度减小到更小,例如0.2。

