用时间轴绘图(Python) [英] Plotting with time axis (Python)
问题描述
我又遇到了一个问题,那已经使我头痛了几个小时……而且我很确定这只是小事.
我得到了一个长数据文件,并创建了一个包含标题名称及其类型的列表:
col_headers = [('TIMESTAMP','|S21'),('XXX_1','<i8'),('YYY_2','<i8')
之后,我创建了一个包含选项的字典变量,最后使用
将其导入bladata = scipy.genfromtxt(datafile, **kwargs)
为了提取我使用的时间信息
import datetime as dt
for i in range (0,len(bladata['TIMESTAMP']),1):
datestring = bladata['TIMESTAMP'][i]
#create a datetime object holding dates and times
d = dt.datetime.strptime(datestring, '"%Y-%m-%d %H:%M:%S"')
最后我通过使用
将数据分配给变量XXX = bladata['XXX_1']
YYY = bladata['YYY_2']
(顺便说一句,当我的数据文件中的标头名称不是XXX_1而是XXX(1)时,我收到错误消息找不到名为XXX(1)的字段"-我也该如何解决此问题?)>
现在进行绘图时,我收到错误消息
x and y must have same first dimension
使用以下代码时
pylab.plot(dt, XXX)
pylab.plot(dt, YYY)
pylab.xlim(d.datetime(2014, 01, 10), d.datetime(2014, 06, 10))
我搜索了该错误消息,并找到了一种建议将列表转换为numpy数组的解决方案...但是,在哪里和哪一个?我很确定这根本不是什么大问题,但是我几个小时都无法解决...所以我很高兴收到任何有用的答复.
dit:为澄清起见... XXX和YYY是2种不同的测量值,我想在1个图中将其对应的日期可视化.
在pylab.plot(dt, XXX)
中,您尝试将导入的datetime
模块本身与值列表相对应,这不是您想要的.
相反,您想要类似pylab.plot(times, XXX)
的内容,其中times
应该是包含与列表XXX
相对应的datetime
对象的列表.
您收到的错误消息指出传递给plot
函数的2个参数应该是长度相同的列表.
请参见使用Matplotlib在Python中绘制时间,以获取更多信息,包括格式化x轴标签.
I ran into a problem again thats causing me headache for several hours already...and I'm pretty sure it's just a minor thing.
I got a long datafile and created a list with the header names and their types:
col_headers = [('TIMESTAMP','|S21'),('XXX_1','<i8'),('YYY_2','<i8')
After that I created a dictionary variable that holds the options and finally imported it with
bladata = scipy.genfromtxt(datafile, **kwargs)
For extracting time information I used
import datetime as dt
for i in range (0,len(bladata['TIMESTAMP']),1):
datestring = bladata['TIMESTAMP'][i]
#create a datetime object holding dates and times
d = dt.datetime.strptime(datestring, '"%Y-%m-%d %H:%M:%S"')
And finally I assigned the data to a variable by using
XXX = bladata['XXX_1']
YYY = bladata['YYY_2']
(btw when the header name in my datafile is not XXX_1 but XXX(1) instead, I receive the error message "field named XXX(1) not found" - how can I solve this problem as well?)
Now when it comes to plotting, I receive the error message
x and y must have same first dimension
when using the following code
pylab.plot(dt, XXX)
pylab.plot(dt, YYY)
pylab.xlim(d.datetime(2014, 01, 10), d.datetime(2014, 06, 10))
I googled that error message and found a solution that suggested converting a list into a numpy array...but where and which one? I'm pretty sure that isn't a big problem at all, but I couldn't figure it out for several hours...so I'm happy for any usefull reply.
€dit: for clarification...XXX and YYY are 2 different measurements that I want to visualize in 1 plot versus its corresponding date.
In pylab.plot(dt, XXX)
you're trying to plot the imported datetime
module itself against a list of values, which is not what you want.
Instead you want something like pylab.plot(times, XXX)
where times
should be a list containing the datetime
objects corresponding to the list XXX
.
The error message you are getting is pointing out the 2 arguments you pass to the plot
function should be lists of the same length.
See Plotting time in Python with Matplotlib for further information including formatting the x-axis labels.
这篇关于用时间轴绘图(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!