8000 Merge pull request #1685 from mspacek/savefig_directory · matplotlib/matplotlib@20cd99c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 20cd99c

Browse files
committed
Merge pull request #1685 from mspacek/savefig_directory
Add default savefig directory
2 parents 84901ec + 9a305a4 commit 20cd99c

File tree

8 files changed

+55
-8
lines changed

8 files changed

+55
-8
lines changed

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2013-01-23 Add 'savefig.directory' to rcParams to remember and fill in the last
2+
directory saved to for figure save dialogs - Martin Spacek
3+
14
2013-01-08 Added two extra titles to axes which are flush with the left and
25
right edges of the plot respectively.
36
Andrew Dawson
@@ -38,7 +41,7 @@
3841
_get_rbg_face except it return a (r,g,b,a) tuble, to line2D.
3942
Modified Line2D.draw to use _get_rbga_face to get the markerface
4043
color so that any alpha set by markerfacecolor will respected.
41-
- Thomas Caswell
44+
- Thomas Caswell
4245

4346
2012-11-13 Add a symmetric log normalization class to colors.py.
4447
Also added some tests for the normalization class.

doc/users/whats_new.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ They may be symmetric or weighted.
2929

3030
.. plot:: mpl_examples/pylab_examples/stackplot_demo2.py
3131

32+
Remember save directory
33+
-----------------------
34+
Martin Spacek made the save figure dialog remember the last directory saved
35+
to. The default is configurable with the new `savefig.directory` setting
36+
in `matplotlibrc`.
37+
3238
Initialize a rotated rectangle
3339
------------------------------
3440
Damon McDougall extended the :class:`~matplotlib.patches.Rectangle` constructor

lib/matplotlib/backends/backend_gtk.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ def get_filechooser(self):
732732
fc = FileChooserDialog(
733733
title='Save the figure',
734734
parent=self.win,
735+
path=os.path.expanduser(rcParams.get('savefig.directory', '')),
735736
filetypes=self.canvas.get_supported_filetypes(),
736737
default_filetype=self.canvas.get_default_filetype())
737738
fc.set_current_name(self.canvas.get_default_filename())
@@ -740,6 +741,13 @@ def get_filechooser(self):
740741
def save_figure(self, *args):
741742
fname, format = self.get_filechooser().get_filename_from_user()
742743
if fname:
744+
startpath = os.path.expanduser(rcParams.get('savefig.directory', ''))
745+
if startpath == '':
746+
# explicitly missing key or empty str signals to use cwd
747+
rcParams['savefig.directory'] = startpath
748+
else:
749+
# save dir for next time
750+
rcParams['savefig.directory'] = os.path.dirname(unicode(fname))
743751
try:
744752
self.canvas.print_figure(fname, format=format)
745753
except Exception as e:
@@ -1010,8 +1018,8 @@ def save_figure(self, *args):
10101018

10111019

