0

So I just installed Python3.3, Numpy, MySQL Connector, and Matplotlib. I have a table with a lot of data where I wrote a query that outputs it to something similar to this:

Timestamp(unix)  Data(float)
1353000347       90.7000
1353007787       90.7000
1353015227       90.7000
1353000347       89.6899
1353007787       90.7000
1353015227       90.7000

So through a lot of trial and error and piecing together tutorials and other questions on here I ended up with this python script:

import matplotlib.pyplot as plt
import numpy as np
import mysql.connector

#Open DB Connection
cnx = mysql.connector.connect(host='localhost',database='db',
                              user='user',password='')
#Get Buffered Cursor
curA = cnx.cursor(buffered=True)

curA.execute(
"""
SELECT UNIX_TIMESTAMP(`timestamp`),`data1`
   FROM `data`
"""
)

rows = np.array(curA.fetchall())
cnx.close()

x = rows[:,0]
y = rows[:,1]
plt.scatter(x,y,20,'r','o',None,None,None,None,0.05,0)
plt.show()

I have a lot of data points that will overlap (same coordinates) so I wanted the alpha real low. I got what I wanted here:

enter image description here

Anyways, what I wanted was to simply format the tick markers to dates (e.g. Month, Year). I searched a lot on this problem and found a similar question here, but I wasn't able to adapt it or make sense of how I could apply it to my script. Seems overly complicated for such a simple thing?

Community
  • 1
  • 1
user17753
  • 3,083
  • 9
  • 35
  • 73
  • I don't have time to write a full answer, but the basic approach I would suggest is to convert your timestamps -> `datetime` objects [doc](http://docs.python.org/2/library/datetime.html). You can then plot directly against those objects. You might still need to tweak the formatters a bit, but this should get you 80% of the way. – tacaswell Mar 12 '13 at 19:38
  • also see `pandas` which has a whole bunch of fancy semi-intelligent date formatting (but may be overkill and is another can of worms) – tacaswell Mar 12 '13 at 19:41
  • Actually, found this [question](http://stackoverflow.com/questions/4090383/plotting-unix-timestamps-in-matplotlib) shortly after which I think gives me what I need. – user17753 Mar 12 '13 at 19:48
  • good. As a style note, you should use kwags, not positional args to pass arguments to `scatter`, ex `scatter(x, y, s=20, color='r', marker='o', alpha=.5)` which is much more readable and (more) future proof. – tacaswell Mar 12 '13 at 19:53
  • Do you think this should be marked as a duplicate of that question? – tacaswell Mar 12 '13 at 19:57
  • @tcaswell Yes, probably. – user17753 Mar 12 '13 at 20:02

0 Answers0