Skip to content

[Bug]: Cannot save figure with GTK4 on Mac #26551

Open

Description

@stevezhang1999

Bug summary

When trying to save figure with GTK4 on Mac, the file format is always pgf regardless what we have selected. Furthermore, if the saving pgf fails, an error box is displayed and cannot be closed. I tried to investigate what was happening there but haven't fixed it yet. (See additional information)

No matter what we have selected, format is always pgf when saving
image

image

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

print(mpl.__file__)
print(mpl.__version__)
mpl.use('gtk4agg')

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

Actual outcome

Save fig fails

Expected outcome

Save fig success

Additional information

pygobject 3.44.1
gtk4: stable 4.12.0

For the "always pgf" bug:

/lib/matplotlib/backends/backend_gtk4.py line 361

dialog.set_choice('format', formats[default_format])

should be

dialog.set_choice('format', formats[0])

full code snippet:
dialog is a Gtk.FileChooserNative instance.

formats = [formats[default_format], *formats[:default_format],
                   *formats[default_format+1:]]
dialog.add_choice('format', 'File format', formats, formats)
dialog.set_choice('format', formats[default_format])

dialog.set_current_folder(Gio.File.new_for_path(
    os.path.expanduser(mpl.rcParams['savefig.directory'])))
dialog.set_current_name(self.canvas.get_default_filename())

However, after fixing this, it always resolve to PNG (the correct default choice.) I suspect dialog.add_choice() cannot work correctly on Mac or doesn't work anymore.

The easiest fix is extracting the file extension via dialog.get_file() which is already at line 372 in /lib/matplotlib/backends/backend_gtk4.py line 361

P. S.
I tried to replace it with Gtk.FileChooserNative.get_filter(), but it doesn't work and it's probably a confirmed bug in GTK4https://gitlab.gnome.org/GNOME/gtk/-/issues/1820

Operating system

Mac

Matplotlib Version

3.8.0.dev1829+g3d2d0727ec.d20230818

Matplotlib Backend

gtk4agg

Python version

3.9.6

Jupyter version

No response

Installation

None

Activity

  1. stevezhang1999 commented on Aug 20, 2023

    @stevezhang1999
    ContributorAuthor

    Today I tested GTK 3 and GTK 4 on Ubuntu. GTK3 works well. GTK4 works well and the "always default" bug doesn't occur as on Mac. So the bug that file filter doesn't work may be a Mac only bug. Also, the different behavior on different platforms make the bug hard to fix:

    Above, I suggested. extracting the file extension with dialog.get_file(). However, the file name retrieved from dialog.get_file() wasn't automatically set with the selected extension format - the behavior of getting file name on Ubuntu and on Mac is different. (See P. S. for more detailed description)
    Furthermore, the GTK4 documentation doesn't state that what kind of behavior is expected or guaranteed. Thus, the solution proposed above about extracting the file extension with dialog.get_file() won't guarantee portability.

    P.S.

    Example using GTK4:
    With GTK4, after we select SVG format from the filter, the file name won't be updated with the format we selected. We have to manually edit the file name before clicking "Save"

    Example using Mac:
    With Mac, the default file name displayed in the text input box for user is without an extension - e.g., "Figure_1". After clicking save, the app will add the extension for us - dialog.get_file().get_path() will give something like "Figure_1.png"

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions