2015-11-29 35 views
0

當我從csv文件讀入數據時,我使用pd.to_datetime(df.date)將我的日期信息從字符串轉換爲pandas.tslib.Timestamp。從這裏,我可以積的數據,並得到類似以下內容:如何在繪製時間戳時包含2個特定日期和刻度?

enter image description here

我有兩個問題:

  1. 的日期必須轉動,這是欺騙。看起來這個大幅上漲發生在2014年12月,實際上,這發生在次年2月。
  2. 巨大峯值的日期具有相對重要性,而其他日期(2014年12月除外)不是。

我想:

  1. 能夠撰寫月份下方的一年,以更經濟,空間,或在x軸上添加刻度,以便它是明確的,其中xticks指向。
  2. 對我的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') 
+0

你有能重現您的問題一些示例數據?我試圖複製它,但索引打印正常。 – Alexander

+0

https://drive.google.com/file/d/0BzmnSPzMM_U_OHFncXRBLWpGcXc/view?usp=sharing這裏是一個鏈接到csv文件 – user3600497

+0

我應該提一下,這個數據只適用於黑色曲線。我故意旋轉日期,以便可以閱讀。 – user3600497

回答

1

這不是一個答案,但我需要將它張貼這樣使您可以查看圖形。鑑於從上面的鏈接和下面的轉換你的數據,這是我的圖表:

df = pd.read_csv(file_location...) 
df.set_index('date', inplace=True) 
df.index = pd.to_datetime(df.index) 
df.plot() 

enter image description here

+0

甜,這幾乎就是我想要的。現在,我怎樣才能將2015年2月和2014年12月添加到ticls中 – user3600497

相關問題