从Pandas数据框中删除时间戳,日期和月份以获取时间序列图

时间:2018-08-16 20:03:08

标签: python pandas ticker

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'])

我想绘制两个时间序列的两个数据帧。我希望时间序列相互叠加,而我正努力为熊猫数据帧工作。此外,我在使用x-tickers时遇到问题。

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

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

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

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()

输出是我想要的图形,但是x-ticker的格式是year-month-date-time。在此示例中,我只希望“年” 2004、2005、2006、2007显示为行情指示器,更重要的是,它们要正确缩放(因此2005行情指示器将接近“ 2005-01-01”数据点)。这可能吗?

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

pytest

2 个答案:

答案 0 :(得分:1)

首先,您应该转到#include <iostream> #include <string> using String = std::string; class Entity { private: String my_Name; public: Entity() : my_Name("new name"){} Entity(const String& name) : my_Name(name){} const String& GetName() const {return my_Name;} }; int main() { std::cin.get() } 中的面向对象的API。这就是我将在其余答案中使用的方式。

matplotlib

答案 1 :(得分:0)

让我们在xaxis和set_major_formatter上使用set_major_locatorplt.subplots

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()

输出:

enter image description here