1

I'm new to python and matplotlib and need a few pointers. I'm trying to write a monitor that queries a table and plots the results. from the table I can extract a timestamp I'd like to use for the X axis and a #of seconds I'd like to use for the Y value (number of packets sent). I'm not sure where "i" is populated for the animate function. My plot comes up but is empty. I'm not sure what ax.set_xlim should be set to and finally how do I get the date/timestamp to show up on the x-axis? Im trying to modify the following example:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()
ax = plt.axes(ylim=(0, 45))
line, = ax.plot([], [], lw=5)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x,y,dk=getData()
    line.set_data(x, y)
    return line,

def Execute():
    #anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=200, blit=True)
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=2000)
    plt.show()
    return(anim)

def getDataSql(sql):
    ... run sql
    return(rl)

def getData():
    ...format return for getDataSql 
    ...return X ex(2013-04-12 18:18:24) and Y ex(7.357) (both are lists)
    return(X,Y,xy)

x=Execute()
2
  • 1
    and what exactly is your question? do you find anything wrong with the example? you don't know how to modify it? on which part? why? Commented Apr 19, 2013 at 19:03
  • Can you edit this to only include the relevant code? We don't need to see your sql code (which we can't run any way), replace it with a stub function that returns fake data. Commented Apr 19, 2013 at 20:31

1 Answer 1

1
def Execute():
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=2000, blit=True)
    plt.show() 
    return anim

anim = Execute()

If you don't return the anim object (which has all the timers ect) in it, it gets garbage collected when Execute returns, which deletes all those objects, and hence your animation does not run.

You might also test using blit=False, it is a bit slower (which isn't an issue as you are updating ever 2s), but it is a bit less fincky to get working right.

Also try

ax.get_xaxis().set_major_locator(matplotlib.dates.AutoDateLocator())
ax.get_xaxis().set_major_formatter(matplotlib.dates.AutoDateFormatter())

before you run anything.

Sign up to request clarification or add additional context in comments.

5 Comments

no joy. Updated the code in the first post. the first value in the list x is 'datetime.datetime(2013, 4, 12, 18, 18, 24)' and y has '7.357' if that helps.
You might also be having issues with the locator not being set up for dates.
ax = plt.axes(ylim=(0, 8)) ax.get_xaxis().set_major_locator(matplotlib.dates.AutoDateLocator()) ax.get_xaxis().set_major_formaterr(matplotlib.dates.AutoDateFormatter()) gets AttributeError: 'XAxis' object has no attribute 'set_major_formaterr'
@Kevin There was an obvious typo in that line.
ax.get_xaxis().set_major_locator(matplotlib.dates.AutoDateLocator()) ax.get_xaxis().set_major_formatter(matplotlib.dates.AutoDateFormatter()) There is still something wrong with the second line .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.