2013-07-04 50 views
3

我正在繪製天氣數據的CSV文件,並且我在代碼中導入了它,但我試圖繪製它。下面是CSV數據的樣本:繪製來自Numpy陣列問題的日期

12:00am,171,6,7,52,76,77.1,63.7,28.74,0.00,0.00,0.0,0,63.7,78.1,67.4,56.0,29.96 
12:01am,192,4,6,52,76,77.1,63.7,28.74,0.00,0.00,0.0,0,63.7,78.1,67.4,56.0,29.96 
12:02am,197,3,6,52,76,77.1,63.7,28.74,0.00,0.00,0.0,0,63.7,78.1,67.4,56.0,29.96 
12:03am,175,3,6,52,76,77.1,63.7,28.73,0.00,0.00,0.0,0,63.7,78.1,67.4,56.0,29.96 
12:04am,194,4,6,52,76,77.1,63.7,28.73,0.00,0.00,0.0,0,63.7,78.1,67.4,56.0,29.96 
12:05am,148,5,6,52,76,77.1,63.7,28.73,0.00,0.00,0.0,0,63.7,78.1,67.4,56.0,29.96 

無論如何,我想是在X軸上的時間,但我不能讓它使用matplotlib繪製。我嘗試了一種使用xticks的方法,並繪製了我的y值,但就是這樣。它在我的X軸上給了我一條粗實線。

import matplotlib as mpl 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.cbook as cbook 
from matplotlib.dates import date2num 
import datetime as DT 
import re 

data = np.genfromtxt('FILE.csv', delimiter=',', dtype=None, skip_header=3) 
length = len(data) 

x = data['f0'] 
y = data['f7'] 

fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax1.set_title("Temperature")  
ax1.set_xlabel('Time') 
ax1.set_ylabel('Degrees') 


#plt.plot_date(x, y) 
plt.show() 
leg = ax1.legend() 

plt.show() 

我錯過了幾個關鍵部分,因爲我真的不知道從哪裏去。我檢查了我的numpy數組的數據類型,它一直說numpy.ndarray,我找不到一種方法將其轉換爲字符串或int值來繪圖。這是一個24小時的CSV文件,我希望每30分鐘左右打勾號。有任何想法嗎?

+0

[這個問題](http://stackoverflow.com/questions/ 1574088/plotting-time-in-python-with-matplotlib)是可能相關的。 –

+0

試過了,但我得到了一堆錯誤,它從不繪製或輸出數據。我試過這個:http://stackoverflow.com/questions/6974847/plot-with-non-numerical-data-on-x-axis-for-ex-dates,我在x軸上只有一條黑色的實線,可能是因爲有600個刻度線。我將如何改變這一點? – user2551677

+0

我已經成功地給了plt.plot()一個x座標的日期時間對象列表,然後是y值的浮動列表。我不確定從一個numpy數組中得到什麼方便的方法,或者如何真正控制刻度標記,但這至少可以給你一個圖表。 – seaotternerd

回答

1

那麼,這不是很優雅,但它的作品。關鍵是要更改存儲在x(它們只是字符串)到datetime對象中的時間,以便matploblib可以繪製它們。我已經完成了一個轉換功能,並將其命名爲get_datetime_from_string

**編輯的代碼是與Python 2.7兼容,將其轉換爲與之前單數的小時數次工作**

import matplotlib as mpl 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.cbook as cbook 
from matplotlib.dates import date2num 
import datetime as DT 
import re 

def get_datetime_from_string(time_string): 
    ''' Returns a datetime.datetime object 

     Args 
     time_string: a string of the form 'xx:xxam' 
     ''' 

    # there's got to be a better way to do this. 
    # Convert it to utf-8 so string slicing works as expected. 
    time_string = unicode(time_string, 'utf-8') 

    # period is either am or pm 
    colon_position = time_string.find(':') 
    period = time_string[-2:] 
    hour = int(time_string[:colon_position]) 
    if period.lower() == 'pm': 
     hour += 12 

    minute = int(time_string[colon_position + 1:colon_position + 3]) 

    return DT.datetime(1,1,1,hour, minute) 

data = np.genfromtxt('test.csv', delimiter=',', dtype=None, skip_header=3) 
length=len(data) 

x=data['f0'] 
y=data['f7'] 

datetimes = [get_datetime_from_string(t) for t in x] 

fig = plt.figure() 

ax1 = fig.add_subplot(111) 

ax1.set_title("Temperature")  
ax1.set_xlabel('Time') 
ax1.set_ylabel('Degrees') 

plt.plot(datetimes, y) 
leg = ax1.legend() 

plt.show() 

我一直得到絆倒了,因爲我試圖做time_string字符串的切片utf-8。在它給我的ASCII值或什麼之前。我不知道爲什麼轉換它有幫助,但它確實。

+0

當我將其添加到我的代碼時,出現錯誤:文件「metogram。py「,第22行,在get_datetime_from_string hour = int(time_string [:2]) ValueError:無效文字爲int()與基數10:'1:' – user2551677

+0

實現我犯了一個小錯誤,適應了我的,現在新的錯誤是Traceback(最近調用最後一次): 文件「metogram.py」,第36行,在 datetimes = [get_datetime_from_string(t)for t in x] 文件「metogram.py」,第20行,在get_datetime_from_string time_string = str(time_string,'utf-8') TypeError:str()最多隻需要1個參數(給出2個) – user2551677

+0

嘗試不進行轉換,換句話說,不用換行'time_string = str (time_string,'utf-8')'。 –

1

​​是一個非常有用的時間序列分析庫,並有一些基於matplotlib的繪圖功能。

Pandas在內部使用dateutil解析日期,但問題是日期不包含在您的文件中。在下面我的代碼假定,你會知道的日期解析文件之前(從文件名?)

In [125]: import pandas as pd 
In [126]: pd.options.display.mpl_style = 'default' 
In [127]: import matplotlib.pyplot as plt 

In [128]: class DateParser():           
    .....:  def __init__(self, datestring): 
    .....:   self.datestring = datestring 
    .....:  def get_datetime(self, time):  
    .....:   return dateutil.parser.parse(' '.join([self.datestring, time])) 
    .....:  

In [129]: dp = DateParser('2013-01-01') 

In [130]: df = pd.read_csv('weather_data.csv', sep=',', index_col=0, header=None, 
        parse_dates={'datetime':[0]}, date_parser=dp.get_datetime) 

In [131]: df.ix[:, :12] # show the first columns 
Out[131]: 
         1 2 3 4 5  6  7  8 9 10 11 12 
datetime                  
2013-01-01 00:00:00 171 6 7 52 76 77.1 63.7 28.74 0 0 0 0 
2013-01-01 00:01:00 192 4 6 52 76 77.1 63.7 28.74 0 0 0 0 
2013-01-01 00:02:00 197 3 6 52 76 77.1 63.7 28.74 0 0 0 0 
2013-01-01 00:03:00 175 3 6 52 76 77.1 63.7 28.73 0 0 0 0 
2013-01-01 00:04:00 194 4 6 52 76 77.1 63.7 28.73 0 0 0 0 
2013-01-01 00:05:00 148 5 6 52 76 77.1 63.7 28.73 0 0 0 0 

In [132]: ax = df.ix[:,1:3].plot(secondary_y=1) 

In [133]: ax.margins(0.04) 

In [134]: plt.tight_layout() 

In [135]: plt.savefig('weather_data.png') 

weather_data.png