【问题标题】:How can I do to plot an hist我该怎么做才能绘制历史
【发布时间】:2020-05-28 08:11:55
【问题描述】:

您好,我有一些日期,我正在尝试绘制一个历史。我的意思是我有一些日期,我想要一个显示每月频率的历史记录。这是我的代码:

import numpy as np
from datetime import datetime 
import matplotlib.pyplot as plt

dateslist = ["25/07/2019", "25/07/2019", 
"25/07/2019", "26/07/2019", "29/07/2019", 
"29/07/2019", "30/07/2019", "18/08/2019",  
"18/08/2019", "20/08/2019", "20/08/2019", 
"21/08/2019", "21/08/2019", "22/08/2019", 
"27/08/2019", "23/09/2019", "26/09/2019", 
"27/09/2019", "30/09/2019", "09/10/2019", 
"18/10/2019", "18/10/2019", "18/10/2019", 
"18/10/2019", "29/10/2019", "29/10/2019", 
"21/11/2019", "21/11/2019", "21/11/2019", 
"21/11/2019", "21/11/2019", "21/11/2019", 
"21/11/2019", "21/11/2019", "21/11/2019", 
"21/11/2019", "29/11/2019", "06/12/2019", 
"11/12/2019", "12/12/2019", "18/12/2019", 
"23/12/2019", "23/12/2019", "23/12/2019", 
"24/12/2019", "24/12/2019", "23/01/2020", 
"20/01/2020", "20/01/2020", "20/01/2020", 
"19/02/2020", "31/01/2020", "24/02/2020", 
"25/02/2020", "25/02/2020", "02/03/2020", 
"18/03/2020", "19/03/2020", "23/03/2020", 
"02/04/2020", "14/04/2020", "21/04/2020", 
"24/04/2020", "20/05/2020"]

dateslistconverted = []

for datelist in dateslist:
    if datetime.strptime(datelist, "%d/%m/%Y").month in range(1,6):
        dateslistconverted.append(datetime.strptime(datelist, "%d/%m/%Y").month+12)
    else: 
        dateslistconverted.append(datetime.strptime(datelist, "%d/%m/%Y").month)

plt.hist(dateslistconverted)
plt.show()

但我没有合适的格式,我希望日期按时间排序,并在不同月份之间留一些空格。

我该怎么办?

非常感谢!

【问题讨论】:

    标签: python python-3.x matplotlib histogram


    【解决方案1】:

    您可以使用 pandas,因为它可以很好地处理日期时间,您也可以直接绘制它:

    pd.Series(pd.to_datetime(dateslist)).apply(lambda x: x.strftime("%Y-%m")).value_counts().sort_index().plot(kind='bar')
    

    在这里,我刚刚将您的列表转换为日期时间格式并将其包装成一个系列,以便我可以调整日期格式。然后我计算每个月的值,对其进行排序并绘制它。

    import pandas as pd
    
    dateslist = ["25/07/2019", "25/07/2019", 
    "25/07/2019", "26/07/2019", "29/07/2019", 
    "29/07/2019", "30/07/2019", "18/08/2019",  
    "18/08/2019", "20/08/2019", "20/08/2019", 
    "21/08/2019", "21/08/2019", "22/08/2019", 
    "27/08/2019", "23/09/2019", "26/09/2019", 
    "27/09/2019", "30/09/2019", "09/10/2019", 
    "18/10/2019", "18/10/2019", "18/10/2019", 
    "18/10/2019", "29/10/2019", "29/10/2019", 
    "21/11/2019", "21/11/2019", "21/11/2019", 
    "21/11/2019", "21/11/2019", "21/11/2019", 
    "21/11/2019", "21/11/2019", "21/11/2019", 
    "21/11/2019", "29/11/2019", "06/12/2019", 
    "11/12/2019", "12/12/2019", "18/12/2019", 
    "23/12/2019", "23/12/2019", "23/12/2019", 
    "24/12/2019", "24/12/2019", "23/01/2020", 
    "20/01/2020", "20/01/2020", "20/01/2020", 
    "19/02/2020", "31/01/2020", "24/02/2020", 
    "25/02/2020", "25/02/2020", "02/03/2020", 
    "18/03/2020", "19/03/2020", "23/03/2020", 
    "02/04/2020", "14/04/2020", "21/04/2020", 
    "24/04/2020", "20/05/2020"]
    
    pd.Series(pd.to_datetime(dateslist)).apply(lambda x: x.strftime("%Y-%m")).value_counts().sort_index().plot(kind='bar');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多