Iteration problem with plot_date

hi all,
i'm getting the following error when running this script.
if the script doesn't iterate over "tels" it runs fine...
it seems to do things right just once.
thanks in advance,
hector

···

################################################################################
ERROR
################################################################################

C:\Documents and Settings\villaf>python e:\src\python\ttp\ttp_plot.py
1082751605.0 : Fri Apr 23 12:20:05 2004 : ttp_plot start
Traceback (most recent call last):
   File "e:\src\python\ttp\ttp_plot.py", line 56, in ?
     plotting_date('traffic', 'tmp_calls0', 3)
   File "e:\src\python\ttp\ttp_plot.py", line 39, in plotting_date
     plot_date(datum, vals, PyDatetimeConverter())
   File "C:\Python23\Lib\site-packages\matplotlib\matlab.py", line 1011, in plot_date
     try: lines = gca().plot_date(*args, **kwargs)
   File "C:\Python23\Lib\site-packages\matplotlib\axes.py", line 1198, in plot_date
     locator = MinuteLocator(1)
   File "C:\Python23\Lib\site-packages\matplotlib\ticker.py", line 720, in __init__
     MultipleLocator.__init__(self, base*SEC_PER_MIN)
NameError: global name 'SEC_PER_MIN' is not defined

################################################################################
SCRIPT
################################################################################

import time, ConfigParser, MySQLdb
import datetime
import matplotlib
matplotlib.use('Agg')
from matplotlib.matlab import *
from matplotlib.dates import PyDatetimeConverter, MONDAY
from matplotlib.ticker import WeekdayLocator, DayLocator, DateFormatter

config = ConfigParser.ConfigParser()
config.readfp(open('e:\\src\\python\\ttp\\ttp.conf', 'r'))
loc_host = config.get('DATABASE', 'loc_host')
loc_user = config.get('DATABASE', 'loc_user')
loc_passwd = config.get('DATABASE', 'loc_passwd')
loc_db = config.get('DATABASE', 'loc_db')

loc_db = MySQLdb.connect(host = loc_host, user = loc_user, passwd = loc_passwd, db = loc_db)
cursor = loc_db.cursor()

def plotting_date(db, table, dig):

     cursor.execute("DROP TABLE IF EXISTS %s.tmp" % (db))
     cursor.execute("CREATE TABLE %s.tmp "
                    "SELECT fecha, mid(tel,1,%d) as tel, sum(minutos) as min FROM %s.%s GROUP BY 1,2" % (db,dig,db,table))
     cursor.execute("SELECT DISTINCT tel FROM %s.tmp" % (db))
     tels = cursor.fetchall()
     for tel in tels:
         cursor.execute("SELECT fecha, min FROM %s.tmp WHERE tel = '%s'" % (db,tel[0]))
         data = cursor.fetchall()
         datum = [datetime.datetime(int(q[0][0:4]), int(q[0][4:6]), int(q[0][6:8]), 0, 0) for q in data]
         vals = [q[1] for q in data]

         ax = subplot(1,1,1)
         plot_date(datum, vals, PyDatetimeConverter())

         ax.xaxis.set_major_locator(WeekdayLocator(MONDAY))
         ax.xaxis.set_major_formatter(DateFormatter('%b %d'))
         ax.xaxis.set_minor_locator(DayLocator())
         ax.xaxis.autoscale_view()
         title('TEST')
         ylabel('test')
         labels = ax.get_xticklabels()
         set(labels, 'rotation', 'vertical')
         grid(True)
         savefig("c:\\tmp\\%s_%s_test" % (tel[0],table))

# --- main -- #
if __name__ == '__main__':
     print time.mktime(time.localtime()), ': \t', time.asctime(), ': \t', 'ttp_plot start'
     plotting_date('traffic', 'tmp_calls0', 3)
     print time.mktime(time.localtime()), ': \t', time.asctime(), ': \t', 'ttp_plot end'

I changed the script and I got this error
Thanks for your help

···

########################################################################
NEW ERROR
########################################################################
C:\Documents and Settings\villaf>python e:\src\python\ttp\ttp_plot.py
1082763298.0 : Fri Apr 23 15:34:58 2004 : ttp_plot start
Traceback (most recent call last):
  File "e:\src\python\ttp\ttp_plot.py", line 60, in ?
    get_data('traffic', 'tmp_calls0', 3)
  File "e:\src\python\ttp\ttp_plot.py", line 37, in get_data
    plotting_date(tel[0], datum, vals)
  File "e:\src\python\ttp\ttp_plot.py", line 54, in plotting_date
    savefig("c:\\tmp\\%s_%s" % (tel,table))
  File "C:\Python23\Lib\site-packages\matplotlib\matlab.py", line 1115, in savefig
    manager.canvas.print_figure(*args, **kwargs)
  File "C:\Python23\Lib\site-packages\matplotlib\backends\backend_agg.py", line 419, in print_figure
    self.renderer._renderer.write_png(filename)
IOError: could not open file

########################################################################
SCRIPT
########################################################################
import time, ConfigParser, MySQLdb
import datetime
import matplotlib
matplotlib.use('Agg')
from matplotlib.matlab import *
from matplotlib.dates import PyDatetimeConverter, MONDAY
from matplotlib.ticker import WeekdayLocator, DayLocator, DateFormatter

config = ConfigParser.ConfigParser()
config.readfp(open('e:\\src\\python\\ttp\\ttp.conf', 'r'))
loc_host = config.get('DATABASE', 'loc_host')
loc_user = config.get('DATABASE', 'loc_user')
loc_passwd = config.get('DATABASE', 'loc_passwd')
loc_db = config.get('DATABASE', 'loc_db')

loc_db = MySQLdb.connect(host = loc_host, user = loc_user, passwd = loc_passwd, db = loc_db)
cursor = loc_db.cursor()

def get_data(db, table, dig):

    cursor.execute("DROP TABLE IF EXISTS %s.tmp" % (db))
    cursor.execute("CREATE TABLE %s.tmp "
                   "SELECT fecha, mid(tel,1,%d) as tel, sum(minutos) as min FROM %s.%s GROUP BY 1,2" % (db,dig,db,table))
    cursor.execute("SELECT DISTINCT tel FROM %s.tmp" % (db))
    tels = cursor.fetchall()
    for tel in tels:
        cursor.execute("SELECT fecha, min FROM %s.tmp WHERE tel = '%s'" % (db,tel[0]))
        data = cursor.fetchall()
        datum = [datetime.datetime(int(q[0][0:4]), int(q[0][4:6]), int(q[0][6:8]), 0, 0) for q in data]
        vals = [q[1] for q in data]
        plotting_date(tel[0], datum, vals)

def plotting_date(tel, datum, vals):

    ax = subplot(1,1,1)
    plot_date(datum, vals, PyDatetimeConverter())

    ax.xaxis.set_major_locator(WeekdayLocator(MONDAY))
    ax.xaxis.set_major_formatter(DateFormatter('%b %d'))
    ax.xaxis.set_minor_locator(DayLocator())
    ax.xaxis.autoscale_view()
    title('TEST')
    ylabel('test')
    labels = ax.get_xticklabels()
    set(labels, 'rotation', 'vertical')
    grid(True)
    savefig("c:\\tmp\\%s_%s" % (tel,table))

# --- main -- #
if __name__ == '__main__':
    print time.mktime(time.localtime()), ': \t', time.asctime(), ': \t', 'ttp_plot start'
    get_data('traffic', 'tmp_calls0', 3)
    print time.mktime(time.localtime()), ': \t', time.asctime(), ': \t', 'ttp_plot end'

Héctor Villafuerte D. wrote:

hi all,
i'm getting the following error when running this script.
if the script doesn't iterate over "tels" it runs fine...
it seems to do things right just once.
thanks in advance,
hector

################################################################################
ERROR
################################################################################

