我尝试使用日期时间x轴绘制图表: 这是代码示例:
#!/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'
我不知道如何用日期时间绘制图表。它试图将日期时间表示为字符串,但它没有帮助。 我做错了什么?
答案 0 :(得分:2)
我曾经尝试过gnuplot-py,但它有一些限制,对我来说是违规行为。因此,我决定使用python编写自己的gnuplot包装器。它可以在:
找到http://sourceforge.net/projects/pygnuplot/
我认为这很不错。这是绘制图表的基本脚本:
#!/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列表中的一些更好的文档,但我没有太多时间去做。
另外,我在Linux和Mac上只使用过pyGnuplot,所以它在其他平台上的运行情况目前还不清楚。