2014-11-06 35 views
0

說,我有兩個列表:的Python:簡介與同一x軸的曲線圖值

yvalues = [30, 40, -20, 0, -10, 20, 45, 12, -5, ....] 
Dates = ['20110103', '20110103', '20110103', '20110108', '20110108', '20110108', '20110113', '20110113', '20110113', ....] 

Dates第一項不對應於所述第一值在yvalues等。日期重複,因爲我每5天觀察多次yvalues

現在,如果我想與Dates繪製yvalues爲x軸,我做的:

plt.plot(yvalues) 
plt.xticks(dates) 

它給了我一個錯誤。如果我嘗試:plt.plot(Dates, yvalues),我得到這個討厭的圖表: enter image description here

我怎樣才能在x軸上繪製正確的日期值(即20110103),並沒有直線分開的觀察?

UPDATE

我不希望我的價值觀要在同一垂直線上的每一天,但一前一後繪製。事實上,每次觀察之間有5分鐘的時間差。我決定我的Dates使用列表中進行轉換:

Dates = [datetime.date(int(d[0:4]), int(d[4:6]), int(d[6:8])) for d in Dates] 

然後我做的:

plt.plot(dates, yvalues) 

,並得到了以下情節:

enter image description here

顯然,這張照片顯示的值同一日期在同一垂直線上。我仍然有令人厭煩的直線分隔每個日期。

現在,如果我不使用任何日期爲x軸,我得到如下圖(這是我想要的,但我想在x軸上的日期):enter image description here

樣品可用的數據集here

+0

所以你要對每一天的每個點之間的直線,但不是每一天之間,這一切都是相同的顏色?爲什麼不把它分成多個數據集? – will 2014-11-06 22:38:36

+0

什麼是實際數據?可能有更好的方式來將其可視化。 – will 2014-11-06 22:39:05

+0

@will我更新了我想要的問題的圖表,但是您會看到x軸上沒有日期,否則我會以前面的(帶空格的藍色條)圖表結束。我只是想在我的x軸圖上添加日期。我不需要在每個點之間有任何距離,但只要每天的每個點不出現在同一個垂直線上。 – Plug4 2014-11-06 23:32:59

回答

2

經過一番討論後,下面是我最終着陸的內容;

import datetime 
import random 
import numpy as np 
import datetime 
import itertools 


dates, allSpillovers, allBins, allDigitised = [], [], [], [] 
with open("year.dat") as year: 
    previousDate = None 
    spillovers = [] 
    for line in year.readlines()[1:]: 
    _, strdate, spillover = line.split(",") 
    spillover = float(spillover) 
    year, month, day = [int(i) for i in strdate.split("-")] 

    date = datetime.date(year, month, day) 

    if previousDate == date: 
     spillovers.append(spillover) 
    elif previousDate != None: 
     mean = np.mean(spillovers) 
     stdev = np.std(spillovers) 

     spillovers.sort() 
     if len(spillovers) > 70: 
      allSpillovers.append([mean, mean-stdev, mean+stdev] + spillovers) 
      dates.append(date) 
     spillovers = [] 

    previousDate = date 



#itertools.izip_longest(*allSpillovers, fillvalue=0) 
allSpillovers = zip(*allSpillovers) 


from matplotlib import pyplot 

print len(dates), len(allSpillovers[0]), len(allSpillovers[1]) 

fig = pyplot.figure() 
ax = fig.add_subplot(1,1,1) 


for i in range(3, len(allSpillovers)-1): 
    alpha = 0.5 - abs(i/float(len(allSpillovers)) - 0.5) 
    print len(dates), len(allSpillovers[i]), len(allSpillovers[i+1]) 
    ax.fill_between(dates, allSpillovers[i], allSpillovers[i+1], facecolor='green', interpolate=True, alpha=alpha, linewidth=0) 

#ax.fill_between(dates, allSpillovers[1], allSpillovers[2], facecolor='green', interpolate=True, alpha=0.5) 

#for b, d in bins, digitised: 



ax.plot(dates, allSpillovers[0], color="blue", linewidth=2) 
ax.plot(dates, [0 for _ in dates], color="red", linewidth=2) 
ax.grid() 

fig.autofmt_xdate() 

pyplot.show() 

enter image description here

enter image description here

+0

呵呵,你打敗我吧!我正在使用strptime,並且還包含了fig.autofmt_xdate()。 – Ajean 2014-11-06 20:13:47

+0

@很好的建議。我的問題應該更清楚。我不希望同一日期的值出現在同一條垂直線上,而是一個接一個地出現。事實上,每一天的觀察結果之間有5分鐘的距離。我會更新我的問題。 – Plug4 2014-11-06 21:18:44

+0

這太棒了!真的很感激@願意 – Plug4 2014-11-07 04:13:16

1

試試這個:

>>> from matplotlib import pyplot as plt 
>>> Dates = ['20110103', '20110103', '20110103', '20110108', '20110108', '20110108', '20110113', '20110113', '20110113'] 
>>> yvalues = [30, 40, -20, 0, -10, 20, 45, 12, -5] 
>>> x=range(len(Dates)) 
>>> plt.xticks(x,Dates) 
>>> plt.plot(x,yvalues) 
>>> plt.show() 

enter image description here