FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ENH]: imshow support for multiple slice image volume · Issue #24360 · matplotlib/matplotlib · GitHub

Repository navigation

[ENH]: imshow support for multiple slice image volume #24360

Closed

Description

Problem

I need to display an image volume with multiple slices, similar to sliceViewer in Matlab. However, the current imshow function only supports RBG(A) or 2D scalar data.

Proposed solution

The code example here shows how to display multiple slice images, but there is a lot of code to write, which is not convenient. Maybe one solution is to provide a wrap function (i.e. imshow_3d or slice_viewer) that implements the example code somehow?

Activity

  1. ustcfdm commented on Nov 4, 2022

    Author

    I just tried to write a wrap function for the example code here, but the scroll wheel doesn't work. The reason is beyond my knowledge. Does anyone know why?

  2. oscargus commented on Nov 4, 2022

    Member

    Does the example work if you just run the example code as is? (Just to work out if it is an OS/backend issue or not. For reference it works for me on CentOS 7 with QtAgg.)

  3. ustcfdm commented on Nov 4, 2022

    Author

    Does the example work if you just run the example code as is? (Just to work out if it is an OS/backend issue or not. For reference it works for me on CentOS 7 with QtAgg.)

    Thanks for your reply. I figured that I have to return and receive the variable tracker to make the scroll wheel work. I guess this is related to the life cycle of variable tracker? An example code is below:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Fixing random state for reproducibility
    np.random.seed(19680801)
    
    class IndexTracker:
        def __init__(self, ax, X):
            self.ax = ax
            ax.set_title('use scroll wheel to navigate images')
    
            self.X = X
            rows, cols, self.slices = X.shape
            self.ind = self.slices//2
    
            self.im = ax.imshow(self.X[:, :, self.ind])
            self.update()
    
        def on_scroll(self, event):
            print("%s %s" % (event.button, event.step))
            if event.button == 'up':
                self.ind = (self.ind + 1) % self.slices
            else:
                self.ind = (self.ind - 1) % self.slices
            self.update()
    
        def update(self):
            self.im.set_data(self.X[:, :, self.ind])
            self.ax.set_ylabel('slice %s' % self.ind)
            self.im.axes.figure.canvas.draw()
    
    
    def imshow_3d(img):
        fig, ax = plt.subplots()
        tracker = IndexTracker(ax, img)
        fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
        
        return tracker
    
    
    X = np.random.rand(20, 20, 40)
    
    # scroll wheel does not work
    imshow_3d(X)
    
    # scroll wheel works
    whatever = imshow_3d(X)

    This is not an elegant solution though, because I have to return the variable tracker. In addition, the y label is occupied by slice index. An ideal solution is something like sliceViewer in Matlab, which has a slider to allow changing slices.

  4. timhoffm commented on Nov 4, 2022

    Member

    The example exists primarily to illustrate event handling. It is intentional minimal and not designed as a fully-fledged image viewer.

    In scope:

    • we should describe that the user has to keep a reference to the tracker so that it lives for the whole lifetime of the figure.

    Out of scope:

    • Matplotlib provides the basic building blocks for plotting. We won't provide a native image slice viewer ourselves. This should be done by a third party package building on top of matplotlib.
  5. ksunden commented on Nov 4, 2022

    Member

    Also, if your primary goal is a nice, interactive, viewer for multidimensional image data, you may be interested in https://napari.org

  6. story645 commented on Nov 4, 2022

    Member

    Matplotlib provides the basic building blocks for plotting. We won't provide a native image slice viewer ourselves. This should be done by a third party package building on top of matplotlib.

    Depending on the domain, maybe xarray or mne/nipy related packages might also fit the bill.

  7. tacaswell commented on Nov 4, 2022

    Member

    I guess this is related to the life cycle of variable tracker? An example code is below:

    Yes, we only hold onto weak refs to the objects when the callbacks get registered so when the object get garbage collected the callbacks go away and hence no interaction. (There has been some discussion of changing that so our callbacks hold hard references but that is well outside of scope here).

    In addition to (as time suggests) making it clear in this example you need to keep the tracker alive, it might be worth adding a linked slider from matplotlib.widgets?

  8. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Nov 4, 2022
  9. tacaswell commented on Nov 4, 2022

    Member

    Tasks:

    • add note about keeping tracker alive (easy difficulty)
    • add linked slider to example (medium difficulty)

    Adding the slider is medium because it will require linking some callbacks and convincing everyone it is a good idea ;)

  10. removed
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Nov 4, 2022
  11. timhoffm commented on Nov 4, 2022

    Member

    I'm -0.5 on adding a slider. This makes the example much more complex and is outside the scope of the event-handing topic.

  12. jklymak commented on Nov 4, 2022

    Member

    I wonder if the title of the example should be changed? If you search image slicing in Python on Google, you get this page which is not very good for the stated purpose and not the actual point of the example.

  13. timhoffm commented on Nov 4, 2022

    Member

    I don't get Matplotlib on the first page of google results 🤷‍♂️

    Anyway, changing the title is a good idea -> done in #24366
    For simplicity, I've kept the filename.

  14. added this to the v3.6.3 milestone on Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL