0
當我從csv文件讀入數據時,我使用pd.to_datetime(df.date)
將我的日期信息從字符串轉換爲pandas.tslib.Timestamp
。從這裏,我可以積的數據,並得到類似以下內容:如何在繪製時間戳時包含2個特定日期和刻度?
我有兩個問題:
- 的日期必須轉動,這是欺騙。看起來這個大幅上漲發生在2014年12月,實際上,這發生在次年2月。
- 巨大峯值的日期具有相對重要性,而其他日期(2014年12月除外)不是。
我想:
- 能夠撰寫月份下方的一年,以更經濟,空間,或在x軸上添加刻度,以便它是明確的,其中xticks指向。
- 對我的xticks更加保守,並確保2014年12月和2015年2月被包括爲蜱總是。
我知道熊貓不能很好地與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')
你有能重現您的問題一些示例數據?我試圖複製它,但索引打印正常。 – Alexander
https://drive.google.com/file/d/0BzmnSPzMM_U_OHFncXRBLWpGcXc/view?usp=sharing這裏是一個鏈接到csv文件 – user3600497
我應該提一下,這個數據只適用於黑色曲線。我故意旋轉日期,以便可以閱讀。 – user3600497