Skip to content

Axes.imshow tooltip does not get updated when another call to Axes.imshow is made #12282

Closed

Description

@jatentaki

Bug report

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

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 = (showing + 1) % 2
    ax.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.

Matplotlib version

  • Operating system: Xubuntu 18.04
  • Matplotlib version: 2.1.2
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.6

Default conda channel.

Activity

  1. ImportanceOfBeingErnest commented on Sep 25, 2018

    @ImportanceOfBeingErnest
    Member

    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.

  2. jatentaki commented on Oct 6, 2018

    @jatentaki
    Author

    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.

  3. ImportanceOfBeingErnest commented on Oct 6, 2018

    @ImportanceOfBeingErnest
    Member

    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()
    
  4. ImportanceOfBeingErnest commented on Oct 6, 2018

    @ImportanceOfBeingErnest
    Member

    If you update to matplotlib 3.0.0 it'll work as you expect it even with the code from the question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions