Skip to content

Saving figure has no ticks or axis labels #231

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
Goobley opened this issue Nov 15, 2023 · 4 comments
Closed

Saving figure has no ticks or axis labels #231

Goobley opened this issue Nov 15, 2023 · 4 comments

Comments

@Goobley
Copy link

Goobley commented Nov 15, 2023

I'm building a napari plugin that computes some derived data and plots that in a pane (so we're not directly hooking the plot up to the layer content). I am trying to use the BaseNapariMPLWidget to have a single plot with toolbar, rather than just docking the figure canvas. I also want to be able to save the figure, however the none of the the ticks, ticklabels, or axis labels appear in a png of the figure.

Minimal example
import matplotlib.pyplot as plt
import napari
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from napari_matplotlib.base import BaseNapariMPLWidget

if __name__ == '__main__':
    viewer = napari.Viewer()

    complete_widget = BaseNapariMPLWidget(viewer)
    complete_widget.add_single_axes()
    complete_widget.axes.plot(np.arange(10), np.sin(np.arange(10)))
    viewer.window.add_dock_widget(complete_widget, name="Complete")

    figure = plt.figure()
    ax = figure.gca()
    ax.plot(np.arange(10), np.sin(np.arange(10)))
    canvas_widget = FigureCanvasQTAgg(figure)
    viewer.window.add_dock_widget(canvas_widget, name="Canvas")

    complete_widget.figure.savefig("test_napari_mpl.png", dpi=300)
    figure.savefig("test_canvas.png", dpi=300)

    napari.run()

Happy to submit a PR with a bit of guidance as where to start. Is this in the choice of canvas?

@Goobley
Copy link
Author

Goobley commented Nov 15, 2023

Apologies, I realised, whilst adding a screenshot that they are present, but weren't visible against the light mode of my image viewer!

@Goobley Goobley closed this as completed Nov 15, 2023
@samcunliffe
Copy link
Collaborator

Would you mind posting a screenshot? I thought I'd fixed this.

@Goobley
Copy link
Author

Goobley commented Nov 16, 2023

Here we go. Ultimately it stemmed from napari being in dark mode, but my image viewer being in light mode, with the plot having a transparent background. My fix has been to force a light mode stylesheet for napari-matplotlib.

test_napari_mpl

Here napari matplotlib is at the top, correctly detecting style as intended (from reading the code).

napari-window

I don't believe there's any real fix for this, unless a separate savefig was added to BaseNapariMPLWidget that overrides the tickcolor etc before saving.

@samcunliffe
Copy link
Collaborator

Ah right, yes.
You will indeed get white lines and ticks with a transparent background if you save straight from the dark mode style.

We put in a way to have custom stylesheet treated at highest precedence, if that's any use (perhaps you already found this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants