{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Shared axis\n\nYou can share the x- or y-axis limits for one axis with another by\npassing an `~.axes.Axes` instance as a *sharex* or *sharey* keyword argument.\n\nChanging the axis limits on one Axes will be reflected automatically\nin the other, and vice-versa, so when you navigate with the toolbar\nthe Axes will follow each other on their shared axis. Ditto for\nchanges in the axis scaling (e.g., log vs. linear). However, it is\npossible to have differences in tick labeling, e.g., you can selectively\nturn off the tick labels on one Axes.\n\nThe example below shows how to customize the tick labels on the\nvarious axes. Shared axes share the tick locator, tick formatter,\nview limits, and transformation (e.g., log, linear). But the ticklabels\nthemselves do not share properties. This is a feature and not a bug,\nbecause you may want to make the tick labels smaller on the upper\naxes, e.g., in the example below.\n\nIf you want to turn off the ticklabels for a given Axes (e.g., on\nsubplot(211) or subplot(212)), you cannot do the standard trick::\n\n setp(ax2, xticklabels=[])\n\nbecause this changes the tick Formatter, which is shared among all\nAxes. But you can alter the visibility of the labels, which is a\nproperty::\n\n setp(ax2.get_xticklabels(), visible=False)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nt = np.arange(0.01, 5.0, 0.01)\ns1 = np.sin(2 * np.pi * t)\ns2 = np.exp(-t)\ns3 = np.sin(4 * np.pi * t)\n\nax1 = plt.subplot(311)\nplt.plot(t, s1)\nplt.tick_params('x', labelsize=6)\n\n# share x only\nax2 = plt.subplot(312, sharex=ax1)\nplt.plot(t, s2)\n# make these tick labels invisible\nplt.tick_params('x', labelbottom=False)\n\n# share x and y\nax3 = plt.subplot(313, sharex=ax1, sharey=ax1)\nplt.plot(t, s3)\nplt.xlim(0.01, 5.0)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n component: axis\n plot-type: line\n level: beginner\n\n" ] } ], "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 }