C:\Documents and Settings\villaf>python e:\src\python\ttp\ttp_plot.py
1082751605.0 : Fri Apr 23 12:20:05 2004 : ttp_plot start
Traceback (most recent call last):
  File "e:\src\python\ttp\ttp_plot.py", line 56, in ?
    plotting_date('traffic', 'tmp_calls0', 3)
  File "e:\src\python\ttp\ttp_plot.py", line 39, in plotting_date
    plot_date(datum, vals, PyDatetimeConverter())
  File "C:\Python23\Lib\site-packages\matplotlib\matlab.py", line 1011, in plot_date
    try: lines = gca().plot_date(*args, **kwargs)
  File "C:\Python23\Lib\site-packages\matplotlib\axes.py", line 1198, in plot_date
    locator = MinuteLocator(1)
  File "C:\Python23\Lib\site-packages\matplotlib\ticker.py", line 720, in __init__
    MultipleLocator.__init__(self, base*SEC_PER_MIN)
NameError: global name 'SEC_PER_MIN' is not defined

################################################################################
SCRIPT
################################################################################

import time, ConfigParser, MySQLdb
import datetime
import matplotlib
matplotlib.use('Agg')
from matplotlib.matlab import *
from matplotlib.dates import PyDatetimeConverter, MONDAY
from matplotlib.ticker import WeekdayLocator, DayLocator, DateFormatter

config = ConfigParser.ConfigParser()
config.readfp(open('e:\\src\\python\\ttp\\ttp.conf', 'r'))
loc_host = config.get('DATABASE', 'loc_host')
loc_user = config.get('DATABASE', 'loc_user')
loc_passwd = config.get('DATABASE', 'loc_passwd')
loc_db = config.get('DATABASE', 'loc_db')

loc_db = MySQLdb.connect(host = loc_host, user = loc_user, passwd = loc_passwd, db = loc_db)
cursor = loc_db.cursor()

def plotting_date(db, table, dig):

    cursor.execute("DROP TABLE IF EXISTS %s.tmp" % (db))
    cursor.execute("CREATE TABLE %s.tmp "
                   "SELECT fecha, mid(tel,1,%d) as tel, sum(minutos) as min FROM %s.%s GROUP BY 1,2" % (db,dig,db,table))
    cursor.execute("SELECT DISTINCT tel FROM %s.tmp" % (db))
    tels = cursor.fetchall()
    for tel in tels:
        cursor.execute("SELECT fecha, min FROM %s.tmp WHERE tel = '%s'" % (db,tel[0]))
        data = cursor.fetchall()
        datum = [datetime.datetime(int(q[0][0:4]), int(q[0][4:6]), int(q[0][6:8]), 0, 0) for q in data]
        vals = [q[1] for q in data]

        ax = subplot(1,1,1)
        plot_date(datum, vals, PyDatetimeConverter())

        ax.xaxis.set_major_locator(WeekdayLocator(MONDAY))
        ax.xaxis.set_major_formatter(DateFormatter('%b %d'))
        ax.xaxis.set_minor_locator(DayLocator())
        ax.xaxis.autoscale_view()
        title('TEST')
        ylabel('test')
        labels = ax.get_xticklabels()
        set(labels, 'rotation', 'vertical')
        grid(True)
        savefig("c:\\tmp\\%s_%s_test" % (tel[0],table))

# --- main -- #
if __name__ == '__main__':
    print time.mktime(time.localtime()), ': \t', time.asctime(), ': \t', 'ttp_plot start'
    plotting_date('traffic', 'tmp_calls0', 3)
    print time.mktime(time.localtime()), ': \t', time.asctime(), ': \t', 'ttp_plot end'

-------------------------------------------------------
This SF.net email is sponsored by: The Robotic Monkeys at ThinkGeek
For a limited time only, get FREE Ground shipping on all orders of $35
or more. Hurry up and shop folks, this offer expires April 30th!
http://www.thinkgeek.com/freeshipping/?cpg=12297
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options