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 7285c4c

Browse filesBrowse files
committed
Merge pull request #4677 from jhamrick/set-fig-size
MNT: Set figure width and height with set_size_inches
2 parents cd22827 + 8524138 commit 7285c4c
Copy full SHA for 7285c4c

File tree

Expand file treeCollapse file tree

2 files changed

+32
-12
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-12
lines changed

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+9-12Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def figimage(self, X,
659659
self.stale = True
660660
return im
661661

662-
def set_size_inches(self, *args, **kwargs):
662+
def set_size_inches(self, w, h=None, forward=False):
663663
"""
664664
set_size_inches(w,h, forward=False)
665665
@@ -682,11 +682,10 @@ def set_size_inches(self, *args, **kwargs):
682682
matplotlib.Figure.get_size_inches
683683
"""
684684

685-
forward = kwargs.get('forward', False)
686-
if len(args) == 1:
687-
w, h = args[0]
688-
else:
689-
w, h = args
685+
# the width and height have been passed in as a tuple to the first
686+
# argument, so unpack them
687+
if h is None:
688+
w, h = w
690689

691690
dpival = self.dpi
692691
self.bbox_inches.p1 = w, h
@@ -766,23 +765,21 @@ def set_dpi(self, val):
766765
self.dpi = val
767766
self.stale = True
768767

769-
def set_figwidth(self, val):
768+
def set_figwidth(self, val, forward=False):
770769
"""
771770
Set the width of the figure in inches
772771
773772
ACCEPTS: float
774773
"""
775-
self.bbox_inches.x1 = val
776-
self.stale = True
774+
self.set_size_inches(val, self.get_figheight(), forward=forward)
777775

778-
def set_figheight(self, val):
776+
def set_figheight(self, val, forward=False):
779777
"""
780778
Set the height of the figure in inches
781779
782780
ACCEPTS: float
783781
"""
784-
self.bbox_inches.y1 = val
785-
self.stale = True
782+
self.set_size_inches(self.get_figwidth(), val, forward=forward)
786783

787784
def set_frameon(self, b):
788785
"""

‎lib/matplotlib/tests/test_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ def _as_mpl_axes(self):
158158
plt.close(fig)
159159

160160

161+
@cleanup
162+
def test_set_fig_size():
163+
fig = plt.figure()
164+
165+
# check figwidth
166+
fig.set_figwidth(5)
167+
assert_equal(fig.get_figwidth(), 5)
168+
169+
# check figheight
170+
fig.set_figheight(1)
171+
assert_equal(fig.get_figheight(), 1)
172+
173+
# check using set_size_inches
174+
fig.set_size_inches(2, 4)
175+
assert_equal(fig.get_figwidth(), 2)
176+
assert_equal(fig.get_figheight(), 4)
177+
178+
# check using tuple to first argument
179+
fig.set_size_inches((1, 3))
180+
assert_equal(fig.get_figwidth(), 1)
181+
assert_equal(fig.get_figheight(), 3)
182+
183+
161184
if __name__ == "__main__":
162185
import nose
163186
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.