【问题标题】:Python: Plotting Slices of Time SeriesPython:绘制时间序列切片
【发布时间】:2016-02-29 07:18:19
【问题描述】:

我有一个两列数据集,一个包含 1990-2015 的每日日期,另一个包含电力输出。我想在一个折线图上绘制特定年份的功率输出、整个数据集的日平均电输出和围绕该平均值的 +-1 标准误差。但是,就目前而言,我得到一个均值图,然后是每年 1 个图(即 8 个单独的图)。我正在尝试在 Pandas 中执行此操作,但在 NumPy 或 Pandas 中的帮助会很棒。

我的数据集和当前代码如下:

# Dummy data since the actual file is very large.
dates = pd.date_range(start='01-01-1990',end='12-31-2015',freq='D')
vols = np.random.random_integers(0,60000,9496)
full = np.array([dates,vols]).T

yrs = [1990,1994,1998,2002,2006,2010,2014]
mons = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

data = pd.DataFrame(full,names=['date','elec'])
data['yr'] = pd.DatetimeIndex(data['date']).year
data['mon'] = pd.DatetimeIndex(data['date']).month
data['day'] = pd.DatetimeIndex(data['date']).day

mean = data.groupby(['mon','day']).mean().reset_index()
std  = data.groupby(['mon','day']).std().reset_index()

power = data[data['yr'].isin(yrs)]
pwryear = power.groupby('yr')

fig, ax1 = plt.subplots(figsize = (14,5))

ax1.plot(mean.elec,'k-',lw=4)
pwryear.plot()
ax1.set_title('Merrimack River floods at Lowell, MA')
ax1.set_ylabel('Discharge [m$^{3}$ s$^{-1}$]')
ax1.set_xticks([0,30,59,90,120,151,181,212,243,273,304,334])
ax1.set_xticklabels(mons)
ax1.set_xlim(0,365)
ax1.grid()
ax1.legend(loc=2)

【问题讨论】:

    标签: python numpy pandas matplotlib plot


    【解决方案1】:

    尝试将pwryear = power.groupby('yr') 替换为:

    # create a column that includes month and day, but not year
    # this will be the x-axis
    data['mon_day'] = data.date.apply(lambda x: x.strftime('%m-%d'))
    # transform your dataframe to have one column per year
    pwryear = power[['mon_day', 'elec', 'yr']].set_index(['mon_day', 'yr']).unstack(1)
    pwryear.columns = pwryear.columns.droplevel(0)
    

    现在,当您运行pwryear.plot() 时,您每年将获得一条线路。

    要获得 std 的阴影,您需要更改

    mean = data.groupby(['mon','day']).mean().reset_index()
    std  = data.groupby(['mon','day']).std().reset_index()
    

    mean = data.groupby('mon_day').mean()
    std  = data.groupby('mon_day').std()
    

    然后,做:

    ax1.fill_between(mean.index, mean.elec - std.elec, mean.elec + std.elec)
    

    希望有帮助!

    【讨论】:

    • 不过,我确实想要年度地块。我只是不希望它们都分开。我希望它们与具有相关标准偏差的年度每日平均值位于同一图表上。
    • 好的。然后,您需要将每一年变成同一数据框中的单独列。你可以使用一些 set_index() 和 unstack() 魔法来做到这一点。我会更新我的答案。
    • 那将不胜感激!谢谢!
    • 这一切都很好,除了 mon_day 以数字字母(?)顺序排列,这意味着索引 0 与 1/1 相关联,但索引 1 是 1/10,索引 2 是 1/11等等等等
    • 糟糕。现在已通过正确填充零来纠正它。
    【解决方案2】:

    我很确定 pandas 图形只是 Matplotlib 的一个包装。如果是这种情况,您可以将您的系列作为 x 坐标和 y 坐标的列表传递,(交替)在同一个图上渲染多个系列。

    http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot

    【讨论】:

    • 我已经尝试过这样的事情,但是 x 轴最终成为了 'elec' 的索引位置,并且因为这些年份如此分散,它会绘制 1990 年,然后是大量91、92、93 和 1994 等的空格。
    • 您可能需要调整数据集以在每个时间段内具有相同的大致坐标数。看看 pandas.DataFrame.resample() 是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-09-20
    • 2016-09-13
    • 1970-01-01
    • 2016-05-07
    • 2017-09-28
    • 2015-06-09
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多