带有日期时间图表的gnuplot-py错误 [英] gnuplot-py error with datetime chart
问题描述
我试图用日期时间x轴绘制图表:这是代码示例:
I tried to plot chart with datetime x axis: here is example of the code:
#!/usr/bin/python2.7 -tt
# coding: utf-8
import Gnuplot
from datetime import datetime
out_file = 'test.png'
out_file_str = 'set out "'+out_file+'"'
#example of chart with options and data
data = (('10-02-2012 18:00:36', '33.547'), ('10-02-2012 18:01:06', '23.962'), ('10-02-2012 18:04:06', '18.071'), ('10-02-2012 18:35:36', '13.513'), ('10-02-2012 18:47:06', '23.869'), ('10-02-2012 18:51:06', '13.988'), ('10-02-2012 18:56:06', '5.869'), ('10-02-2012 18:56:36', '3.811'), ('10-02-2012 18:59:36', '4.01'))
ytics = 10
start_range = '"10-02-2012 18:00:00"'
end_range = '"10-02-2012 19:00:00"'
xrange = ( start_range, end_range)
yrange = ( 0, 50 )
chart = Gnuplot.Gnuplot()
set_term = 'set terminal png truecolor size 780,464'
chart(set_term)
chart(out_file_str)
chart('set xdata time')
set_timefmt = 'set timefmt "%d-%m-%Y %H:%M:%S"'
chart(set_timefmt)
set_xformat = 'set format x "%H:%M\\n%d.%m"'
chart(set_xformat)
set_yformat = 'set format y "%.0f"'
chart(set_yformat)
set_yticformat = 'set ytics format "%.0f"'
chart(set_yticformat)
chart('set ytics out nomirror')
chart('set grid xtics ytics mxtics mytics')
chart('set xtics axis out scale 1.0,0.1 nomirror')
chart('set key out horiz')
chart('set key center')
chart('set key bmargin')
set_ytics = 'set ytics ' + str(ytics)
chart(set_ytics)
y_range = (0, 100)
chart.set_range('yrange',y_range)
chart.set_range('xrange', xrange)
data1 = []
for val in data:
ctime = datetime.strptime(val[0], '%d-%m-%Y %H:%M:%S')
cur_str = (ctime, val[1])
data1.append(cur_str)
for d in data1:
chart.plot(d[0],d[1])
,此代码返回错误:
Fatal: array dimensions not equal!
Traceback (most recent call last):
File "./test-gnuplot2.py", line 49, in <module>
chart.plot(d[0],d[1])
File "/usr/lib/python2.7/site-packages/Gnuplot/_Gnuplot.py", line 284, in plot
self._add_to_queue(items)
File "/usr/lib/python2.7/site-packages/Gnuplot/_Gnuplot.py", line 254, in _add_to_queue
self.itemlist.append(PlotItems.Data(item))
File "/usr/lib/python2.7/site-packages/Gnuplot/PlotItems.py", line 554, in Data
if len(data.shape) == 1:
AttributeError: 'NoneType' object has no attribute 'shape'
我不知道如何绘制带有日期时间的图表.它试图将日期时间表示为字符串,但没有帮助.我在做什么错了?
i don't know how can i plot chart with datetime. it tried to represent datetime as a string, but it didn't help. what am i doing wrong?
推荐答案
我前一段时间尝试了gnuplot-py,但是它有一些局限性,对我来说是个破坏因素.因此,我决定使用python编写自己的gnuplot包装器.可以在以下位置找到它:
I tried gnuplot-py a while back, but it had some limitations that were deal-breakers for me. Because of that, I decided to write my own gnuplot wrapper using python. It can be found at:
http://sourceforge.net/projects/pygnuplot/
我认为这很好.这是绘制图形的基本脚本:
I think it is pretty good. Here's basic script to plot your graph:
#!/usr/bin/python2.7 -tt
# coding: utf-8
import pyGnuplot
from datetime import datetime
out_file = 'test.png'
out_file_str = 'set out "'+out_file+'"'
#example of chart with options and data
data = (('10-02-2012 18:00:36', '33.547'),
('10-02-2012 18:01:06', '23.962'),
('10-02-2012 18:04:06', '18.071'),
('10-02-2012 18:35:36', '13.513'),
('10-02-2012 18:47:06', '23.869'),
('10-02-2012 18:51:06', '13.988'),
('10-02-2012 18:56:06', '5.869'),
('10-02-2012 18:56:36', '3.811'),
('10-02-2012 18:59:36', '4.01'))
ytics = 10
#When str(datatime.datatime), the format is %Y-%m-%d %H:%M:%S, so that
# is the format pyGnuplot uses by default -- Of course the format you use
# to create datetime objects is up to you.
start_range = '"2012-02-10 18:00:00"'
end_range = '"2012-02-10 19:00:00"'
xrange = ( start_range, end_range)
# yrange = ( 0, 50 )
tvals,yvals=zip(*data)
tvals=[datetime.strptime(i, '%d-%m-%Y %H:%M:%S') for i in tvals]
print str(tvals[0])
print tvals
print yvals
g = pyGnuplot.gnuplot(debug=1)
g('set xdata time')
g('set timefmt "%Y-%d-%m %H:%M:%S"') #Default datatime output format.
set_xformat = 'set format x "%H:%M\\n%d.%m"'
g(set_xformat)
set_yformat = 'set format y "%.0f"'
g(set_yformat)
set_yticformat = 'set ytics format "%.0f"'
g(set_yticformat)
g('set ytics out nomirror')
g('set grid xtics ytics mxtics mytics')
g('set xtics axis out scale 1.0,0.1 nomirror')
g('set key out horiz')
g('set key center')
g('set key bmargin')
set_ytics = 'set ytics ' + str(ytics)
g(set_ytics)
y_range = (0, 100)
plt=g.plot(yvals,xvals=tvals,u="1:3")
plt.xrange(xrange)
plt.yrange(y_range)
g.show(plt) #Show the plot in the gnuplot default terminal
g.hardcopy(plt,file='myfile.png',size='780,464',truecolor=True) #Make a hardcopy
print g.readlines() #Get any script error messages from gnuplot
pyGnuplot可能会使用一些更好的文档,这些文档在我的TODO列表中,但是我没有很多时间来做.
pyGnuplot could probably use some better documentation which is on my TODO list, but I haven't had a ton of time to do it.
此外,我只在Linux和Mac上使用过pyGnuplot,因此目前尚不清楚它在其他平台上的运行情况.
Also, I have only used pyGnuplot on Linux and Mac, so how well it works on other platforms is currently unknown.
这篇关于带有日期时间图表的gnuplot-py错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!