8000 Merge pull request #15293 from anntzer/wxsavefig · matplotlib/matplotlib@d546347 · GitHub
[go: up one dir, main page]

Skip to content

Commit d546347

Browse files
authored
Merge pull request #15293 from anntzer/wxsavefig
Fixes for wx savefig dialog.
2 parents 6d45118 + e55dcfc commit d546347

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import logging
1111
import math
12-
import os.path
12+
import pathlib
1313
import sys
1414
import weakref
1515

@@ -1374,29 +1374,28 @@ def save_figure(self, *args):
13741374
# Fetch the required filename and file type.
13751375
filetypes, exts, filter_index = self.canvas._get_imagesave_wildcards()
13761376
default_file = self.canvas.get_default_filename()
1377-
dlg = wx.FileDialog(self.canvas.GetParent(),
1378-
"Save to file", "", default_file, filetypes,
1379-
wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
1377+
dlg = wx.FileDialog(
1378+
self.canvas.GetParent(), "Save to file",
1379+
rcParams["savefig.directory"], default_file, filetypes,
1380+
wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
13801381
dlg.SetFilterIndex(filter_index)
13811382
if dlg.ShowModal() == wx.ID_OK:
1382-
dirname = dlg.GetDirectory()
1383-
filename = dlg.GetFilename()
1384-
_log.debug('%s - Save file dir:%s name:%s',
1385-
self, dirname, filename)
1386-
format = exts[dlg.GetFilterIndex()]
1387-
basename, ext = os.path.splitext(filename)
1388-
if ext.startswith('.'):
1389-
ext = ext[1:]
1390-
if ext in ('svg', 'pdf', 'ps', 'eps', 'png') and format != ext:
1383+
path = pathlib.Path(dlg.GetPath())
1384+
_log.debug('%s - Save file path: %s', type(self), path)
1385+
fmt = exts[dlg.GetFilterIndex()]
1386+
ext = path.suffix[1:]
1387+
if ext in self.canvas.get_supported_filetypes() and fmt != ext:
13911388
# looks like they forgot to set the image type drop
13921389
# down, going with the extension.
13931390
_log.warning('extension %s did not match the selected '
13941391
'image type %s; going with %s',
1395-
ext, format, ext)
1396-
format = ext
1392+
ext, fmt, ext)
1393+
fmt = ext
1394+
# Save dir for next time, unless empty str (which means use cwd).
1395+
if rcParams["savefig.directory"]:
1396+
rcParams["savefig.directory"] = str(path.parent)
13971397
try:
1398-
self.canvas.figure.savefig(
1399-
os.path.join(dirname, filename), format=format)
1398+
self.canvas.figure.savefig(str(path), format=fmt)
14001399
except Exception as e:
14011400
error_msg_wx(str(e))
14021401

0 commit comments

Comments
 (0)
0