.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/ticks_and_spines/spine_placement_demo.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_spine_placement_demo.py: =============== Spine Placement =============== Adjusting the location and appearance of axis spines. Note: If you want to obtain arrow heads at the ends of the axes, also check out the :doc:`/gallery/ticks_and_spines/centered_spines_with_arrows` example. .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: default import numpy as np import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 16-61 .. code-block:: default fig = plt.figure() x = np.linspace(-np.pi, np.pi, 100) y = 2 * np.sin(x) ax = fig.add_subplot(2, 2, 1) ax.set_title('centered spines') ax.plot(x, y) ax.spines.left.set_position('center') ax.spines.right.set_color('none') ax.spines.bottom.set_position('center') ax.spines.top.set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax = fig.add_subplot(2, 2, 2) ax.set_title('zeroed spines') ax.plot(x, y) ax.spines.left.set_position('zero') ax.spines.right.set_color('none') ax.spines.bottom.set_position('zero') ax.spines.top.set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax = fig.add_subplot(2, 2, 3) ax.set_title('spines at axes (0.6, 0.1)') ax.plot(x, y) ax.spines.left.set_position(('axes', 0.6)) ax.spines.right.set_color('none') ax.spines.bottom.set_position(('axes', 0.1)) ax.spines.top.set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax = fig.add_subplot(2, 2, 4) ax.set_title('spines at data (1, 2)') ax.plot(x, y) ax.spines.left.set_position(('data', 1)) ax.spines.right.set_color('none') ax.spines.bottom.set_position(('data', 2)) ax.spines.top.set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') .. image:: /gallery/ticks_and_spines/images/sphx_glr_spine_placement_demo_001.png :alt: centered spines, zeroed spines, spines at axes (0.6, 0.1), spines at data (1, 2) :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 62-63 Define a method that adjusts the location of the axis spines .. GENERATED FROM PYTHON SOURCE LINES 63-86 .. code-block:: default def adjust_spines(ax, spines): for loc, spine in ax.spines.items(): if loc in spines: spine.set_position(('outward', 10)) # outward by 10 points else: spine.set_color('none') # don't draw spine # turn off ticks where there is no spine if 'left' in spines: ax.yaxis.set_ticks_position('left') else: # no yaxis ticks ax.yaxis.set_ticks([]) if 'bottom' in spines: ax.xaxis.set_ticks_position('bottom') else: # no xaxis ticks ax.xaxis.set_ticks([]) .. GENERATED FROM PYTHON SOURCE LINES 87-88 Create another figure using our new ``adjust_spines`` method .. GENERATED FROM PYTHON SOURCE LINES 88-111 .. code-block:: default fig = plt.figure() x = np.linspace(0, 2 * np.pi, 100) y = 2 * np.sin(x) ax = fig.add_subplot(2, 2, 1) ax.plot(x, y, clip_on=False) adjust_spines(ax, ['left']) ax = fig.add_subplot(2, 2, 2) ax.plot(x, y, clip_on=False) adjust_spines(ax, []) ax = fig.add_subplot(2, 2, 3) ax.plot(x, y, clip_on=False) adjust_spines(ax, ['left', 'bottom']) ax = fig.add_subplot(2, 2, 4) ax.plot(x, y, clip_on=False) adjust_spines(ax, ['bottom']) plt.show() .. image:: /gallery/ticks_and_spines/images/sphx_glr_spine_placement_demo_002.png :alt: spine placement demo :class: sphx-glr-single-img .. _sphx_glr_download_gallery_ticks_and_spines_spine_placement_demo.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: spine_placement_demo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: spine_placement_demo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature Keywords: matplotlib code example, codex, python plot, pyplot `Gallery generated by Sphinx-Gallery `_