Python matplotlib使直方图填充打印区域

Python matplotlib使直方图填充打印区域,python,matplotlib,histogram,Python,Matplotlib,Histogram,我正在使用matplotlib进行绘图,我的绘图区域比直方图面片占用的区域大,这让我很烦恼。我的问题是: 你看,我想去掉“23”块之间的空白,结束情节的右边界 这是我的密码: lines = (' '.join(line.strip().split()) for line in open(filename) if line.strip()) #prepare the date strings local_dates = ( pytz.utc.localize(datetime.datetime

我正在使用matplotlib进行绘图,我的绘图区域比直方图面片占用的区域大,这让我很烦恼。我的问题是:

你看,我想去掉“23”块之间的空白,结束情节的右边界

这是我的密码:

lines = (' '.join(line.strip().split()) for line in open(filename) if line.strip()) #prepare the date strings
local_dates = ( pytz.utc.localize(datetime.datetime.strptime(dt, "%Y-%m-%d %H:%M:%S.%f")).astimezone(localtz) for dt in lines )

local_dates = list(local_dates)

#plot by hour of day
bins = range(25)

n,bins,patches = plt.hist([ t.hour for t in local_dates], bins=bins )

xticks_pos = [0.5*patch.get_width() + patch.get_xy()[0] for patch in patches]

plt.xticks(xticks_pos, range(25),  ha='center')


plt.xlabel('Ora')
plt.ylabel('Numar de evenimente')

plt.title(filename, y=1.03)

fig = plt.gcf()
fig.set_size_inches(12,8)
plt.subplots_adjust(left=0, right=1, top=0.9, bottom=0.1)
plt.savefig(filename+'.by_hour.png', dpi=100,bbox_inches='tight')
plt.clf()

那么,我可以让我的面片拉伸整个绘图视口吗

您可以使用
plt.xlim
设置直方图x轴的限制,就像设置常规打印线一样。在你的例子中,我想,
plt.xlim(0,23.5)
上面的评论基本上是正确的,但我认为你可能希望限制为
0,24