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])
and this code returns error:
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?
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:
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.
Also, I have only used pyGnuplot on Linux and Mac, so how well it works on other platforms is currently unknown.