[Forwarded to the list.]
---------- Weitergeleitete Nachricht ----------
Betreff: Re: [PyX-user] Drawing with MySQL dates (datetime type)
Datum: Donnerstag 04 Juni 2009
Von: Alexander Brankov <[email protected]>
An: Stefan Schenk <[email protected]>
Hi Stephen,
Thank you very much for your help! It is much appreciated.
I had a play with the example I was given. It looks very promissing.
However, with your permission, I would like to bring a light to my
findings.
I attached 3 files that demonstrates my line of thoughts.
The first file (timeaxis1.py) is your original example adapted to my
environment. It works fine with the data provided and serves it
purpose perfectly. Thank you very much for that!
The second file is the modified example illustrating the way the data
is required to be represented. It shows that the graph is perfectly
capable of printing the Data and the Time together. It also shows the
limitations of the timetick class introduced, which despite inheriting
the datatime Pythin class limits the data to just the Date proportion
of the time. As a result the timeaxis behaves more like dateaxis.
The third file (timeaxis3.py) does not work, probably because there
are 2 measurements taken in the same day but at different time. This
makes me wander, how would I display the data where we have multiple
measurements in one day? Would there be a different axis for this
case?
It would be nice to know.
Best regards,
Alex
P.S. Is this correspondence going to the PyX users mail list
On Sat, May 30, 2009 at 2:12 PM, Stefan Schenk
<[email protected]> wrote:
>
> 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 <module>
>> 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
>>
>> <[email protected]> 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
>> > [email protected]
>> > https://lists.sourceforge.net/lists/listinfo/pyx-user
>
>
> ------------------------------------------------------------------------------
> 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 as they present alongside digital heavyweights like
Barbarian
> Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com
> _______________________________________________
> PyX-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/pyx-user
>
-------------------------------------------------------
--
Stefan Schenk [email protected]
Theoretische Physik II www.Physik.Uni-Augsburg.DE/~schenkst
Universität Augsburg Phone: +49-821-598-3240
86135 Augsburg, Germany
##############################################################################
# #
# File: timeaxis1.py #
# Brief: The original example of the use of the timeaxis in PyX #
# on the X axis. #
# #
##############################################################################
from datetime import datetime
import sys
sys.path.insert(0, "/opt/PyX-module")
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("timeaxis1")
##############################################################################
# #
# File: timeaxis3.py #
# Brief: Illustrates how to dysplay the Date and Time on the X axis. #
# #
##############################################################################
from datetime import datetime
import sys
sys.path.insert(0, "/opt/PyX-module")
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 = 15
delta = (max(results2)[0] - min(results2)[0])/(number_of_ticks-1)
time_scale_begin = min(results2)[0] - delta
time_scale_end = max(results2)[0] + delta
scale_ticks = [ timetick( time_scale_begin.year, time_scale_begin.month, time_scale_begin.day ) ]
scale_ticks += [ timetick( tr[0].year, tr[0].month, tr[0].day ) for tr in results2 ]
# The following line shoud replace the one above once the timetick class is properly
# developed to take the Time as well as the Date
#scale_ticks += [timetick(tr[0].year, tr[0].month, tr[0].day, tr[0].hour, tr[0].minute, tr[0].second ) for tr in results2]
scale_ticks += [timetick( time_scale_end.year, time_scale_end.month, time_scale_end.day ) ]
xaxis = timeaxis.timeaxis(manualticks=scale_ticks,
texter=timeaxis.timetexter(format=r"%x - %X"),
painter=painter.regular(labeldirection=rotatetext(0)))
g = graph.graphxy(width=20, x=xaxis)
g.plot(graph.data.points(results2, x=1, y=2))
g.writeEPSfile("timeaxis3")
##############################################################################
# #
# File: timeaxis4.py #
# Brief: Illustrates the problem with values for the same Date and #
# different time #
# #
##############################################################################
from datetime import datetime
import sys
sys.path.insert(0, "/opt/PyX-module")
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),
(1228939333L, 507.610),
(1233691200L, 494.24000000000001))
results2 = [[datetime.utcfromtimestamp(r[0]),r[1]] for r in results]
number_of_ticks = 15
delta = (max(results2)[0] - min(results2)[0])/(number_of_ticks-1)
time_scale_begin = min(results2)[0] - delta
time_scale_end = max(results2)[0] + delta
scale_ticks = [ timetick( time_scale_begin.year, time_scale_begin.month, time_scale_begin.day ) ]
scale_ticks += [ timetick( tr[0].year, tr[0].month, tr[0].day ) for tr in results2 ]
# The following line shoud replace the one above once the timetick class is properly
# developed to take the Time as well as the Date
#scale_ticks += [timetick(tr[0].year, tr[0].month, tr[0].day, tr[0].hour, tr[0].minute, tr[0].second ) for tr in results2]
scale_ticks += [timetick( time_scale_end.year, time_scale_end.month, time_scale_end.day ) ]
xaxis = timeaxis.timeaxis(manualticks=scale_ticks,
texter=timeaxis.timetexter(format=r"%x - %X"),
painter=painter.regular(labeldirection=rotatetext(0)))
g = graph.graphxy(width=20, x=xaxis)
g.plot(graph.data.points(results2, x=1, y=2))
g.writeEPSfile("timeaxis4")
------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
PyX-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyx-user