Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/users/whats_new/updated_figure.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Updated Figure.savefig()
------------------------

Added support to save the figure with the same dpi as the figure on the screen using dpi='figure'

Example:
f = plt.figure(dpi=25) # dpi set to 25
S = plt.scatter([1,2,3],[4,5,6])
f.savefig('output.png', dpi='figure') # output savefig dpi set to 25 (same as figure)
7 changes: 5 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,9 +1424,10 @@ def savefig(self, *args, **kwargs):

Keyword arguments:

*dpi*: [ *None* | ``scalar > 0`` ]
*dpi*: [ *None* | ``scalar > 0`` | 'figure']
The resolution in dots per inch. If *None* it will default to
the value ``savefig.dpi`` in the matplotlibrc file.
the value ``savefig.dpi`` in the matplotlibrc file. If 'figure'
it will set the dpi to be the value of the figure.

*facecolor*, *edgecolor*:
the colors of the figure rectangle
Expand Down Expand Up @@ -1473,6 +1474,8 @@ def savefig(self, *args, **kwargs):
"""

kwargs.setdefault('dpi', rcParams['savefig.dpi'])
if kwargs['dpi'] == 'figure':
kwargs['dpi'] = self.get_dpi()
frameon = kwargs.pop('frameon', rcParams['savefig.frameon'])
transparent = kwargs.pop('transparent',
rcParams['savefig.transparent'])
Expand Down
11 changes: 10 additions & 1 deletion lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def validate_float_or_None(s):
except ValueError:
raise ValueError('Could not convert "%s" to float' % s)

def validate_dpi(s):
"""confirm s is string 'figure' or convert s to float or raise"""
if s == 'figure':
return s
try:
return float(s)
except ValueError:
raise ValueError('"%s" is not string "figure" or'
' could not convert "%s" to float' % (s, s))

def validate_int(s):
"""convert s to int or raise"""
Expand Down Expand Up @@ -749,7 +758,7 @@ def __call__(self, s):
closedmax=False)],

## Saving figure's properties
'savefig.dpi': [100, validate_float], # DPI
'savefig.dpi': [100, validate_dpi], # DPI
'savefig.facecolor': ['w', validate_color], # facecolor; white
'savefig.edgecolor': ['w', validate_color], # edgecolor; white
'savefig.frameon': [True, validate_bool],
Expand Down