在python绘图中用d突出显示最大点

2024-09-30 01:22:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这个问题和很多其他回答的问题很接近,但是之前所有的回答都给了我同样的回溯问题。在

我有一个简单的时间序列,我试图突出最大点。 我在操作Pandas数据帧以获取用于在图形上绘制的最大y值时遇到问题。我想我快到了,但是我想pd.read_csv文件导入正在扰乱我的索引。在

导入数据集时,有一个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
        .......

取日平均值后:

^{pr2}$

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")

这张图是y轴上风速的平均值。 enter image description here

当我试图标注最大风速时,问题就来了。在

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列的方式有什么问题吗?非常感谢大家!在


Tags: csv数据instamp时间序列avg平均值
1条回答
网友
1楼 · 发布于 2024-09-30 01:22:41

排队

wind_avg = weather.wind_speed.resample('D').mean()

您将resample应用于Dataframe的wind_speed列中的单个Pandas系列,因此您将得到一个序列作为返回值:

^{pr2}$

试试看

weather_avg = weather.resample('D').mean()
type(weather_avg)
Out: pandas.core.frame.DataFrame

你可以每天重新采样整个天气数据集。在

相关问题 更多 >

    热门问题