[英]Highlight maximum point point in python plot by date
我知道這個問題確實很接近其他許多已回答的問題,但是以前的所有答案都給了我同樣的追溯問題。
我有一個簡單的時間序列,我正在嘗試突出顯示最高點。 我在處理“熊貓數據框”以獲得在圖上繪制的最大y值時遇到問題。 我想我快要在那里了,但我認為pd.read_csv導入的parse_dates參數弄亂了我的索引編制。
導入數據集時,我有一個datetime列和一個wind_speed列。 當我對每日平均值進行重新采樣時,變量列的標題消失,而datetime列變得無法調用。
在獲取每日平均值之前:
In[12]: weather.head()
Out[12]: wind_speed
d_stamp_t_stamp
2017-07-26 00:05:09 1.31
2017-07-26 00:35:13 1.62
2017-07-26 01:05:05 1.50
.......
取每日平均值后:
wind_avg = weather.wind_speed.resample('D').mean()
d_stamp_t_stamp
2017-09-01 3.870625
2017-09-02 4.386875
2017-09-03 5.426739
2017-09-04 2.718750
2017-09-05 3.407708
wind_speed列的標簽消失了,我似乎再也無法對該數據進行采樣了。
這是到目前為止的時間序列的代碼:
## Import weather data.
weather = pd.read_csv('/Users/regina/university_projects/Themo_Data/Weather0717-0618.csv',
parse_dates=[[0,1]], index_col=0)
wind_avg = weather.wind_speed.resample('D').mean()
## Wind Speed graph
windplot = wind_avg.plot(title="Wind Speed", figsize=(12,8),
fontsize=12, marker='o', markersize=7)
windplot.set_xlabel("Date"),windplot.set_ylabel("Wind Speed in m/s")
當我嘗試注釋最大風速時,問題就來了。
y0 = max(wind_avg.wind_speed)
xpos = wind_avg.wind_speed.index(y0)
x0 = (wind_avg.d_stamp_t_stamp[xpos])
windplot.annotate(
"Max Speed", xy=(x0,y0), ha='right',
va='bottom', textcoords='offset points', bbox=dict(BoxStyle='Round, pad=0.5', fc='yellow',
alpha=0.5), arrowprops=dict(facecolor='black', shrink=0.05))
我收到這樣的屬性錯誤消息:
Traceback (most recent call last):
File "<ipython-input-15-5e45876c5ebc>", line 5, in <module>
y0 = max(wind_avg.wind_speed)
File "/Users/regina/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 4372, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'wind_speed'
我重新采樣wind_speed列以刪除其標簽的方式是否有問題? 非常感謝大家!
在行中
wind_avg = weather.wind_speed.resample('D').mean()
您將resample
應用於單個Pandas系列,該系列位於數據wind_speed
的wind_speed列中,因此您將獲得系列作為返回值:
type(wind_avg)
Out: pandas.core.series.Series
嘗試
weather_avg = weather.resample('D').mean()
type(weather_avg)
Out: pandas.core.frame.DataFrame
這樣您就可以每天對整個天氣數據集進行重新采樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.