2015-12-21 120 views
1

我有一个基于时间索引的数据框,另一列指出在那个小时发生碰撞时受伤的行人的数量。我正在尝试使用Matplotlib来绘制数据图,并成功地绘制了它,但是,范围太大了。我有一天的数据,并且想要使用最小值和最大值来计算相应的x轴限制。我尝试过使用plt.set_xlim,但出现错误:X轴散点图上的极限范围大熊猫MatplotLib

AttributeError: 'PathCollection' object has no attribute 'set_xlim'

如何正确设置限制?

图无极限
Graph without limits

df = test_request_pandas() 
df1 = df[['time','number_of_pedestrians_injured']] 
df1.set_index(['time'],inplace=True) 
df1 = df1.astype(float) 
min = df1.index.min() 
max = df1.index.max() 
fig = plt.scatter(df1.index.to_pydatetime(), df1['number_of_pedestrians_injured']) 
#fig.set_xlim([min, max]) // causing me issues 
plt.show() 
+1

的[如何更改x轴与MatPlotLib日期时间的范围是多少?(可能的复制http://stackoverflow.com/questions/21423158/how-do-i-change-the- x-axis-with-datetimes-in-matplotlib) –

回答

0

您可以创建轴,然后使用ax.xlim()

import pandas as pd 
df = pd.DataFrame({'x':[1,2,3], 'y':[3,4,5]}) 
fig = plt.figure() 
ax = plt.subplot(111) 
df.plot(x='x',y='y',ax=ax, kind='scatter') 
ax.set_xlim(2,3) 
+1

@IIya V. Schurov 111是什么意思? – Withoutahold

+0

它创建1行1列的“子图矩阵”。在这里看到细节:http://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111 –

+0

@IIya V. Schurov我得到“KeyError:time” – Withoutahold