Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 775b7f4

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

File tree

Expand file treeCollapse file tree

3 files changed

+24
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+24
-3
lines changed
+9Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+5-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/rcsetup.py
+10-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.