Python matplotlib hist() 中条形之间的间距与数千个 bin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25394913/
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
Spacing between bars in matplotlib hist() with thousands of bins
提问by whymca
I'm making histograms using matplotlib's hist() function or bar(), and I want to use >10,000 bins (one bin to represent the counts at each coordinate of a large entity). Is there any way to create more whitespace between the vertical bars when I create the figure? Currently, there is no whitespace between each bar of the histogram. For example:
我正在使用 matplotlib 的 hist() 函数或 bar() 制作直方图,并且我想使用 >10,000 个 bin(一个 bin 表示大型实体的每个坐标处的计数)。创建图形时,有什么方法可以在垂直条之间创建更多空白?目前,直方图的每个条形之间没有空格。例如:
# imports
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import random
# Generating dummy data
coordinate_counts = []
for __ in range(1,100000) :
coordinate_counts.append(random.randrange(1,10000))
# plotting
fig, ax1 = plt.subplots()
ax1.hist(coordinate_counts,bins=range(1,10000))
fig.savefig('temp.png')
I've tried using rwidth() and varying the value of that, as well as tried using figsize() and simply expanding the size of the plot, but the final result always has each vertical bar next to eachother with no whitespace inbetween.
我试过使用 rwidth() 并改变它的值,也试过使用 figsize() 并简单地扩展绘图的大小,但最终结果总是让每个垂直条彼此相邻,中间没有空格。
回答by andere
The parameter rwidthspecifies the width of your bar relative to the width of your bin. For example, if your binwidth is say 1 and rwidth=0.5, the bar width will be 0.5. On both side of the bar you will have a space of 0.25.
该参数rwidth指定相对于 bin 宽度的条形宽度。例如,如果您的bin宽度为 1 和rwidth=0.5,则条宽将为0.5。在栏的两侧,您将有0.25的空间。
Mind: this gives a space of 0.5 between consecutive bars. With the number of bins you have, you won't see these spaces. But with fewer bins they do show up.
注意:这在连续柱之间提供了 0.5 的空间。随着您拥有的垃圾箱数量,您将看不到这些空间。但是随着垃圾箱的减少,它们确实出现了。



