【问题标题】:Removing timestamp, day and month from Pandas dataframe for a time series plot从 Pandas 数据框中删除时间序列图的时间戳、日期和月份
【发布时间】:2018-08-16 20:03:07
【问题描述】:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

我有两个数据框,我想为其绘制两个时间序列。我希望时间序列堆叠在一起,我正在努力为 Pandas 数据框工作。此外,我遇到了 x-tickers 的问题。

日期和时间以字符串形式给出,并且已经按时间顺序排列。这是数据样本(取自更大的数据集)

df1 = pd.DataFrame([["2004-03-01 00:00", 2.3],
              ["2004-03-05 00:00", 2.4],
              ["2004-03-25 00:00", 2.25],
              ["2004-07-01 00:00", 2.7],
              ["2005-01-01 00:00", 2.9],
              ["2005-02-17 00:00", 3.1],
              ["2005-12-01 00:00", 3.5],
              ["2006-02-01 00:00", 3.3],
              ["2006-04-05 00:00", 3.08],
              ["2006-08-22 00:00", 2.4],
              ["2007-07-01 00:00", 2.1]], columns = ['Date and Time', 'Values 1'])


df2 = pd.DataFrame([["2004-03-01 00:00", 12.3],
              ["2004-03-05 00:00", 14.5],
              ["2004-03-25 00:00", 12.1],
              ["2004-07-01 00:00", 10.0],
              ["2005-01-01 00:00", 12.1],
              ["2005-02-17 00:00", 9.3],
              ["2005-12-01 00:00", 8.1],
              ["2006-02-01 00:00", 6.5],
              ["2006-04-05 00:00", 7.5],
              ["2006-08-22 00:00", 6.4],
              ["2007-07-01 00:00", 4.1]], columns = ['Date and Time', 'Values 2'])

首先。如果我尝试绘制 df1,

df1.plot(x='Date and Time', y='Values 1',  legend=False)
plt.xlabel('Year')
plt.ylabel('Values 1')
plt.show()

输出是我想要的图表,但 x-ticker 的格式是年-月-日期-时间。在此示例中,我希望仅将“年”2004、2005、2006、2007 显示为代码,更重要的是,它们已正确缩放(因此 2005 代码将接近“2005-01-01”数据点)。这可能吗?

此外,我想绘制这些图堆叠在一起,我尝试了下面的代码无济于事。

plt.figure(1)

plt.subplot(211)
df1.plot(x='Date and Time', y='Values 1', legend=False)
plt.xlabel('Year')
plt.ylabel('Values 1')

plt.subplot(212)
df2.plot(x='Date and Time', y='Values 2', legend=False)
plt.xlabel('Year')
plt.ylabel('Values 2')

plt.show()

【问题讨论】:

    标签: python pandas ticker


    【解决方案1】:

    首先,您应该转到matplotlib 中的面向对象API。这将是我用于此答案的其余部分的内容。

    df1['Date and Time'] = pd.to_datetime(df1['Date and Time'])
    fig, ax = plt.subplots()
    df1.plot(x='Date and Time', y='Values 1', legend=False, ax=ax)
    plot_ticks = date_and_time.groupby(date_and_time.dt.year).first()
    ax.set_xticks(plot_ticks.values)
    ax.set_xticklabels(plot_ticks.dt.date.values)
    

    【讨论】:

      【解决方案2】:

      让我们在 xaxis 和 plt.subplots 上使用 set_major_formatterset_major_locator

      import pandas as pd
      import matplotlib.pyplot as plt
      import numpy as np
      import matplotlib.dates as mdates
      
      df1 = pd.DataFrame([["2004-03-01 00:00", 2.3],
                    ["2004-03-05 00:00", 2.4],
                    ["2004-03-25 00:00", 2.25],
                    ["2004-07-01 00:00", 2.7],
                    ["2005-01-01 00:00", 2.9],
                    ["2005-02-17 00:00", 3.1],
                    ["2005-12-01 00:00", 3.5],
                    ["2006-02-01 00:00", 3.3],
                    ["2006-04-05 00:00", 3.08],
                    ["2006-08-22 00:00", 2.4],
                    ["2007-07-01 00:00", 2.1]], columns = ['Date and Time', 'Values 1'])
      
      
      df2 = pd.DataFrame([["2004-03-01 00:00", 12.3],
                    ["2004-03-05 00:00", 14.5],
                    ["2004-03-25 00:00", 12.1],
                    ["2004-07-01 00:00", 10.0],
                    ["2005-01-01 00:00", 12.1],
                    ["2005-02-17 00:00", 9.3],
                    ["2005-12-01 00:00", 8.1],
                    ["2006-02-01 00:00", 6.5],
                    ["2006-04-05 00:00", 7.5],
                    ["2006-08-22 00:00", 6.4],
                    ["2007-07-01 00:00", 4.1]], columns = ['Date and Time', 'Values 2'])
      
      yearFmt = mdates.DateFormatter('%Y')
      years = mdates.YearLocator()  
      
      
      df1['Date and Time'] = pd.to_datetime(df1['Date and Time'])
      fig, ax = plt.subplots(2,1, figsize=(10,10))
      df1.plot(x='Date and Time', y='Values 1', legend=False, ax=ax[0])
      ax[0].set_xlabel('Year')
      ax[0].set_ylabel('Values 1')
      ax[0].xaxis.set_major_formatter(yearFmt)
      ax[0].xaxis.set_major_locator(years)
      
      
      df2['Date and Time'] = pd.to_datetime(df2['Date and Time'])
      df2.plot(x='Date and Time', y='Values 2', legend=False, ax=ax[1])
      ax[1].set_xlabel('Year')
      ax[1].set_ylabel('Values 2')
      ax[1].xaxis.set_major_formatter(yearFmt)
      ax[1].xaxis.set_major_locator(years)
      
      plt.tight_layout()
      plt.show()
      

      输出:

      【讨论】:

      • 嗨,这看起来很棒。虽然有什么理由为什么第一个图的 x 轴和 y 轴分别不是“Year”和“Values 1”?
      • 是的。刚刚解决了这个问题。我们需要用户轴方法set_xlabelset_ylabel
      猜你喜欢
      • 2018-02-02
      • 2022-11-30
      • 2014-09-28
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2017-02-01
      相关资源
      最近更新 更多