.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/spines/multiple_yaxis_with_spines.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_spines_multiple_yaxis_with_spines.py: =========================== Multiple y-axis with Spines =========================== Create multiple y axes with a shared x-axis. This is done by creating a `~.axes.Axes.twinx` axes, turning all spines but the right one invisible and offset its position using `~.spines.Spine.set_position`. Note that this approach uses `matplotlib.axes.Axes` and their `~matplotlib.spines.Spine`\s. Alternative approaches using non-standard axes are shown in the :doc:`/gallery/axisartist/demo_parasite_axes` and :doc:`/gallery/axisartist/demo_parasite_axes2` examples. .. GENERATED FROM PYTHON SOURCE LINES 15-47 .. image-sg:: /gallery/spines/images/sphx_glr_multiple_yaxis_with_spines_001.png :alt: multiple yaxis with spines :srcset: /gallery/spines/images/sphx_glr_multiple_yaxis_with_spines_001.png, /gallery/spines/images/sphx_glr_multiple_yaxis_with_spines_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import matplotlib.pyplot as plt fig, ax = plt.subplots() fig.subplots_adjust(right=0.75) twin1 = ax.twinx() twin2 = ax.twinx() # Offset the right spine of twin2. The ticks and label have already been # placed on the right by twinx above. twin2.spines.right.set_position(("axes", 1.2)) p1, = ax.plot([0, 1, 2], [0, 1, 2], "C0", label="Density") p2, = twin1.plot([0, 1, 2], [0, 3, 2], "C1", label="Temperature") p3, = twin2.plot([0, 1, 2], [50, 30, 15], "C2", label="Velocity") ax.set(xlim=(0, 2), ylim=(0, 2), xlabel="Distance", ylabel="Density") twin1.set(ylim=(0, 4), ylabel="Temperature") twin2.set(ylim=(1, 65), ylabel="Velocity") ax.yaxis.label.set_color(p1.get_color()) twin1.yaxis.label.set_color(p2.get_color()) twin2.yaxis.label.set_color(p3.get_color()) ax.tick_params(axis='y', colors=p1.get_color()) twin1.tick_params(axis='y', colors=p2.get_color()) twin2.tick_params(axis='y', colors=p3.get_color()) ax.legend(handles=[p1, p2, p3]) plt.show() .. _sphx_glr_download_gallery_spines_multiple_yaxis_with_spines.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: multiple_yaxis_with_spines.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: multiple_yaxis_with_spines.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_