{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Secondary Axis\n\nSometimes we want a secondary axis on a plot, for instance to convert\nradians to degrees on the same plot. We can do this by making a child\naxes with only one axis visible via `.axes.Axes.secondary_xaxis` and\n`.axes.Axes.secondary_yaxis`. This secondary axis can have a different scale\nthan the main axis by providing both a forward and an inverse conversion\nfunction in a tuple to the ``functions`` kwarg:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\nimport datetime\nimport matplotlib.dates as mdates\nfrom matplotlib.ticker import AutoMinorLocator\n\nfig, ax = plt.subplots(constrained_layout=True)\nx = np.arange(0, 360, 1)\ny = np.sin(2 * x * np.pi / 180)\nax.plot(x, y)\nax.set_xlabel('angle [degrees]')\nax.set_ylabel('signal')\nax.set_title('Sine wave')\n\n\ndef deg2rad(x):\n return x * np.pi / 180\n\n\ndef rad2deg(x):\n return x * 180 / np.pi\n\n\nsecax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg))\nsecax.set_xlabel('angle [rad]')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is the case of converting from wavenumber to wavelength in a\nlog-log scale.\n\n
In this case, the xscale of the parent is logarithmic, so the child is\n made logarithmic as well.
In order to properly handle the data margins, the mapping functions\n (``forward`` and ``inverse`` in this example) need to be defined beyond the\n nominal plot limits.\n\n In the specific case of the numpy linear interpolation, `numpy.interp`,\n this condition can be arbitrarily enforced by providing optional kwargs\n *left*, *right* such that values outside the data range are mapped\n well outside the plot limits.