From 76a405c9dcd337c0e1b7168d6ea2ce425f0ae784 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 17 Apr 2020 16:23:03 +0200 Subject: [PATCH] Convert SubplotZero example into centered-spines-with-arrows recipe. --- examples/axisartist/demo_axisline_style.py | 32 ---------- .../recipes/centered_spines_with_arrows.py | 58 +++++++++++++++++++ 2 files changed, 58 insertions(+), 32 deletions(-) delete mode 100644 examples/axisartist/demo_axisline_style.py create mode 100644 examples/recipes/centered_spines_with_arrows.py diff --git a/examples/axisartist/demo_axisline_style.py b/examples/axisartist/demo_axisline_style.py deleted file mode 100644 index fc61375147e4..000000000000 --- a/examples/axisartist/demo_axisline_style.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -================ -Axis line styles -================ - -This example shows some configurations for axis style. -""" - -from mpl_toolkits.axisartist.axislines import SubplotZero -import matplotlib.pyplot as plt -import numpy as np - - -fig = plt.figure() -ax = SubplotZero(fig, 111) -fig.add_subplot(ax) - -for direction in ["xzero", "yzero"]: - # adds arrows at the ends of each axis - ax.axis[direction].set_axisline_style("-|>") - - # adds X and Y-axis from the origin - ax.axis[direction].set_visible(True) - -for direction in ["left", "right", "bottom", "top"]: - # hides borders - ax.axis[direction].set_visible(False) - -x = np.linspace(-0.5, 1., 100) -ax.plot(x, np.sin(x*np.pi)) - -plt.show() diff --git a/examples/recipes/centered_spines_with_arrows.py b/examples/recipes/centered_spines_with_arrows.py new file mode 100644 index 000000000000..c1ab69842e55 --- /dev/null +++ b/examples/recipes/centered_spines_with_arrows.py @@ -0,0 +1,58 @@ +""" +=========================== +Centered spines with arrows +=========================== + +This examples shows two ways to draw a "math textbook" style plot, where the +spines ("axes lines") are drawn at ``x = 0`` and ``y = 0``, and have arrows at +their ends. +""" + +import matplotlib.pyplot as plt +from mpl_toolkits.axisartist.axislines import SubplotZero +import numpy as np + + +fig, ax = plt.subplots() +# Move the left and bottom spines to x = 0 and y = 0, respectively. +ax.spines["left"].set_position(("data", 0)) +ax.spines["bottom"].set_position(("data", 0)) +# Hide the top and right spines. +ax.spines["top"].set_visible(False) +ax.spines["right"].set_visible(False) + +# Draw arrows (as black triangles: ">k"/"^k") at the end of the axes. In each +# case, one of the coordinates (0) is a data coordinate (i.e., y = 0 or x = 0, +# respectively) and the other one (1) is an axes coordinate (i.e., at the very +# right/top of the axes). Also, disable clipping (clip_on=False) as the marker +# actually spills out of the axes. +ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False) +ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False) + +# Some sample data. +x = np.linspace(-0.5, 1., 100) +ax.plot(x, np.sin(x*np.pi)) + +############################################################################### +# Alternatively, one can use mpl_toolkit's SubplotZero, but note that many +# standard Matplotlib options (e.g. rcParams) have no effect. + +fig = plt.figure() +ax = SubplotZero(fig, 111) +fig.add_subplot(ax) + +for direction in ["xzero", "yzero"]: + # adds arrows at the ends of each axis + ax.axis[direction].set_axisline_style("-|>") + # adds X and Y-axis from the origin + ax.axis[direction].set_visible(True) + +for direction in ["left", "right", "bottom", "top"]: + # hides borders + ax.axis[direction].set_visible(False) + +x = np.linspace(-0.5, 1., 100) +ax.plot(x, np.sin(x*np.pi)) + + +plt.show()