Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
haesleinhuepf opened this issue Jan 28, 2023 · 4 comments · Fixed by #65
Closed
Labels
Bug Something isn't working
Milestone

Comments

@haesleinhuepf
Copy link

... 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

@haesleinhuepf
Copy link
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
Copy link
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
Copy link
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
Copy link
Contributor

jo-mueller commented Jan 30, 2023

@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.

@dstansby dstansby added the Bug Something isn't working label Jan 30, 2023
@dstansby dstansby added this to the 0.3.0 milestone May 18, 2023
samcunliffe added a commit to samcunliffe/napari-matplotlib that referenced this issue Jun 12, 2023
samcunliffe added a commit to samcunliffe/napari-matplotlib that referenced this issue Jun 13, 2023
Use plt.subplots for safety.

And no longer need copy.deepcopy.

Save eyes and make a lovely histogram.
samcunliffe added a commit to samcunliffe/napari-matplotlib that referenced this issue Jun 13, 2023
Use plt.subplots for safety.

And no longer need copy.deepcopy.

Save eyes and make a lovely histogram.
samcunliffe added a commit to samcunliffe/napari-matplotlib that referenced this issue Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants