Re: [PyX-user] Drawing with MySQL dates (datetime type) From: Stefan Schenk - 2009-05-30 13:10 ```Hi Alex, the problem of your example is that the PyX timeaxis uses the datetime.datetime format, whereas you provided a utc timestamp. So we can just use a simple conversion with datetime.datetime.utcfromtimestamp() to solve this issue. After playing around a little bit, it seems that the labeling of the axis sill has to be done by hand (which is not really surprising since we are using experimental code). I think the following example should give satisfactory results. Gruß, Stefan # ------------------------------------------------------------------------- from datetime import datetime from pyx import graph from pyx.graph.axis import timeaxis, painter from pyx.graph.axis.timeaxis import timetick from pyx.graph.axis.painter import rotatetext results = ((1189623600L, 612.52999999999997), (1207292640L, 638.05999999999995), (1212486300L, 637.25999999999999), (1215021600L, 637.46000000000004), (1228939200L, 513.61000000000001), (1233691200L, 494.24000000000001)) results2 = [[datetime.utcfromtimestamp(r[0]),r[1]] for r in results] number_of_ticks = 10 delta = (max(results2)[0] - min(results2)[0])/(number_of_ticks-1) dticks = [min(results2)[0] + i*delta for i in range(number_of_ticks)] ticks = [timetick(t.year, t.month, t.day) for t in dticks] xaxis = timeaxis.timeaxis(manualticks=ticks, texter=timeaxis.timetexter(format=r"%x"), painter=painter.regular(labeldirection=rotatetext(0))) g = graph.graphxy(width=8, x=xaxis) g.plot(graph.data.points(results2, x=1, y=2)) g.writeEPSfile("timeaxis") # ----------------------------------------------------------------------- Am Freitag 29 Mai 2009 12:48:42 schrieben Sie: > Hi Stefan, > > Again thank you all for your help and advise!!! > > I finally had some time to try some of the timeaxis but I am getting > all sorts of pythony errors. > I checked the repository up and down but it looks like I have the > latest and the greatest. > > So here is my very simplified example: > > # Beginning of example file > import MySQLdb > from pyx import * > # I added the timeaxis to teh __init__.py file solike this: > # __allmodules__ = ["painter", "parter", "rater", "texter", "tick", > "timeaxis"] # so I can be lazy > > # Connect to MySQL Database > my_db_con = MySQLdb.Connect( host = '192.168.xxx.xxx', > user = 'user', > passwd = 'password', > db = 'db_name' ) > my_db_cursor = my_db_con.cursor() > > my_db_cursor.execute( "select unix_timestamp( date_and_time ), cast( > data_value * 1000 as signed)/1000 from my_table" ) > > query_results = my_db_cursor.fetchall() # Get the data in a tuple > # The result looks like: ((1189623600L, 612.52999999999997), > (1189710000L, 613.03999999999996), (1189796400L, 613.13), > (1189882800L, 613.67999999999995), (1197489600L, 636.96000000000004), > (1197662400L, 637.61000000000001), (1199563200L, 638.11000000000001), > (1207292640L, 638.05999999999995), (1207653300L, 637.25999999999999), > (1207738860L, 637.11000000000001), (1207835160L, 637.25999999999999), > (1207837800L, 637.71000000000004), (1207896660L, 637.25999999999999), > (1208156160L, 636.90999999999997), (1208187420L, 637.25999999999999), > (1210093500L, 637.30999999999995), (1210264200L, 638.05999999999995), > (1211911200L, 637.11000000000001), (1212486300L, 637.25999999999999), > (1212492360L, 638.00999999999999), (1212499440L, 636.66999999999996), > (1212504000L, 635.67999999999995), (1212507960L, 637.65999999999997), > (1212512040L, 638.11000000000001), (1212516180L, 636.66999999999996), > (1212520260L, 636.47000000000003), (1212524640L, 637.30999999999995), > (1212532860L, 638.29999999999995), (1212540840L, 637.30999999999995), > (1212548760L, 637.25999999999999), (1212571980L, 637.86000000000001), > (1212576240L, 637.05999999999995), (1212577920L, 636.96000000000004), > (1212580800L, 637.00999999999999), (1212590100L, 637.25999999999999), > (1213015920L, 637.30999999999995), (1213725600L, 638.11000000000001), > (1213819380L, 638.11000000000001), (1214416800L, 638.25), > (1214503200L, 636.47000000000003), (1214589600L, 637.00999999999999), > (1214676000L, 638.39999999999998), (1214762400L, 637.90999999999997), > (1215021600L, 637.46000000000004), (1228923420L, 637.55999999999995), > (1228923420L, 637.05999999999995), (1228923420L, 636.82000000000005), > (1228939200L, 513.61000000000001), (1233259200L, 493.94), > (1233691200L, 494.24000000000001)) > > # now the PyX part of the code: > myaxis = graph.axis.timeaxis.timeaxis() > > graph_data = graph.data.list( query_results, x = 1, y = 2 ) > > var_graph = graph.graphxy( width = 20, x = myaxis ) > > var_graph.plot( graph_data, [graph.style.line()] ) > > var_graph.writePDFfile( "graph" ) > var_graph.writeEPSfile("graph") > > # End of example file > > Here is my trace: > > Traceback (most recent call last): > File "example.py", line xx, in > File "example.py", line 121, in graph_var_data > var_graph.writePDFfile( graph ) > File "/usr/lib/python2.5/site-packages/pyx/canvas.py", line 329, in > wrappedindocument > return method(d, file) > File "/usr/lib/python2.5/site-packages/pyx/document.py", line 161, > in writePDFfile > pdfwriter.PDFwriter(self, file, *args, **kwargs) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 475, > in __init__ > catalog = PDFcatalog(document, self, registry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 155, > in __init__ > self.PDFpages = PDFpages(document, writer, registry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 212, > in __init__ > page = PDFpage(page, pageno, self, writer, registry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 238, > in __init__ > self.PDFcontent = PDFcontent(page, writer, self.pageregistry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 267, > in __init__ > page.processPDF(contentfile, writer, acontext, registry, self.bbox) > File "/usr/lib/python2.5/site-packages/pyx/document.py", line 141, > in processPDF > self._process("processPDF", *args) > File "/usr/lib/python2.5/site-packages/pyx/document.py", line 81, in > _process getattr(self.canvas, processMethod)(canvasfile, writer, context, > registry, bbox) > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 161, in processPDF > self.finish() > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 250, in finish > self.doaxes() > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 472, in doaxes > self.dolayout() > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 456, in dolayout > self.doaxiscreate(axisname) > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 450, in doaxiscreate > self.axes[axisname].create() > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 594, in create > self.linkedto.docreate() > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 499, in docreate > self._createfunction(*self._createargs, **self._createkwargs) > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 450, in doaxiscreate > self.axes[axisname].create() > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 565, in create > self.canvas = self.axis.create(self.data, self.positioner, > self.graphtexrunner, self.errorname) > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 228, in create > return _regularaxis._create(self, data, positioner, > graphtexrunner, self.parter, self.rater, errorname) > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 123, in _create > raise RuntimeError("incomplete axis range%s" % errorname) > RuntimeError: incomplete axis range for axis x > > This makes me think that the Unix timestamp data is not appropriate > for this use of timeaxis. > > Would it be possible to help me with making this example working? > > Many thanks, > > Alex > > > > On Fri, May 22, 2009 at 11:03 AM, Stefan Schenk > > wrote: > > Am Freitag 22 Mai 2009 11:41 schrieb Alexander Brankov: > >> Hi Stefan, > >> > >> Thank you for your quick reply! > >> > >> > in the current pyx svn-repository there is a timeaxis available. > >> > However, it is marked as experimental code and i never used it, so i > >> > cannot tell you if it fits your needs. > >> > >> I would like to have a look if you can tell me how to get a copy of the > >> code. Currently, I am using the latest and the greatest that comes from > >> Debian Lenny Package Manager market as 09-4+b1 > > > > To check out the whole trunk into the current directory (Of course you > > need svn installed): > > > > svn checkout https://pyx.svn.sourceforge.net/svnroot/pyx/trunk . > > > > > > To checkout only the directory with the axes: > > > > svn checkout > > https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx/pyx/graph/axis/ . > > > > > > > > Stefan > > > > ------------------------------------------------------------------------- > >----- Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT > > is a gathering of tech-side developers & brand creativity professionals. > > Meet the minds behind Google Creative Lab, Visual Complexity, Processing, > > & iPhoneDevCamp asthey present alongside digital heavyweights like > > Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com > > _______________________________________________ > > PyX-user mailing list > > PyX-user@... > > https://lists.sourceforge.net/lists/listinfo/pyx-user ```

 Re: [PyX-user] Drawing with MySQL dates (datetime type) From: Stefan Schenk - 2009-05-22 09:20 ```Hi, Am Freitag 22 Mai 2009 10:39 schrieb Alexander Brankov: > My question is there a way to draw the time data on the X axis and to > keep the time ratio? in the current pyx svn-repository there is a timeaxis available. However, it is marked as experimental code and i never used it, so i cannot tell you if it fits your needs. Stefan -- Stefan Schenk Stefan.Schenk@... Theoretische Physik II http://www.Physik.Uni-Augsburg.DE/~schenkst Universität Augsburg Phone: +49-821-598-3240 86135 Augsburg, Germany ```
 Re: [PyX-user] Drawing with MySQL dates (datetime type) From: Alexander Brankov - 2009-05-22 09:41 ```Hi Stefan, Thank you for your quick reply! > in the current pyx svn-repository there is a timeaxis available. However, > it is marked as experimental code and i never used it, so i cannot tell > you if it fits your needs. I would like to have a look if you can tell me how to get a copy of the code. Currently, I am using the latest and the greatest that comes from Debian Lenny Package Manager market as 09-4+b1 Another thing that might help me is if I can draw named values like this tuple: ( ( 'one', 1, 11 ) ( 'two', 2, 12 ) ('three', 3, 13 ) ('four', 4, 14 ) ) It would be nice if it was possible to display the values on the X axis with their names and not the values. Unfortunately, I don't know how to do that either :( Kind regards, Alex ```
 Re: [PyX-user] Drawing with MySQL dates (datetime type) From: Stefan Schenk - 2009-05-22 10:04 ```Am Freitag 22 Mai 2009 11:41 schrieb Alexander Brankov: > Hi Stefan, > > Thank you for your quick reply! > > > in the current pyx svn-repository there is a timeaxis available. > > However, it is marked as experimental code and i never used it, so i > > cannot tell you if it fits your needs. > > I would like to have a look if you can tell me how to get a copy of the > code. Currently, I am using the latest and the greatest that comes from > Debian Lenny Package Manager market as 09-4+b1 To check out the whole trunk into the current directory (Of course you need svn installed): svn checkout https://pyx.svn.sourceforge.net/svnroot/pyx/trunk . To checkout only the directory with the axes: svn checkout https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx/pyx/graph/axis/ . Stefan ```
 Re: [PyX-user] Drawing with MySQL dates (datetime type) From: Francisco Vila - 2009-05-22 10:07 ```2009/5/22 Alexander Brankov : > I converted the data to Unix timestamp, but than I cannot tell which > data comes from which day. > > My question is there a way to draw the time data on the X axis and to > keep the time ratio? I am very interested in a solution for this, too. My approach has been very innocent: create some manualticks and put them in a custom axis. import time from pyx import * format="%d %b %Y" (...) time2006=time.mktime(time.strptime("01 Jan 2006", format)) time2007=time.mktime(time.strptime("01 Jan 2007", format)) time2008=time.mktime(time.strptime("01 Jan 2008", format)) time2009=time.mktime(time.strptime("01 Jan 2009", format)) minRange=time2006 maxRange= (...) (...) myticksDate = [graph.axis.tick.tick(time2006, label="2006"), graph.axis.tick.tick(time2007, label="2007"), graph.axis.tick.tick(time2008, label="2008"), graph.axis.tick.tick(time2009, label="2009"), ] xaxis=graph.axis.linear(title=r"Date",min=minRange,max=maxRange,manualticks=myticksDate,density=1) (...) g = graph.graphxy(width=8,x=xaxis,x2=x2axis,y=yaxis) g.plot(graph.data.file("data",x=1,y="B"), ... -- Francisco Vila. Badajoz (Spain) http://www.paconet.org ```
 Re: [PyX-user] Drawing with MySQL dates (datetime type) From: Stefan Schenk - 2009-05-30 13:10 ```Hi Alex, the problem of your example is that the PyX timeaxis uses the datetime.datetime format, whereas you provided a utc timestamp. So we can just use a simple conversion with datetime.datetime.utcfromtimestamp() to solve this issue. After playing around a little bit, it seems that the labeling of the axis sill has to be done by hand (which is not really surprising since we are using experimental code). I think the following example should give satisfactory results. Gruß, Stefan # ------------------------------------------------------------------------- from datetime import datetime from pyx import graph from pyx.graph.axis import timeaxis, painter from pyx.graph.axis.timeaxis import timetick from pyx.graph.axis.painter import rotatetext results = ((1189623600L, 612.52999999999997), (1207292640L, 638.05999999999995), (1212486300L, 637.25999999999999), (1215021600L, 637.46000000000004), (1228939200L, 513.61000000000001), (1233691200L, 494.24000000000001)) results2 = [[datetime.utcfromtimestamp(r[0]),r[1]] for r in results] number_of_ticks = 10 delta = (max(results2)[0] - min(results2)[0])/(number_of_ticks-1) dticks = [min(results2)[0] + i*delta for i in range(number_of_ticks)] ticks = [timetick(t.year, t.month, t.day) for t in dticks] xaxis = timeaxis.timeaxis(manualticks=ticks, texter=timeaxis.timetexter(format=r"%x"), painter=painter.regular(labeldirection=rotatetext(0))) g = graph.graphxy(width=8, x=xaxis) g.plot(graph.data.points(results2, x=1, y=2)) g.writeEPSfile("timeaxis") # ----------------------------------------------------------------------- Am Freitag 29 Mai 2009 12:48:42 schrieben Sie: > Hi Stefan, > > Again thank you all for your help and advise!!! > > I finally had some time to try some of the timeaxis but I am getting > all sorts of pythony errors. > I checked the repository up and down but it looks like I have the > latest and the greatest. > > So here is my very simplified example: > > # Beginning of example file > import MySQLdb > from pyx import * > # I added the timeaxis to teh __init__.py file solike this: > # __allmodules__ = ["painter", "parter", "rater", "texter", "tick", > "timeaxis"] # so I can be lazy > > # Connect to MySQL Database > my_db_con = MySQLdb.Connect( host = '192.168.xxx.xxx', > user = 'user', > passwd = 'password', > db = 'db_name' ) > my_db_cursor = my_db_con.cursor() > > my_db_cursor.execute( "select unix_timestamp( date_and_time ), cast( > data_value * 1000 as signed)/1000 from my_table" ) > > query_results = my_db_cursor.fetchall() # Get the data in a tuple > # The result looks like: ((1189623600L, 612.52999999999997), > (1189710000L, 613.03999999999996), (1189796400L, 613.13), > (1189882800L, 613.67999999999995), (1197489600L, 636.96000000000004), > (1197662400L, 637.61000000000001), (1199563200L, 638.11000000000001), > (1207292640L, 638.05999999999995), (1207653300L, 637.25999999999999), > (1207738860L, 637.11000000000001), (1207835160L, 637.25999999999999), > (1207837800L, 637.71000000000004), (1207896660L, 637.25999999999999), > (1208156160L, 636.90999999999997), (1208187420L, 637.25999999999999), > (1210093500L, 637.30999999999995), (1210264200L, 638.05999999999995), > (1211911200L, 637.11000000000001), (1212486300L, 637.25999999999999), > (1212492360L, 638.00999999999999), (1212499440L, 636.66999999999996), > (1212504000L, 635.67999999999995), (1212507960L, 637.65999999999997), > (1212512040L, 638.11000000000001), (1212516180L, 636.66999999999996), > (1212520260L, 636.47000000000003), (1212524640L, 637.30999999999995), > (1212532860L, 638.29999999999995), (1212540840L, 637.30999999999995), > (1212548760L, 637.25999999999999), (1212571980L, 637.86000000000001), > (1212576240L, 637.05999999999995), (1212577920L, 636.96000000000004), > (1212580800L, 637.00999999999999), (1212590100L, 637.25999999999999), > (1213015920L, 637.30999999999995), (1213725600L, 638.11000000000001), > (1213819380L, 638.11000000000001), (1214416800L, 638.25), > (1214503200L, 636.47000000000003), (1214589600L, 637.00999999999999), > (1214676000L, 638.39999999999998), (1214762400L, 637.90999999999997), > (1215021600L, 637.46000000000004), (1228923420L, 637.55999999999995), > (1228923420L, 637.05999999999995), (1228923420L, 636.82000000000005), > (1228939200L, 513.61000000000001), (1233259200L, 493.94), > (1233691200L, 494.24000000000001)) > > # now the PyX part of the code: > myaxis = graph.axis.timeaxis.timeaxis() > > graph_data = graph.data.list( query_results, x = 1, y = 2 ) > > var_graph = graph.graphxy( width = 20, x = myaxis ) > > var_graph.plot( graph_data, [graph.style.line()] ) > > var_graph.writePDFfile( "graph" ) > var_graph.writeEPSfile("graph") > > # End of example file > > Here is my trace: > > Traceback (most recent call last): > File "example.py", line xx, in > File "example.py", line 121, in graph_var_data > var_graph.writePDFfile( graph ) > File "/usr/lib/python2.5/site-packages/pyx/canvas.py", line 329, in > wrappedindocument > return method(d, file) > File "/usr/lib/python2.5/site-packages/pyx/document.py", line 161, > in writePDFfile > pdfwriter.PDFwriter(self, file, *args, **kwargs) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 475, > in __init__ > catalog = PDFcatalog(document, self, registry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 155, > in __init__ > self.PDFpages = PDFpages(document, writer, registry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 212, > in __init__ > page = PDFpage(page, pageno, self, writer, registry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 238, > in __init__ > self.PDFcontent = PDFcontent(page, writer, self.pageregistry) > File "/usr/lib/python2.5/site-packages/pyx/pdfwriter.py", line 267, > in __init__ > page.processPDF(contentfile, writer, acontext, registry, self.bbox) > File "/usr/lib/python2.5/site-packages/pyx/document.py", line 141, > in processPDF > self._process("processPDF", *args) > File "/usr/lib/python2.5/site-packages/pyx/document.py", line 81, in > _process getattr(self.canvas, processMethod)(canvasfile, writer, context, > registry, bbox) > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 161, in processPDF > self.finish() > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 250, in finish > self.doaxes() > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 472, in doaxes > self.dolayout() > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 456, in dolayout > self.doaxiscreate(axisname) > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 450, in doaxiscreate > self.axes[axisname].create() > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 594, in create > self.linkedto.docreate() > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 499, in docreate > self._createfunction(*self._createargs, **self._createkwargs) > File "/usr/lib/python2.5/site-packages/pyx/graph/graph.py", line > 450, in doaxiscreate > self.axes[axisname].create() > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 565, in create > self.canvas = self.axis.create(self.data, self.positioner, > self.graphtexrunner, self.errorname) > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 228, in create > return _regularaxis._create(self, data, positioner, > graphtexrunner, self.parter, self.rater, errorname) > File "/usr/lib/python2.5/site-packages/pyx/graph/axis/axis.py", line > 123, in _create > raise RuntimeError("incomplete axis range%s" % errorname) > RuntimeError: incomplete axis range for axis x > > This makes me think that the Unix timestamp data is not appropriate > for this use of timeaxis. > > Would it be possible to help me with making this example working? > > Many thanks, > > Alex > > > > On Fri, May 22, 2009 at 11:03 AM, Stefan Schenk > > wrote: > > Am Freitag 22 Mai 2009 11:41 schrieb Alexander Brankov: > >> Hi Stefan, > >> > >> Thank you for your quick reply! > >> > >> > in the current pyx svn-repository there is a timeaxis available. > >> > However, it is marked as experimental code and i never used it, so i > >> > cannot tell you if it fits your needs. > >> > >> I would like to have a look if you can tell me how to get a copy of the > >> code. Currently, I am using the latest and the greatest that comes from > >> Debian Lenny Package Manager market as 09-4+b1 > > > > To check out the whole trunk into the current directory (Of course you > > need svn installed): > > > > svn checkout https://pyx.svn.sourceforge.net/svnroot/pyx/trunk . > > > > > > To checkout only the directory with the axes: > > > > svn checkout > > https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx/pyx/graph/axis/ . > > > > > > > > Stefan > > > > ------------------------------------------------------------------------- > >----- Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT > > is a gathering of tech-side developers & brand creativity professionals. > > Meet the minds behind Google Creative Lab, Visual Complexity, Processing, > > & iPhoneDevCamp asthey present alongside digital heavyweights like > > Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com > > _______________________________________________ > > PyX-user mailing list > > PyX-user@... > > https://lists.sourceforge.net/lists/listinfo/pyx-user ```