Graph Dates and Values

brianrpsgt1 brianlong at cox.net
Tue Mar 10 08:08:41 CET 2009


I am trying to plot dates and values on a graph using matplotlib.
Below is the code.  I can run this and it works great, until I get to
about 2000 rows from the DB.  Things really start to slow down.  I
have successfully plotted up to 5000 rows from the DB, but it is very
slow.  I am attempting to plot values for a day, which would be equal
to 84600 records.  Is there a more efficient may to accomplish this?

import os
import psycopg2
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pylab
import dateutil
from datetime import datetime

conn = psycopg2.connect("dbname='db' user='user' password='somepass'
host='localhost'")

spo2_cur = conn.cursor()
sql = ("""SELECT System_Time, val1 FROM data WHERE DATE(System_Time)
='2009-01-07' AND Inst_Num='12345';""")

data_cur.execute(sql)

value_data = data_cur.fetchall()

data_cur.close()
conn.close()

num_rows = len(value_data)
print "There are",num_rows,"rows in the database"


datesFmt = mdates.DateFormatter('%H:%M')

plt.figure(figsize=(14,3))

for s in value_data:

    dates = mdates.date2num([s[0]])

    plt.plot([dates],[s[1]], 'bo', ms=6)

plt.ylim(60,100)
plt.axhline(y=90, linewidth=2, color='r')

ax1= plt.gca()
ax1.xaxis.set_major_formatter(datesFmt)

for label in ax1.xaxis.get_ticklabels():
    label.set_color('black')
    label.set_rotation(45)
    label.set_fontsize(8)

for label in ax1.yaxis.get_ticklabels():
    label.set_fontsize(8)

plt.show()
plt.close()


Any help would be great!

Thanks

B



More information about the Python-list mailing list