当我从csv文件中读取数据时,我使用pandas.tslib.Timestamp
将日期信息从字符串转换为pd.to_datetime(df.date)
。从这里,我可以绘制数据,并获得如下内容:
我有两个问题:
我想:
我理解pandas与Matplotlib日期功能不相符。如何在不必切换到numpy数组的情况下完成我想要的工作?
我的代码:
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
import seaborn as sns
palette = sns.color_palette()
plt.close('all')
s = pd.read_csv('all_tweets.csv')
t = pd.to_datetime(s.date)
fig = plt.figure(1)
ax = plt.gca()
plt.grid('off')
ax.set_axis_bgcolor('white')
ax.axhline(0, color = 'k')
ax.axvline(min(t), color = 'k')
plt.xlim([min(t), max(t)])
plt.plot(t,s.tweet, color = 'k', linewidth = 10, linestyle = '-', label = 'Tweets')
smoothed = pd.ewma(s.tweet, span = 20, adjust = False)
l, = plt.plot(t,smoothed, color = 'r', linewidth = 5, linestyle = '--',label = 'Smoothed')
l.set_dashes([10,6])
plt.xticks(fontsize = 30, rotation = 45)
plt.yticks(fontsize = 30)
plt.xlabel('Date', fontsize = 36,labelpad = 25)
plt.ylabel('Tweets Per Day', fontsize = 36,labelpad = 25)
figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
plt.savefig("all_tweets.png",bbox_inches='tight')