Matplotlib pyplot show () does not work after closing

I have such a cycle

#!/usr/bin/env python import matplotlib.pyplot as p for i in xrange(N): # Create my_image here # Display this image p.figure() p.imshow(my_image) p.show() p.close() 

This works fine with i = 0. For the program to continue, I need to close the new shape created by pyplot. For all other iterations of the loop (i> 0), another new figure is not created, the graph is not displayed, and the program simply proceeds. Why is closing the figure creating the forge unable to open a new one (e.g. MATLAB)?

The behavior I expect is as follows:

  • Execution stops at p.show ()
  • When I close the shape, execution continues
  • When p.show () is encountered again, a new image is displayed.
  • Repeat step 2 until more graph appears to show
+6
python matplotlib
source share
4 answers

This may be due to an error in previous versions of matplotlib. I had a similar problem when I issued sequential show() commands - only the first will show (and stay); but when I updated matplotlib to 1.0.1, the problem disappeared.

+3
source share

There may be a better way to revive imshow, but this should work as a last resort. This is a slightly modified version of the animation example.

+5
source share

After I applied the unutbu example, I found a behavior that I could normally debug with PyDev, where I could gradually see graphs.

 import time, threading import numpy from matplotlib.pyplot import * x = numpy.linspace(0, 10) y = x**2 def main(): plot(x, x) draw() time.sleep(2) plot(x, y) draw() thread = threading.Thread() thread.run = main manager = get_current_fig_manager() manager.window.after(100, thread.start) figure(1) show() 
0
source share

I have been looking for this problem for a long time, and I may have a solution, although I have not tested it yet.

The key in writing code is more like MatLab, name your numbers and then call them to show ().

eg.

  from matplotlib import pyplot as plt fig1 = plt.figure() fig2 = plt.figure() fig1.show() fig2.show() 

This can work for animation and construction at different stages.

0
source share

All Articles