|
| 1 | +""" |
| 2 | +=========================== |
| 3 | +Centered spines with arrows |
| 4 | +=========================== |
| 5 | +
|
| 6 | +This example shows a way to draw a "math textbook" style plot, where the |
| 7 | +spines ("axes lines") are drawn at ``x = 0`` and ``y = 0``, and have arrows at |
| 8 | +their ends. |
| 9 | +""" |
| 10 | + |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +import numpy as np |
| 13 | + |
| 14 | + |
| 15 | +fig, ax = plt.subplots() |
| 16 | +# Move the left and bottom spines to x = 0 and y = 0, respectively. |
| 17 | +ax.spines["left"].set_position(("data", 0)) |
| 18 | +ax.spines["bottom"].set_position(("data", 0)) |
| 19 | +# Hide the top and right spines. |
| 20 | +ax.spines["top"].set_visible(False) |
| 21 | +ax.spines["right"].set_visible(False) |
| 22 | + |
| 23 | +# Draw arrows (as black triangles: ">k"/"^k") at the end of the axes. In each |
| 24 | +# case, one of the coordinates (0) is a data coordinate (i.e., y = 0 or x = 0, |
| 25 | +# respectively) and the other one (1) is an axes coordinate (i.e., at the very |
| 26 | +# right/top of the axes). Also, disable clipping (clip_on=False) as the marker |
| 27 | +# actually spills out of the axes. |
| 28 | +ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False) |
| 29 | +ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False) |
| 30 | + |
| 31 | +# Some sample data. |
| 32 | +x = np.linspace(-0.5, 1., 100) |
| 33 | +ax.plot(x, np.sin(x*np.pi)) |
| 34 | + |
| 35 | +plt.show() |
0 commit comments