From a9c407023856840c1bba4020abebc5220e226076 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 22 Mar 2020 22:20:29 +0100 Subject: [PATCH] Cleanup new_fixed_axis examples. - We can call new_fixed_axis directly on the axes instance, which will forward the call to the grid_helper and also correctly set the *axes* kwarg. - In demo_parasite_axes and demo_parasite_axes2, group together the limits and labels settings, and remove some duplicates. - Reword the description of demo_parasite_axes2. --- examples/axisartist/demo_parasite_axes.py | 25 +++++------- examples/axisartist/demo_parasite_axes2.py | 45 ++++++++++------------ examples/axisartist/simple_axisline.py | 7 +--- 3 files changed, 31 insertions(+), 46 deletions(-) diff --git a/examples/axisartist/demo_parasite_axes.py b/examples/axisartist/demo_parasite_axes.py index a9fe6356340d..d3fe972d4a49 100644 --- a/examples/axisartist/demo_parasite_axes.py +++ b/examples/axisartist/demo_parasite_axes.py @@ -29,37 +29,30 @@ host.parasites.append(par1) host.parasites.append(par2) -host.set_ylabel("Density") -host.set_xlabel("Distance") - host.axis["right"].set_visible(False) -par1.axis["right"].set_visible(True) -par1.set_ylabel("Temperature") +par1.axis["right"].set_visible(True) par1.axis["right"].major_ticklabels.set_visible(True) par1.axis["right"].label.set_visible(True) -par2.set_ylabel("Velocity") -offset = (60, 0) -new_axisline = par2.get_grid_helper().new_fixed_axis -par2.axis["right2"] = new_axisline(loc="right", axes=par2, offset=offset) +par2.axis["right2"] = par2.new_fixed_axis(loc="right", offset=(60, 0)) fig.add_axes(host) -host.set_xlim(0, 2) -host.set_ylim(0, 2) - -host.set_xlabel("Distance") -host.set_ylabel("Density") -par1.set_ylabel("Temperature") - p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") 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.legend() host.axis["left"].label.set_color(p1.get_color()) diff --git a/examples/axisartist/demo_parasite_axes2.py b/examples/axisartist/demo_parasite_axes2.py index eca4b562bac5..3c23e37b7ae7 100644 --- a/examples/axisartist/demo_parasite_axes2.py +++ b/examples/axisartist/demo_parasite_axes2.py @@ -1,27 +1,27 @@ """ -=================== -Demo Parasite Axes2 -=================== - +================== Parasite axis demo +================== + +This example demonstrates the use of parasite axis to plot multiple datasets +onto one single plot. -The following code is an example of a parasite axis. It aims to show how -to plot multiple different values onto one single plot. Notice how in this -example, par1 and par2 are both calling twinx meaning both are tied directly to -the x-axis. From there, each of those two axis can behave separately from the -each other, meaning they can take on separate values from themselves as well as -the x-axis. +Notice how in this example, *par1* and *par2* are both obtained by calling +``twinx()``, which ties their x-limits with the host's x-axis. From there, each +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`\' +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`'s `~.mpl_toolkits.axes_grid1.parasite_axes.HostAxes` and `~.mpl_toolkits.axes_grid1.parasite_axes.ParasiteAxes` is the :doc:`/gallery/axisartist/demo_parasite_axes` example. -An alternative approach using the usual matplotlib subplots is shown in +An alternative approach using the usual Matplotlib subplots is shown in the :doc:`/gallery/ticks_and_spines/multiple_yaxis_with_spines` example. """ + from mpl_toolkits.axes_grid1 import host_subplot from mpl_toolkits import axisartist import matplotlib.pyplot as plt @@ -32,30 +32,25 @@ par1 = host.twinx() par2 = host.twinx() -offset = 60 -new_fixed_axis = par2.get_grid_helper().new_fixed_axis -par2.axis["right"] = new_fixed_axis(loc="right", - axes=par2, - offset=(offset, 0)) +par2.axis["right"] = par2.new_fixed_axis(loc="right", offset=(60, 0)) par1.axis["right"].toggle(all=True) par2.axis["right"].toggle(all=True) +p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") +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") -p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density") -p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature") -p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity") - -par1.set_ylim(0, 4) -par2.set_ylim(1, 65) - host.legend() host.axis["left"].label.set_color(p1.get_color()) diff --git a/examples/axisartist/simple_axisline.py b/examples/axisartist/simple_axisline.py index 9ec7283894fb..1d48d9f3ff8a 100644 --- a/examples/axisartist/simple_axisline.py +++ b/examples/axisartist/simple_axisline.py @@ -4,8 +4,8 @@ =============== """ -import matplotlib.pyplot as plt +import matplotlib.pyplot as plt from mpl_toolkits.axisartist.axislines import SubplotZero @@ -30,10 +30,7 @@ #ax.axis["left"].label.set_text("Label Y") # make new (right-side) yaxis, but with some offset -offset = (20, 0) -new_axisline = ax.get_grid_helper().new_fixed_axis - -ax.axis["right2"] = new_axisline(loc="right", offset=offset, axes=ax) +ax.axis["right2"] = ax.new_fixed_axis(loc="right", offset=(20, 0)) ax.axis["right2"].label.set_text("Label Y2") ax.plot([-2, 3, 2])