1. # coding=utf-8
    2. import pandas as pd
    3. from matplotlib import pyplot as plt
    4. file_path = "./PM2.5/BeijingPM20100101_20151231.csv"
    5. df = pd.read_csv(file_path)
    6. #把分开的时间字符串通过periodIndex的方法转化为pandas的时间类型
    7. period = pd.PeriodIndex(year=df["year"],month=df["month"],day=df["day"],hour=df["hour"],freq="H")
    8. df["datetime"] = period
    9. # print(df.head(10))
    10. #把datetime 设置为索引
    11. df.set_index("datetime",inplace=True)
    12. #进行降采样
    13. df = df.resample("7D").mean()
    14. print(df.head())
    15. #处理缺失数据,删除缺失数据
    16. # print(df["PM_US Post"])
    17. data =df["PM_US Post"]
    18. data_china = df["PM_Nongzhanguan"]
    19. print(data_china.head(100))
    20. #画图
    21. _x = data.index
    22. _x = [i.strftime("%Y%m%d") for i in _x]
    23. _x_china = [i.strftime("%Y%m%d") for i in data_china.index]
    24. print(len(_x_china),len(_x_china))
    25. _y = data.values
    26. _y_china = data_china.values
    27. plt.figure(figsize=(20,8),dpi=80)
    28. plt.plot(range(len(_x)),_y,label="US_POST",alpha=0.7)
    29. plt.plot(range(len(_x_china)),_y_china,label="CN_POST",alpha=0.7)
    30. plt.xticks(range(0,len(_x_china),10),list(_x_china)[::10],rotation=45)
    31. plt.legend(loc="best")
    32. plt.show()