2013-07-04 17 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