0

Is there any way to draw to a plot within a function and then have the changes show up on the plot before the function is finished executing?

For instance, in this function I would like to get the window extent of the text, but the function gives the error Cannot get window extent w/o renderer because at the time s.get_window_extent() is called the text has not been drawn on the plot.

import matplotlib.pyplot as plt
import time
plt.ion()
myfig, myax = plt.subplots()


def plot_now():
    s = myax.annotate("foo", [0.5, 0.5])
    myfig.canvas.draw_idle()
    s.get_window_extent()

plot_now()

I'm using iPython, python 2.7.6, matplotlib 1.4.3, and the Qt4Agg backend

C_Z_
  • 7,427
  • 5
  • 44
  • 81

2 Answers2

1

Add plt.draw() before it goes sleep.

Nathan Davis
  • 5,636
  • 27
  • 39
DaveQ
  • 1,642
  • 10
  • 15
  • Does not work - the figure doesn't update until the function is done running – C_Z_ Aug 27 '15 at 18:55
  • It worked on my end running on 2.7.6 shell and matplotlib version 1.3.1 – DaveQ Aug 27 '15 at 18:58
  • Well, that's quite the mystery then. I wonder why it works for you but not for me, what version of MPL are you running? – C_Z_ Aug 27 '15 at 18:59
  • Update: This does actually work, the problem I was having was with time.sleep(), which I don't actually need. It works with new example I put up. Thanks! – C_Z_ Aug 28 '15 at 14:49
1

Try plt.pause(60) instead of time.sleep(60). Someone answering this question says that time.sleep doesn't work with Qt4Agg.

Community
  • 1
  • 1
Amy Teegarden
  • 3,842
  • 20
  • 23
  • I was actually just using time.sleep() for demonstration purposes, it's not actually in my real code. I've updated my example to be closer to my actual problem – C_Z_ Aug 28 '15 at 14:32