Skip to content

Commit 5faa1bb

Browse files
authored
Merge pull request #20514 from timhoffm/doc-autolimit_mode
Fix example for rcParams['autolimit_mode']
2 parents 33beff9 + 1a036aa commit 5faa1bb

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
"""
2-
=================================
3-
Automatically setting tick labels
4-
=================================
2+
====================================
3+
Automatically setting tick positions
4+
====================================
55
66
Setting the behavior of tick auto-placement.
77
8-
If you don't explicitly set tick positions / labels, Matplotlib will attempt
9-
to choose them both automatically based on the displayed data and its limits.
8+
By default, Matplotlib will choose the number of ticks and tick positions so
9+
that there is a reasonable number of ticks on the axis and they are located
10+
at "round" numbers.
1011
11-
By default, this attempts to choose tick positions that are distributed
12-
along the axis:
12+
As a result, there may be no ticks on the edges of the plot.
1313
"""
1414

1515
import matplotlib.pyplot as plt
1616
import numpy as np
1717
np.random.seed(19680801)
1818

1919
fig, ax = plt.subplots()
20-
dots = np.arange(10) / 100. + .03
21-
x, y = np.meshgrid(dots, dots)
22-
data = [x.ravel(), y.ravel()]
23-
ax.scatter(*data, c=data[1])
20+
dots = np.linspace(0.3, 1.2, 10)
21+
X, Y = np.meshgrid(dots, dots)
22+
x, y = X.ravel(), Y.ravel()
23+
ax.scatter(x, y, c=x+y)
24+
plt.show()
2425

2526
###############################################################################
26-
# Sometimes choosing evenly-distributed ticks results in strange tick numbers.
27-
# If you'd like Matplotlib to keep ticks located at round numbers, you can
28-
# change this behavior with the following rcParams value:
27+
# If you want to keep ticks at round numbers, and also have ticks at the edges
28+
# you can switch :rc:`axes.autolimit_mode` to 'round_numbers'. This expands the
29+
# axis limits to the next round number.
2930

30-
print(plt.rcParams['axes.autolimit_mode'])
31+
plt.rcParams['axes.autolimit_mode'] = 'round_numbers'
3132

32-
# Now change this value and see the results
33-
with plt.rc_context({'axes.autolimit_mode': 'round_numbers'}):
34-
fig, ax = plt.subplots()
35-
ax.scatter(*data, c=data[1])
33+
# Note: The limits are calculated at draw-time. Therefore, when using
34+
# :rc:`axes.autolimit_mode` in a context manager, it is important that
35+
# the ``show()`` command is within the context.
3636

37-
###############################################################################
38-
# You can also alter the margins of the axes around the data by
39-
# with ``axes.(x,y)margin``:
37+
fig, ax = plt.subplots()
38+
ax.scatter(x, y, c=x+y)
39+
plt.show()
4040

41-
with plt.rc_context({'axes.autolimit_mode': 'round_numbers',
42-
'axes.xmargin': .8,
43-
'axes.ymargin': .8}):
44-
fig, ax = plt.subplots()
45-
ax.scatter(*data, c=data[1])
41+
###############################################################################
42+
# The round numbers autolimit_mode is still respected if you set an additional
43+
# margin around the data using `.Axes.set_xmargin` / `.Axes.set_ymargin`:
4644

45+
fig, ax = plt.subplots()
46+
ax.scatter(x, y, c=x+y)
47+
ax.set_xmargin(0.8)
4748
plt.show()

0 commit comments

Comments
 (0)