Skip to content

Commit d6848b1

Browse files
Yunfei YangYunfei Yang
authored andcommitted
Added square plot parameter to axes
1 parent a6af675 commit d6848b1

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,13 +1299,15 @@ def axis(self, *v, **kwargs):
12991299
ymin, ymax = self.get_ylim()
13001300
return xmin, xmax, ymin, ymax
13011301

1302+
emit = kwargs.get('emit', True)
1303+
13021304
if len(v) == 1 and is_string_like(v[0]):
13031305
s = v[0].lower()
13041306
if s == 'on':
13051307
self.set_axis_on()
13061308
elif s == 'off':
13071309
self.set_axis_off()
1308-
elif s in ('equal', 'tight', 'scaled', 'normal', 'auto', 'image'):
1310+
elif s in ('equal', 'tight', 'scaled', 'normal', 'auto', 'image', 'square'):
13091311
self.set_autoscale_on(True)
13101312
self.set_aspect('auto')
13111313
self.autoscale_view(tight=False)
@@ -1322,15 +1324,23 @@ def axis(self, *v, **kwargs):
13221324
self.autoscale_view(tight=True)
13231325
self.set_autoscale_on(False)
13241326
self.set_aspect('equal', adjustable='box', anchor='C')
1327+
elif s == 'square':
1328+
self.set_aspect('equal', adjustable='box', anchor='C')
1329+
self.set_autoscale_on(False)
1330+
xlim = self.get_xlim()
1331+
ylim = self.get_ylim()
1332+
minlim = min(xlim[0], ylim[0])
1333+
maxlim = max(xlim[1], ylim[1])
1334+
self.set_xlim([minlim, maxlim], emit=emit, auto=False)
1335+
self.set_ylim([minlim, maxlim], emit=emit, auto=False)
13251336

13261337
else:
13271338
raise ValueError('Unrecognized string %s to axis; '
13281339
'try on or off' % s)
13291340
xmin, xmax = self.get_xlim()
13301341
ymin, ymax = self.get_ylim()
13311342
return xmin, xmax, ymin, ymax
1332-
1333-
emit = kwargs.get('emit', True)
1343+
13341344
try:
13351345
v[0]
13361346
except IndexError:
9.81 KB
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,6 +3635,13 @@ def test_bar_negative_width():
36353635
assert_equal(b._width, 1)
36363636
assert_equal(b._height, indx + 1)
36373637

3638+
@image_comparison(baseline_images=['square_plot'], extensions=['png'])
3639+
def test_square_plot():
3640+
x = np.arange(4)
3641+
y = np.array([1., 3., 5., 7.])
3642+
fig, ax = plt.subplots()
3643+
ax.plot(x, y, 'mo')
3644+
ax.axis('square')
36383645

36393646
if __name__ == '__main__':
36403647
import nose

0 commit comments

Comments
 (0)