10121020
class FileChooserDialog(gtk.FileChooserDialog):
1013-
"""GTK+ 2.4 file selector which remembers the last file/directory
1014-
selected and presents the user with a menu of supported image formats
1021+
"""GTK+ 2.4 file selector which presents the user with a menu
1022+
of supported image formats
10151023
"""
10161024
def __init__ (self,
10171025
title = 'Save file',

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ def get_filechooser(self):
537537
fc = FileChooserDialog(
538538
title='Save the figure',
539539
parent=self.win,
540+
path=os.path.expanduser(rcParams.get('savefig.directory', '')),
540541
filetypes=self.canvas.get_supported_filetypes(),
541542
default_filetype=self.canvas.get_default_filetype())
542543
fc.set_current_name(self.canvas.get_default_filename())
@@ -545,6 +546,13 @@ def get_filechooser(self):
545546
def save_figure(self, *args):
546547
fname, format = self.get_filechooser().get_filename_from_user()
547548
if fname:
549+
startpath = os.path.expanduser(rcParams.get('savefig.directory', ''))
550+
if startpath == '':
551+
# explicitly missing key or empty str signals to use cwd
552+
rcParams['savefig.directory'] = startpath
553+
else:
554+
# save dir for next time
555+
rcParams['savefig.directory'] = os.path.dirname(unicode(fname))
548556
try:
549557
self.canvas.print_figure(fname, format=format)
550558
except Exception, e:

lib/matplotlib/backends/backend_qt4.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ def save_figure(self, *args):
644644
sorted_filetypes.sort()
645645
default_filetype = self.canvas.get_default_filetype()
646646

647-
start = self.canvas.get_default_filename()
647+
startpath = matplotlib.rcParams.get('savefig.directory', '')
648+
startpath = os.path.expanduser(startpath)
649+
start = os.path.join(startpath, self.canvas.get_default_filename())
648650
filters = []
649651
selectedFilter = None
650652
for name, exts in sorted_filetypes:
@@ -657,6 +659,12 @@ def save_figure(self, *args):
657659
fname = _getSaveFileName(self, "Choose a filename to save to",
658660
start, filters, selectedFilter)
659661
if fname:
662+
if startpath == '':
663+
# explicitly missing key or empty str signals to use cwd
664+
matplotlib.rcParams['savefig.directory'] = startpath
665+
else:
666+
# save dir for next time
667+
matplotlib.rcParams['savefig.directory'] = os.path.dirname(unicode(fname))
660668
try:
661669
self.canvas.print_figure( unicode(fname) )
662670
except Exception as e:

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,17 +895,27 @@ def save_figure(self, *args):
895895
# work - JDH
896896
#defaultextension = self.canvas.get_default_filetype()
897897
defaultextension = ''
898+
initialdir = rcParams.get('savefig.directory', '')
899+
initialdir = os.path.expanduser(initialdir)
900+
initialfile = self.canvas.get_default_filename()
898901
fname = asksaveasfilename(
899902
master=self.window,
900903
title='Save the figure',
901-
filetypes = tk_filetypes,
902-
defaultextension = defaultextension,
903-
initialfile=self.canvas.get_default_filename(),
904+
filetypes=tk_filetypes,
905+
defaultextension=defaultextension,
906+
initialdir=initialdir,
907+
initialfile=initialfile,
904908
)
905909

906910
if fname == "" or fname == ():
907911
return
908912
else:
913+
if initialdir == '':
914+
# explicitly missing key or empty str signals to use cwd
915+
rcParams['savefig.directory'] = initialdir
916+
else:
917+
# save dir for next time
918+
rcParams['savefig.directory'] = os.path.dirname(unicode(fname))
909919
try:
910920
# This method will handle the delegation to the correct type
911921
self.canvas.print_figure(fname)

lib/matplotlib/rcsetup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,8 @@ def __call__(self, s):
686686
# options are 'tight', or 'standard'. 'standard' validates to None.
687687
'savefig.bbox': [None, validate_bbox],
688688
'savefig.pad_inches': [0.1, validate_float],
689+
# default directory in savefig dialog box
690+
'savefig.directory': ['~', unicode],
689691

690692
# Maintain shell focus for TkAgg
691693
'tk.window_focus': [False, validate_bool],

matplotlibrc.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
366366
#savefig.format : png # png, ps, pdf, svg
367367
#savefig.bbox : standard # 'tight' or 'standard'.
368368
#savefig.pad_inches : 0.1 # Padding to be used when bbox is set to 'tight'
369+
#savefig.directory : ~ # default directory in savefig dialog box,
370+
# leave empty to always use current working directory
369371

370372
# tk backend params
371373
#tk.window_focus : False # Maintain shell focus for TkAgg
@@ -450,6 +452,6 @@ text.hinting_factor : 8 # Specifies the amount of softness for hinting in the
450452
# $PATH is searched
451453
#animation.avconv_args: '' # Additional arguments to pass to avconv
452454
#animation.mencoder_path: 'mencoder'
453-
# Path to mencoder binary. Without full path
455+
# Path to mencoder binary. Without full path
454456
# $PATH is searched
455457
#animation.mencoder_args: '' # Additional arguments to pass to mencoder

0 commit comments

Comments
 (0)
0