8000 Avoid using `axis([xlo, xhi, ylo, yhi])` in examples. · matplotlib/matplotlib@fa23f88 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa23f88

Browse files
committed
Avoid using axis([xlo, xhi, ylo, yhi]) in examples.
In my view, `axis()` does too much: it can manipulate axis limits, axes aspect ratio, lines and label visibility, etc. This PR replaces in the examples `axis([xlo, xhi, ylo, yhi])` by `set(xlim=(xlo, xhi), ylim=(ylo, yhi))` (or `plt.xlim(xlo, xhi); plt.ylim(xlo, yhi)` for pyplot examples), which is easier to intuit IMO. There are also cases where I just remove the call to axis() when it doesn't add much to the example. Special cases: - anscombe can be greatly shortened by using shared axes. Note that the "old" xlims (2, 20) were immediately overwritten by xticks=(0, 10, 20) which expanded the xlims to (0, 20)... - fonts_demo/fonts_demo_kw are better showcased using figtext() anyways. - In pcolor_demo, the call to axis() is unneeded (pcolor/pcolormesh correctly set the artist sticky edges to have the right bounds). - In pyplot_formatstr (which could probably be deleted...), the call to axis() does not showcase anything. - In slider_demo, margins(x=0) is semantically more correct.
1 parent 35b875c commit fa23f88

File tree

21 files changed

+96
-143
lines changed

21 files changed

+96
-143
lines changed

examples/axes_grid1/inset_locator_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
# We set the axis limits to something other than the default, in order to not
7676
# distract from the fact that axes coordinates are used here.
77-
ax.axis([0, 10, 0, 10])
77+
ax.set(xlim=(0, 10), ylim=(0, 10))
7878

7979

8080
# Note how the two following insets are created at the same positions, one by
@@ -126,7 +126,7 @@
126126

127127
ax2 = fig.add_subplot(133)
128128
ax2.set_xscale("log")
129-
ax2.axis([1e-6, 1e6, -2, 6])
129+
ax2.set(xlim=(1e-6, 1e6), ylim=(-2, 6))
130130

131131
# Create inset in data coordinates using ax.transData as transform
132132
axins3 = inset_axes(ax2, width="100%", height="100%",

examples/event_handling/ginput_manual_clabel_sgskip.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def tellme(s):
2828

2929

3030
plt.clf()
31-
plt.axis([-1., 1., -1., 1.])
3231
plt.setp(plt.gca(), autoscale_on=False)
3332

3433
tellme('You will define a triangle, click to begin')
@@ -84,13 +83,14 @@ def f(x, y, pts):
8483

8584
while True:
8685
tellme('Select two corners of zoom, middle mouse button to finish')
87-
pts = np.asarray(plt.ginput(2, timeout=-1))
88-
86+
pts = plt.ginput(2, timeout=-1)
8987
if len(pts) < 2:
9088
break
91-
92-
pts = np.sort(pts, axis=0)
93-
plt.axis(pts.T.ravel())
89+
(x0, y0), (x1, y1) = pts
90+
xmin, xmax = sorted([x0, x1])
91+
ymin, ymax = sorted([y0, y1])
92+
plt.xlim(xmin, xmax)
93+
plt.ylim(ymin, ymax)
9494

9595
tellme('All Done!')
9696
plt.show()

examples/event_handling/pick_event_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def pick_image():
168168
ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True)
169169
ax.imshow(rand(20, 25), extent=(1, 2, 3, 4), picker=True)
170170
ax.imshow(rand(30, 12), extent=(3, 4, 3, 4), picker=True)
171-
ax.axis([0, 5, 0, 5])
171+
ax.set(xlim=(0, 5), ylim=(0, 5))
172172

173173
def onpick4(event):
174174
artist = event.artist

examples/images_contours_and_fields/irregulardatagrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
fig.colorbar(cntr1, ax=ax1)
6464
ax1.plot(x, y, 'ko', ms=3)
65-
ax1.axis((-2, 2, -2, 2))
65+
ax1.set(xlim=(-2, 2), ylim=(-2, 2))
6666
ax1.set_title('grid and contour (%d points, %d grid points)' %
6767
(npts, ngridx * ngridy))
6868

@@ -78,7 +78,7 @@
7878

7979
fig.colorbar(cntr2, ax=ax2)
8080
ax2.plot(x, y, 'ko', ms=3)
81-
ax2.axis((-2, 2, -2, 2))
81+
ax2.set(xlim=(-2, 2), ylim=(-2, 2))
8282
ax2.set_title('tricontour (%d points)' % npts)
8383

8484
plt.subplots_adjust(hspace=0.5)

