Description
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

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