2015-12-21 117 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