Skip to content

importing napari-matplotlib modifies how plots are shown in jupyter notebooks #64

Closed
@haesleinhuepf

Description

@haesleinhuepf

... and it should not.

The visualization of plots in matplotlib change after napari_matplotlib has been imported. Global settings e.g. here should not be modified by a napari-plugin. Consider programming napari-plugins side-effect free.

image

To reproduce:

import matplotlib.pyplot as plt
import numpy as np

image = np.random.random((3,3))
plt.imshow(image)
import napari_matplotlib
plt.imshow(image)

The same can be observed when not importing napari_matplotlib but instead opening a viewer where napari-stress is installed, because starting the viewer imports napari_stress and this imports napari_matplotlib in the background. A napari-plugin should not modify how things are displayed in jupyter notebooks. It's kind of evil usage of global variables.

CC @jo-mueller

Activity

haesleinhuepf

haesleinhuepf commented on Jan 28, 2023

@haesleinhuepf
Author

We had a similar issue earlier in another plugin and this is how we solved it:

https://github.com/BiAPoL/napari-clusters-plotter/blob/b917c1c7e01acf28b24fdbe64913e20588bed642/napari_clusters_plotter/_Qt_code.py#L257-L274

I hope this helps! :-)

jo-mueller

jo-mueller commented on Jan 30, 2023

@jo-mueller
Contributor

Hi @haesleinhuepf ,

I think a similar issue has been discussed here and I thought it was also seeb as not desirable to change the global matplotlib behavior. I've seen the same thing happening for other plots (napari-style color scheme) so the global variables must have been set somewhere else, anyway.

Another quick fix that I use a lot would be:

import matplotlib as mpl
mpl.style.use('default')
haesleinhuepf

haesleinhuepf commented on Jan 30, 2023

@haesleinhuepf
Author

That's a great workaround. Thanks, @jo-mueller .

so the global variables must have been set somewhere else, anyway.

Can you give an example and create a GitHub issue on the affected project?

jo-mueller

jo-mueller commented on Jan 30, 2023

@jo-mueller
Contributor

@haesleinhuepf

so the global variables must have been set somewhere else, anyway.

I mean more that the matplotlib global variables must have been set somewhere else within the scope of napari-matplotlib. I think here. A workaround would be to set the ax parameters (facecolors, etc) where the canvas.axes objects are created, e.g. here and here.

@dstansby would it be acceptable for you to define the display parameters in the __init__ of the respective "end-user" widget definitions? Doing so would probably also close #36.

added this to the 0.3.0 milestone on May 18, 2023
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

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      importing napari-matplotlib modifies how plots are shown in jupyter notebooks · Issue #64 · matplotlib/napari-matplotlib