You are reading documentation for the unreleased version of Matplotlib. Try searching for the released version of this page instead?
Applications are open for the 2018 John Hunter Matplotlib Summer Fellowship. Apply now!
Version 2.2.2.post1772+gd9240e24b
matplotlib
Fork me on GitHub

Related Topics

Automatically setting tick labelsΒΆ

Setting the behavior of tick auto-placement.

If you don't explicitly set tick positions / labels, Matplotlib will attempt to choose them both automatically based on the displayed data and its limits.

By default, this attempts to choose tick positions that are distributed along the axis:

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)

fig, ax = plt.subplots()
dots = np.arange(10) / 100. + .03
x, y = np.meshgrid(dots, dots)
data = [x.ravel(), y.ravel()]
ax.scatter(*data, c=data[1])
../../_images/sphx_glr_auto_ticks_001.png

Sometimes choosing evenly-distributed ticks results in strange tick numbers. If you'd like Matplotlib to keep ticks located at round numbers, you can change this behavior with the following rcParams value:

print(plt.rcParams['axes.autolimit_mode'])

# Now change this value and see the results
with plt.rc_context({'axes.autolimit_mode': 'round_numbers'}):
    fig, ax = plt.subplots()
    ax.scatter(*data, c=data[1])
../../_images/sphx_glr_auto_ticks_002.png

Out:

data

You can also alter the margins of the axes around the data by with axes.(x,y)margin:

with plt.rc_context({'axes.autolimit_mode': 'round_numbers',
                     'axes.xmargin': .8,
                     'axes.ymargin': .8}):
    fig, ax = plt.subplots()
    ax.scatter(*data, c=data[1])

plt.show()
../../_images/sphx_glr_auto_ticks_003.png

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery