熊猫X_axis每小时

时间:2018-02-23 12:12:33

标签: pandas matplotlib

我有这个小Pandascode:

graph = auswahl[['Volumenstrom_Außen', 'Vpunkt_Gesamt','Zuluft_Druck_10','Abluft_Druck_10']]
a = graph.plot(figsize=[50,10])
a.set(ylabel="m³/h", xlabel="Zeit", title="Volumenströme")#,ylim=[0,100])
a.legend(loc="upper left")
plt.show()

如何设置每小时显示的X轴?

数据框如下所示:

    Volumenstrom_Außen  Vpunkt_Gesamt   Zuluft_Druck    Abluft_Druck
Zeit                
2018-02-15 16:49:00     1021.708443     752.699     49.328  46.811
2018-02-15 16:49:15     1021.708443     752.699     49.328  46.811
2018-02-15 16:49:30     1021.708443     752.699     49.328  46.811
2018-02-15 16:49:45     1021.708443     752.699     49.328  46.811
2018-02-15 16:50:00     1021.708443     752.699     49.328  46.811
2018-02-15 16:50:15     1021.708443     752.699     49.328  46.811
2018-02-15 16:50:30     1021.708443     752.699     49.328  46.811
2018-02-15 16:50:45     1021.708443     752.699     49.328  46.811
2018-02-15 16:51:00     1092.171094     752.699     49.328  46.811
2018-02-15 16:51:15     1092.171094     752.699     49.328  46.811

1 个答案:

答案 0 :(得分:0)

让我们以此示例数据框为例,其索引为分钟粒度

import pandas as pd
import random
ts_index = pd.date_range('1/1/2000', periods=1000, freq='T')
v1 = [random.random() for i in range(1000)]
v2 = [random.random() for i in range(1000)]
v3 = [random.random() for i in range(1000)]
ts_df = pd.DataFrame({'v1':v1,'v2':v2,'v3':v3},index=ts_index)
ts_df.head()



                         v1           v2        v3
2000-01-01 00:00:00     0.593039    0.017351    0.742111
2000-01-01 00:01:00     0.563233    0.837362    0.869767
2000-01-01 00:02:00     0.453925    0.962600    0.690868
2000-01-01 00:03:00     0.757895    0.123610    0.622777
2000-01-01 00:04:00     0.759841    0.906674    0.263902

我们可以使用pandas.DataFrame.resample将此数据下采样为每小时粒度,如下所示

hourly_mean_df = ts_df.resample('H').mean() # you can use .sum() also
hourly_mean_df.head()

                          v1          v2         v3
2000-01-01 00:00:00     0.516001    0.461119    0.467895
2000-01-01 01:00:00     0.530603    0.458208    0.550892
2000-01-01 02:00:00     0.472090    0.522278    0.508345
2000-01-01 03:00:00     0.515713    0.486906    0.541538
2000-01-01 04:00:00     0.514543    0.478097    0.489217

现在您可以绘制每小时摘要

hourly_mean_df.plot()

Hourly Plot