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

Skip to content

Commit 775b7f4

Browse files
committed
Merge pull request matplotlib#4286 from patchen/dpi_fix
ENH : Added native dpi option for print_figure
2 parents bbd5f4c + 1f25f15 commit 775b7f4

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Updated Figure.savefig()
2+
------------------------
3+
4+
Added support to save the figure with the same dpi as the figure on the screen using dpi='figure'
5+
6+
Example:
7+
f = plt.figure(dpi=25) # dpi set to 25
8+
S = plt.scatter([1,2,3],[4,5,6])
9+
f.savefig('output.png', dpi='figure') # output savefig dpi set to 25 (same as figure)

lib/matplotlib/figure.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,9 +1432,10 @@ def savefig(self, *args, **kwargs):
14321432
14331433
Keyword arguments:
14341434
1435-
*dpi*: [ *None* | ``scalar > 0`` ]
1435+
*dpi*: [ *None* | ``scalar > 0`` | 'figure']
14361436
The resolution in dots per inch. If *None* it will default to
1437-
the value ``savefig.dpi`` in the matplotlibrc file.
1437+
the value ``savefig.dpi`` in the matplotlibrc file. If 'figure'
1438+
it will set the dpi to be the value of the figure.
14381439
14391440
*facecolor*, *edgecolor*:
14401441
the colors of the figure rectangle
@@ -1481,6 +1482,8 @@ def savefig(self, *args, **kwargs):
14811482
"""
14821483

14831484
kwargs.setdefault('dpi', rcParams['savefig.dpi'])
1485+
if kwargs['dpi'] == 'figure':
1486+
kwargs['dpi'] = self.get_dpi()
14841487
frameon = kwargs.pop('frameon', rcParams['savefig.frameon'])
14851488
transparent = kwargs.pop('transparent',
14861489
rcParams['savefig.transparent'])

lib/matplotlib/rcsetup.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def validate_float_or_None(s):
117117
except ValueError:
118118
raise ValueError('Could not convert "%s" to float' % s)
119119

120+
def validate_dpi(s):
121+
"""confirm s is string 'figure' or convert s to float or raise"""
122+
if s == 'figure':
123+
return s
124+
try:
125+
return float(s)
126+
except ValueError:
127+
raise ValueError('"%s" is not string "figure" or'
128+
' could not convert "%s" to float' % (s, s))
120129

121130
def validate_int(s):
122131
"""convert s to int or raise"""
@@ -749,7 +758,7 @@ def __call__(self, s):
749758
closedmax=False)],
750759

751760
## Saving figure's properties
752-
'savefig.dpi': [100, validate_float], # DPI
761+
'savefig.dpi': [100, validate_dpi], # DPI
753762
'savefig.facecolor': ['w', validate_color], # facecolor; white
754763
'savefig.edgecolor': ['w', validate_color], # edgecolor; white
755764
'savefig.frameon': [True, validate_bool],

0 commit comments

Comments
 (0)