From 24bcd306faf22dcf41682127a7dace0a0d440c19 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 18 Oct 2022 10:53:17 +0200 Subject: [PATCH] Recommend multiple_yaxis_with_spines over parasite axes. Standard Axes are, well, more standard. Also some small rewording, and make the axes limits/label setting shorter with `.set()`, as that's not really the main point of the examples. --- examples/axisartist/demo_parasite_axes.py | 22 ++++------- examples/axisartist/demo_parasite_axes2.py | 29 ++++++--------- examples/spines/multiple_yaxis_with_spines.py | 37 +++++++------------ 3 files changed, 34 insertions(+), 54 deletions(-) diff --git a/examples/axisartist/demo_parasite_axes.py b/examples/axisartist/demo_parasite_axes.py index e502a7a42c02..b1bdcbaba091 100644 --- a/examples/axisartist/demo_parasite_axes.py +++ b/examples/axisartist/demo_parasite_axes.py @@ -9,18 +9,18 @@ This approach uses `mpl_toolkits.axes_grid1.parasite_axes.HostAxes` and `mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes`. -An alternative approach using standard Matplotlib subplots is shown in the -:doc:`/gallery/spines/multiple_yaxis_with_spines` example. +The standard and recommended approach is to use instead standard Matplotlib +axes, as shown in the :doc:`/gallery/spines/multiple_yaxis_with_spines` +example. -An alternative approach using :mod:`mpl_toolkits.axes_grid1` -and :mod:`mpl_toolkits.axisartist` is found in the +An alternative approach using `mpl_toolkits.axes_grid1` and +`mpl_toolkits.axisartist` is shown in the :doc:`/gallery/axisartist/demo_parasite_axes2` example. """ from mpl_toolkits.axisartist.parasite_axes import HostAxes import matplotlib.pyplot as plt - fig = plt.figure() host = fig.add_axes([0.15, 0.1, 0.65, 0.8], axes_class=HostAxes) @@ -39,15 +39,9 @@ p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") -host.set_xlim(0, 2) -host.set_ylim(0, 2) -par1.set_ylim(0, 4) -par2.set_ylim(1, 65) - -host.set_xlabel("Distance") -host.set_ylabel("Density") -par1.set_ylabel("Temperature") -par2.set_ylabel("Velocity") +host.set(xlim=(0, 2), ylim=(0, 2), xlabel="Distance", ylabel="Density") +par1.set(ylim=(0, 4), ylabel="Temperature") +par2.set(ylim=(1, 65), ylabel="Velocity") host.legend() diff --git a/examples/axisartist/demo_parasite_axes2.py b/examples/axisartist/demo_parasite_axes2.py index 651cdd032ae5..d72611c88cd5 100644 --- a/examples/axisartist/demo_parasite_axes2.py +++ b/examples/axisartist/demo_parasite_axes2.py @@ -11,15 +11,16 @@ of those two axis behave separately from each other: different datasets can be plotted, and the y-limits are adjusted separately. -Note that this approach uses the `mpl_toolkits.axes_grid1.parasite_axes`' -`~mpl_toolkits.axes_grid1.parasite_axes.host_subplot` and -`mpl_toolkits.axisartist.axislines.Axes`. An alternative approach using the -`~mpl_toolkits.axes_grid1.parasite_axes`'s -`~.mpl_toolkits.axes_grid1.parasite_axes.HostAxes` and -`~.mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes` is the +This approach uses `mpl_toolkits.axes_grid1.parasite_axes.host_subplot` and +`mpl_toolkits.axisartist.axislines.Axes`. + +The standard and recommended approach is to use instead standard Matplotlib +axes, as shown in the :doc:`/gallery/spines/multiple_yaxis_with_spines` +example. + +An alternative approach using `mpl_toolkits.axes_grid1.parasite_axes.HostAxes` +and `mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes` is shown in the :doc:`/gallery/axisartist/demo_parasite_axes` example. -An alternative approach using the usual Matplotlib subplots is shown in -the :doc:`/gallery/spines/multiple_yaxis_with_spines` example. """ from mpl_toolkits.axes_grid1 import host_subplot @@ -41,15 +42,9 @@ p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") -host.set_xlim(0, 2) -host.set_ylim(0, 2) -par1.set_ylim(0, 4) -par2.set_ylim(1, 65) - -host.set_xlabel("Distance") -host.set_ylabel("Density") -par1.set_ylabel("Temperature") -par2.set_ylabel("Velocity") +host.set(xlim=(0, 2), ylim=(0, 2), xlabel="Distance", ylabel="Density") +par1.set(ylim=(0, 4), ylabel="Temperature") +par2.set(ylim=(1, 65), ylabel="Velocity") host.legend() diff --git a/examples/spines/multiple_yaxis_with_spines.py b/examples/spines/multiple_yaxis_with_spines.py index 54eebdae11ba..d4116a877a6f 100644 --- a/examples/spines/multiple_yaxis_with_spines.py +++ b/examples/spines/multiple_yaxis_with_spines.py @@ -1,21 +1,20 @@ r""" -========================== -Multiple Yaxis With Spines -========================== +=========================== +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. An alternative approach for parasite -axes is shown in the :doc:`/gallery/axisartist/demo_parasite_axes` and +`~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. """ import matplotlib.pyplot as plt - fig, ax = plt.subplots() fig.subplots_adjust(right=0.75) @@ -26,29 +25,21 @@ # placed on the right by twinx above. twin2.spines.right.set_position(("axes", 1.2)) -p1, = ax.plot([0, 1, 2], [0, 1, 2], "b-", label="Density") -p2, = twin1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature") -p3, = twin2.plot([0, 1, 2], [50, 30, 15], "g-", label="Velocity") - -ax.set_xlim(0, 2) -ax.set_ylim(0, 2) -twin1.set_ylim(0, 4) -twin2.set_ylim(1, 65) +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_xlabel("Distance") -ax.set_ylabel("Density") -twin1.set_ylabel("Temperature") -twin2.set_ylabel("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()) -tkw = dict(size=4, width=1.5) -ax.tick_params(axis='y', colors=p1.get_color(), **tkw) -twin1.tick_params(axis='y', colors=p2.get_color(), **tkw) -twin2.tick_params(axis='y', colors=p3.get_color(), **tkw) -ax.tick_params(axis='x', **tkw) +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])