|
1 | 1 | """
|
2 |
| -================================= |
3 |
| -Automatically setting tick labels |
4 |
| -================================= |
| 2 | +==================================== |
| 3 | +Automatically setting tick positions |
| 4 | +==================================== |
5 | 5 |
|
6 | 6 | Setting the behavior of tick auto-placement.
|
7 | 7 |
|
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. |
10 | 11 |
|
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. |
13 | 13 | """
|
14 | 14 |
|
15 | 15 | import matplotlib.pyplot as plt
|
16 | 16 | import numpy as np
|
17 | 17 | np.random.seed(19680801)
|
18 | 18 |
|
19 | 19 | 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() |
24 | 25 |
|
25 | 26 | ###############################################################################
|
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. |
29 | 30 |
|
30 |
| -print(plt.rcParams['axes.autolimit_mode']) |
| 31 | +plt.rcParams['axes.autolimit_mode'] = 'round_numbers' |
31 | 32 |
|
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. |
36 | 36 |
|
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() |
40 | 40 |
|
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`: |
46 | 44 |
|
| 45 | +fig, ax = plt.subplots() |
| 46 | +ax.scatter(x, y, c=x+y) |
| 47 | +ax.set_xmargin(0.8) |
47 | 48 | plt.show()
|
0 commit comments