2017-05-02 82 views
10

(這個問題可以單獨閱讀,但續集:Timeseries from CSV data (Timestamp and events)時間序列圖:X-標籤不變

我想可視化CSV數據(從2檔)如下所示,通過時間序列表示,使用python的熊貓模塊(請參閱下面的鏈接)。 DF1的

的樣本數據:

   TIMESTAMP eventid 
0 2017-03-20 02:38:24  1 
1 2017-03-21 05:59:41  1 
2 2017-03-23 12:59:58  1 
3 2017-03-24 01:00:07  1 
4 2017-03-27 03:00:13  1 

的「事件ID」列始終包含值1,和我試圖展現事件的總和,每天的數據集。 第2集,DF0,也有類似的結構,但只包含零:DF0的

的樣本數據:

   TIMESTAMP eventid 
0 2017-03-21 01:38:24  0 
1 2017-03-21 03:59:41  0 
2 2017-03-22 11:59:58  0 
3 2017-03-24 01:03:07  0 
4 2017-03-26 03:50:13  0 

x軸的標籤只顯示了相同的日期,我的問題是:如何才能顯示不同的日期? (是什麼原因導致在同一日至X標籤上顯示了多次?)

腳本至今:

import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker 

df1 = pd.read_csv('timestamp01.csv', parse_dates=True, index_col='TIMESTAMP') 
df0 = pd.read_csv('timestamp00.csv', parse_dates=True, index_col='TIMESTAMP') 

f, (ax1, ax2) = plt.subplots(1, 2) 
ax1.plot(df0.resample('D').size()) 

ax1.set_xlim([pd.to_datetime('2017-01-27'), pd.to_datetime('2017-04-30')]) 

ax1.xaxis.set_major_formatter(ticker.FixedFormatter 
(df0.index.strftime('%Y-%m-%d'))) 

plt.setp(ax1.xaxis.get_majorticklabels(), rotation=15) 

ax2.plot(df1.resample('D').size()) 
ax2.set_xlim([pd.to_datetime('2017-03-22'), pd.to_datetime('2017-04-29')]) 
ax2.xaxis.set_major_formatter(ticker.FixedFormatter(df1.index.strftime 
('%Y-%m-%d'))) 
plt.setp(ax2.xaxis.get_majorticklabels(), rotation=15) 
plt.show() 

輸出:(https://www.dropbox.com/s/z21koflkzglm6c3/figure_1.png?dl=0enter image description here

鏈接我一直奉行:

任何幫助深表感謝。

回答

7

製作的例子可重複性,我們可以創建以下的文本文件(data/timestamp01.csv):

TIMESTAMP;eventid 
2017-03-20 02:38:24;1 
2017-03-21 05:59:41;1 
2017-03-23 12:59:58;1 
2017-03-24 01:00:07;1 
2017-03-27 03:00:13;1 

(同爲data/timestamp00.csv)。然後,我們可以在

import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker 

df1 = pd.read_csv('data/timestamp01.csv', parse_dates=True, index_col='TIMESTAMP', sep=";") 
df0 = pd.read_csv('data/timestamp00.csv', parse_dates=True, index_col='TIMESTAMP', sep=";") 

閱讀繪製他們

enter image description here

這是所希望的描繪

f, (ax1, ax2) = plt.subplots(1, 2) 

ax1.plot(df0.resample('D').size()) 
ax2.plot(df1.resample('D').size()) 

plt.setp(ax1.xaxis.get_majorticklabels(), rotation=30, ha="right") 
plt.setp(ax2.xaxis.get_majorticklabels(), rotation=30, ha="right") 
plt.show() 

結果。

相關問題