Using local install of current git master - the following code works with the tkagg backend, but raises an error with the macosx backend:
import matplotlib
matplotlib.use('macosx')
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
im = plt.imshow([[1, 2], [3, 4]])
fig.canvas.draw()
ax.draw_artist(im)
Error for macosx backend is:
Traceback (most recent call last):
File "draw_artist.py", line 8, in <module>
ax.draw_artist(im)
File "/Users/mb312/dev_trees/matplotlib/lib/matplotlib/axes/_base.py",
line 2423, in draw_artist
raise AttributeError(msg)
AttributeError: draw_artist can only be used after an initial draw
which caches the render
It seems to me that the previous call fig.canvas.draw() is the "initial draw" that should cache the renderer. So, is this a bug?
For osx, draw does not actually run the renderer, it tells the GUI manager that the window is invalid and to please re-draw it at it's earliest convenience. The actual rendering happens in a callback from the GUI thread.
Once the figure is displayed on the screen once, it will work.
Sorry to expose my ignorance, but how can I make sure that the figure is displayed on the screen? plt.show() before draw_artist in the above script does not work, for example. Is there any way of forcing the code to build a renderer?
It's the image viewer we have here. Basically it shows several panels showing different slices from a 3D image, with crosshairs on top of the image. When the user clicks the crosshairs, it changes the position of the crosshairs, and the slices displayed. We use draw_artist to change the slices and crosshairs. When we initialize the viewer, and thence call the slice / crosshair display function here - the viewer crashes with the message above.
I wonder if #9495 helps at all with this. With the simple script above it still crashes, but I wonder if flushing the events helps in more of a real-world case. I'm also able to get the above script to work just fine with a plt.pause(0.0001)--I don't know if that's of any help to you guys, though.
Activity
tacaswell commented on Dec 2, 2016
For osx, draw does not actually run the renderer, it tells the GUI manager that the window is invalid and to please re-draw it at it's earliest convenience. The actual rendering happens in a callback from the GUI thread.
Once the figure is displayed on the screen once, it will work.
matthew-brett commented on Dec 2, 2016
Sorry to expose my ignorance, but how can I make sure that the figure is displayed on the screen? plt.show() before draw_artist in the above script does not work, for example. Is there any way of forcing the code to build a renderer?
tacaswell commented on Dec 2, 2016
What is the use case driving this question?
matthew-brett commented on Dec 3, 2016
It's the image viewer we have here. Basically it shows several panels showing different slices from a 3D image, with crosshairs on top of the image. When the user clicks the crosshairs, it changes the position of the crosshairs, and the slices displayed. We use draw_artist to change the slices and crosshairs. When we initialize the viewer, and thence call the slice / crosshair display function here - the viewer crashes with the message above.
dopplershift commented on Oct 24, 2017
I wonder if #9495 helps at all with this. With the simple script above it still crashes, but I wonder if flushing the events helps in more of a real-world case. I'm also able to get the above script to work just fine with a plt.pause(0.0001)--I don't know if that's of any help to you guys, though.