Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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

Open
stevezhang1999 opened this issue Aug 18, 2023 · 1 comment · May be fixed by #26558
Open

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

stevezhang1999 opened this issue Aug 18, 2023 · 1 comment · May be fixed by #26558
Labels

Comments

@stevezhang1999
Copy link
Contributor

stevezhang1999 commented Aug 18, 2023

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

@stevezhang1999
Copy link
Contributor Author

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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants