You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Axes.imshow tooltip does not get updated when another call to Axes.imshow is made #12282
Bug summary
When changing an imshow picture via imshow -> draw, the tooltip at the bottom of the figure fails to update and keeps reporting values as they were in the previous image.
Code for reproduction
importnumpyasnpimportmatplotlib.pyplotasplt# create two images which are different enough to easily tell them apartimg1=np.sin(np.linspace(-4, 4, 50)).reshape(-1, 1) +np.zeros((1, 50))
img2=img1.Timgs= [img1, img2]
showing=0fig, ax=plt.subplots()
ax.imshow(imgs[showing])
defswap(*_args):
''' swap the two images in the figure '''globalshowingshowing= (showing+1) %2ax.imshow(imgs[showing])
plt.draw()
fig.canvas.mpl_connect('key_press_event', swap)
plt.show()
Actual outcome
The image with horizontal stripes displays fine, and the tooltip behaves as it should: it changes when moving the mouse vertically and remains constant when moving horizontally. Once we push any key to trigger the update, the image changes, but the tooltip remains as it was, now contradicting what is seen on the picture.
Expected outcome
The tooltip should change along with the image being updated, to always stay in accordance with the displayed data.
Note 1
I am aware that there are more elegant solutions, such as set_data, which solve the problem, but repeating the imshow call is definitely the most straightforward, and since it silently does only half the job, I consider it issue-worthy.
Note 2
While troubleshooting this issue, I found another one, reported in #12279. These may be related.
Once you press a key, you add another image to the axes. It will then contain two images. This naturally leads to the question: Which of those two images should the tooltip refer to? The original one? the one that was last added? Or the one with the highest z-order?
I feel like while matplotlib may currently not make the best choice here, any of the options may have a fan base and you can't satisfy everyone.
It should also be noted that indeed using set_data on a single image is more efficient and would hence be what I would recommend to do anyways.
I would argue that it should reflect the highest z-order one, which also happens to be the latest, unless the user consciously changed the z-order (in which case he probably knows what he's doing).
That being said, I now understand that this is a feature and not a bug and close the issue.
I would argue that it should reflect the highest z-order one
That's what it does with your version of matplotlib, right? The problem here is that both images have equal z-order.
for im in ax.images:
print(im.get_zorder())
prints 0 0. You can set the zorder to something different. E.g. like this:
import numpy as np
import matplotlib.pyplot as plt
# create two images which are different enough to easily tell them apart
img1 = np.sin(np.linspace(-4, 4, 50)).reshape(-1, 1) + np.zeros((1, 50))
img2 = img1.T
imgs = [img1, img2]
showing = 0
fig, ax = plt.subplots()
ax.imshow(imgs[showing])
def swap(*_args):
''' swap the two images in the figure '''
global showing
showing += 1
ax.imshow(imgs[showing%2], zorder=showing/100.)
for im in ax.images:
print(im.get_zorder())
plt.draw()
fig.canvas.mpl_connect('key_press_event', swap)
plt.show()
Activity
ImportanceOfBeingErnest commentedon Sep 25, 2018
Once you press a key, you add another image to the axes. It will then contain two images. This naturally leads to the question: Which of those two images should the tooltip refer to? The original one? the one that was last added? Or the one with the highest z-order?
I feel like while matplotlib may currently not make the best choice here, any of the options may have a fan base and you can't satisfy everyone.
It should also be noted that indeed using
set_dataon a single image is more efficient and would hence be what I would recommend to do anyways.jatentaki commentedon Oct 6, 2018
I would argue that it should reflect the highest z-order one, which also happens to be the latest, unless the user consciously changed the z-order (in which case he probably knows what he's doing).
That being said, I now understand that this is a feature and not a bug and close the issue.
ImportanceOfBeingErnest commentedon Oct 6, 2018
That's what it does with your version of matplotlib, right? The problem here is that both images have equal z-order.
prints
0 0. You can set the zorder to something different. E.g. like this:ImportanceOfBeingErnest commentedon Oct 6, 2018
If you update to matplotlib 3.0.0 it'll work as you expect it even with the code from the question.