{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Parasite axis demo\n\nThis example demonstrates the use of parasite axis to plot multiple datasets\nonto one single plot.\n\nNotice how in this example, *par1* and *par2* are both obtained by calling\n``twinx()``, which ties their x-limits with the host's x-axis. From there, each\nof those two axis behave separately from each other: different datasets can be\nplotted, and the y-limits are adjusted separately.\n\nThis approach uses `mpl_toolkits.axes_grid1.parasite_axes.host_subplot` and\n`mpl_toolkits.axisartist.axislines.Axes`.\n\nThe standard and recommended approach is to use instead standard Matplotlib\naxes, as shown in the :doc:`/gallery/spines/multiple_yaxis_with_spines`\nexample.\n\nAn alternative approach using `mpl_toolkits.axes_grid1.parasite_axes.HostAxes`\nand `mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes` is shown in the\n:doc:`/gallery/axisartist/demo_parasite_axes` example.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom mpl_toolkits import axisartist\nfrom mpl_toolkits.axes_grid1 import host_subplot\n\nhost = host_subplot(111, axes_class=axisartist.Axes)\nplt.subplots_adjust(right=0.75)\n\npar1 = host.twinx()\npar2 = host.twinx()\n\npar2.axis[\"right\"] = par2.new_fixed_axis(loc=\"right\", offset=(60, 0))\n\npar1.axis[\"right\"].toggle(all=True)\npar2.axis[\"right\"].toggle(all=True)\n\np1, = host.plot([0, 1, 2], [0, 1, 2], label=\"Density\")\np2, = par1.plot([0, 1, 2], [0, 3, 2], label=\"Temperature\")\np3, = par2.plot([0, 1, 2], [50, 30, 15], label=\"Velocity\")\n\nhost.set(xlim=(0, 2), ylim=(0, 2), xlabel=\"Distance\", ylabel=\"Density\")\npar1.set(ylim=(0, 4), ylabel=\"Temperature\")\npar2.set(ylim=(1, 65), ylabel=\"Velocity\")\n\nhost.legend()\n\nhost.axis[\"left\"].label.set_color(p1.get_color())\npar1.axis[\"right\"].label.set_color(p2.get_color())\npar2.axis[\"right\"].label.set_color(p3.get_color())\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }