.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/ticks_and_spines/auto_ticks.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_ticks_and_spines_auto_ticks.py: ================================= 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: .. GENERATED FROM PYTHON SOURCE LINES 14-25 .. code-block:: default 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]) .. image:: /gallery/ticks_and_spines/images/sphx_glr_auto_ticks_001.png :alt: auto ticks :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 26-29 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: .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: default 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]) .. image:: /gallery/ticks_and_spines/images/sphx_glr_auto_ticks_002.png :alt: auto ticks :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none data .. GENERATED FROM PYTHON SOURCE LINES 38-40 You can also alter the margins of the axes around the data by with ``axes.(x,y)margin``: .. GENERATED FROM PYTHON SOURCE LINES 40-48 .. code-block:: default 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() .. image:: /gallery/ticks_and_spines/images/sphx_glr_auto_ticks_003.png :alt: auto ticks :class: sphx-glr-single-img .. _sphx_glr_download_gallery_ticks_and_spines_auto_ticks.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: auto_ticks.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: auto_ticks.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature Keywords: matplotlib code example, codex, python plot, pyplot `Gallery generated by Sphinx-Gallery `_