examples/images_contours_and_fields/pcolor_demo.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,11 @@
5454
ax = axs[0, 0]
5555
c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
5656
ax.set_title('pcolor')
57-
# set the limits of the plot to the limits of the data
58-
ax.axis([x.min(), x.max(), y.min(), y.max()])
5957
fig.colorbar(c, ax=ax)
6058

6159
ax = axs[0, 1]
6260
c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
6361
ax.set_title('pcolormesh')
64-
# set the limits of the plot to the limits of the data
65-
ax.axis([x.min(), x.max(), y.min(), y.max()])
6662
fig.colorbar(c, ax=ax)
6763

6864
ax = axs[1, 0]

examples/images_contours_and_fields/plot_streamplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
# Displaying the starting points with blue symbols.
5555
ax3.plot(seed_points[0], seed_points[1], 'bo')
56-
ax3.axis((-w, w, -w, w))
56+
ax3.set(xlim=(-w, w), ylim=(-w, w))
5757

5858
# Create a mask
5959
mask = np.zeros(U.shape, dtype=bool)

examples/misc/contour_manual.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@
2525

2626
###############################################################################
2727

28-
plt.figure()
28+
fig, ax = plt.subplots()
2929

3030
# Filled contours using filled=True.
31-
cs = ContourSet(plt.gca(), [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone)
32-
cbar = plt.colorbar(cs)
31+
cs = ContourSet(ax, [0, 1, 2], [filled01, filled12], filled=True, cmap=cm.bone)
32+
cbar = fig.colorbar(cs)
3333

3434
# Contour lines (non-filled).
35-
lines = ContourSet(plt.gca(), [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool,
36-
linewidths=3)
35+
lines = ContourSet(
36+
ax, [0, 1, 2], [lines0, lines1, lines2], cmap=cm.cool, linewidths=3)
3737
cbar.add_lines(lines)
3838

39-
plt.axis([-0.5, 3.5, -0.5, 4.5])
40-
plt.title('User-specified contours')
39+
ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 4.5),
40+
title='User-specified contours')
4141

4242
###############################################################################
4343
# Multiple filled contour lines can be specified in a single list of polygon
4444
# vertices along with a list of vertex kinds (code types) as described in the
4545
# Path class. This is particularly useful for polygons with holes.
4646
# Here a code type of 1 is a MOVETO, and 2 is a LINETO.
4747

48-
plt.figure()
48+
fig, ax = plt.subplots()
4949
filled01 = [[[0, 0], [3, 0], [3, 3], [0, 3], [1, 1], [1, 2], [2, 2], [2, 1]]]
5050
kinds01 = [[1, 2, 2, 2, 1, 2, 2, 2]]
51-
cs = ContourSet(plt.gca(), [0, 1], [filled01], [kinds01], filled=True)
52-
cbar = plt.colorbar(cs)
51+
cs = ContourSet(ax, [0, 1], [filled01], [kinds01], filled=True)
52+
cbar = fig.colorbar(cs)
5353

54-
plt.axis([-0.5, 3.5, -0.5, 3.5])
55-
plt.title('User specified filled contours with holes')
54+
ax.set(xlim=(-0.5, 3.5), ylim=(-0.5, 3.5),
55+
title='User specified filled contours with holes')
5656

5757
plt.show()

examples/pyplots/pyplot_formatstr.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
2-
================
3-
Pyplot Formatstr
4-
================
2+
====================
3+
plot() format string
4+
====================
55
6-
Use a format string to colorize a `~matplotlib.axes.Axes.plot` and set its
7-
markers.
6+
Use a format string (here, 'ro') to set the color and markers of a
7+
`~matplotlib.axes.Axes.plot`.
88
"""
9+
910
import matplotlib.pyplot as plt
10-
plt.plot([1,2,3,4], [1,4,9,16], 'ro')
11-
plt.axis([0, 6, 0, 20])
11+
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
1212
plt.show()
1313

1414
#############################################################################

examples/pyplots/pyplot_text.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
plt.ylabel('Probability')
2222
plt.title('Histogram of IQ')
2323
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
24-
plt.axis([40, 160, 0, 0.03])
24+
plt.xlim(40, 160)
25+
plt.ylim(0, 0.03)
2526
plt.grid(True)
2627
plt.show()
2728

examples/pyplots/text_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
3636
arrowprops=dict(facecolor='black', shrink=0.05))
3737

38-
ax.axis([0, 10, 0, 10])
38+
ax.set(xlim=(0, 10), ylim=(0, 10))
3939

4040
plt.show()
4141

0 commit comments

Comments
 (0)
0