|
| 1 | +""" |
| 2 | +================================= |
| 3 | +Automatically setting tick labels |
| 4 | +================================= |
| 5 | +
|
| 6 | +Setting the behavior of tick auto-placement. |
| 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. |
| 10 | +
|
| 11 | +By default, this attempts to choose tick positions that are distributed |
| 12 | +along the axis: |
| 13 | +""" |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | +np.random.seed(19680801) |
| 17 | + |
| 18 | +fig, ax = plt.subplots() |
| 19 | +dots = np.arange(10) / 100. + .03 |
| 20 | +x, y = np.meshgrid(dots, dots) |
| 21 | +data = [x.ravel(), y.ravel()] |
| 22 | +ax.scatter(*data, c=data[1]) |
| 23 | + |
| 24 | +################################################################################ |
| 25 | +# Sometimes choosing evenly-distributed ticks results in strange tick numbers. |
| 26 | +# If you'd like Matplotlib to keep ticks located at round numbers, you can |
| 27 | +# change this behavior with the following rcParams value: |
| 28 | + |
| 29 | +print(plt.rcParams['axes.autolimit_mode']) |
| 30 | + |
| 31 | +# Now change this value and see the results |
| 32 | +with plt.rc_context({'axes.autolimit_mode': 'round_numbers'}): |
| 33 | + fig, ax = plt.subplots() |
| 34 | + ax.scatter(*data, c=data[1]) |
| 35 | + |
| 36 | +################################################################################ |
| 37 | +# You can also alter the margins of the axes around the data by |
| 38 | +# with ``axes.(x,y)margin``: |
| 39 | + |
| 40 | +with plt.rc_context({'axes.autolimit_mode': 'round_numbers', |
| 41 | + 'axes.xmargin': .8, |
| 42 | + 'axes.ymargin': .8}): |
| 43 | + fig, ax = plt.subplots() |
| 44 | + ax.scatter(*data, c=data[1]) |
| 45 | + |
| 46 | +plt.show() |
0 commit comments