|
1 | 1 | """
|
2 |
| -================== |
3 |
| -Simple Axisartist1 |
4 |
| -================== |
| 2 | +============================= |
| 3 | +Custom spines with axisartist |
| 4 | +============================= |
5 | 5 |
|
| 6 | +This example showcases the use of :mod:`.axisartist` to draw spines at custom |
| 7 | +positions (here, at ``y = 0``). |
| 8 | +
|
| 9 | +Note, however, that it is simpler to achieve this effect |
| 10 | +using standard `.Spine` methods, as demonstrated in |
| 11 | +:doc:`/gallery/ticks_and_spines/centered_spines_with_arrows`. |
| 12 | +
|
| 13 | +.. redirect-from:: /gallery/axisartist/simple_axisline2 |
6 | 14 | """
|
| 15 | + |
7 | 16 | import matplotlib.pyplot as plt
|
8 | 17 | from mpl_toolkits import axisartist
|
| 18 | +import numpy as np |
| 19 | + |
| 20 | + |
| 21 | +fig = plt.figure(figsize=(6, 3), constrained_layout=True) |
| 22 | +# To construct axes of two different classes, we need to use gridspec (or |
| 23 | +# MATLAB-style add_subplot calls). |
| 24 | +gs = fig.add_gridspec(1, 2) |
| 25 | + |
| 26 | + |
| 27 | +ax0 = fig.add_subplot(gs[0, 0], axes_class=axisartist.Axes) |
| 28 | +# Make a new axis along the first (x) axis which passes through y=0. |
| 29 | +ax0.axis["y=0"] = ax0.new_floating_axis(nth_coord=0, value=0, |
| 30 | + axis_direction="bottom") |
| 31 | +ax0.axis["y=0"].toggle(all=True) |
| 32 | +ax0.axis["y=0"].label.set_text("y = 0") |
| 33 | +# Make other axis invisible. |
| 34 | +ax0.axis["bottom", "top", "right"].set_visible(False) |
9 | 35 |
|
10 |
| -fig = plt.figure() |
11 |
| -fig.subplots_adjust(right=0.85) |
12 |
| -ax = fig.add_subplot(axes_class=axisartist.Axes) |
13 | 36 |
|
14 |
| -# make some axis invisible |
15 |
| -ax.axis["bottom", "top", "right"].set_visible(False) |
| 37 | +# Alternatively, one can use AxesZero, which automatically sets up two |
| 38 | +# additional axis, named "xzero" (the y=0 axis) and "yzero" (the x=0 axis). |
| 39 | +ax1 = fig.add_subplot(gs[0, 1], axes_class=axisartist.axislines.AxesZero) |
| 40 | +# "xzero" and "yzero" default to invisible; make xzero axis visible. |
| 41 | +ax1.axis["xzero"].set_visible(True) |
| 42 | +ax1.axis["xzero"].label.set_text("Axis Zero") |
| 43 | +# Make other axis invisible. |
| 44 | +ax1.axis["bottom", "top", "right"].set_visible(False) |
16 | 45 |
|
17 |
| -# make an new axis along the first axis axis (x-axis) which pass |
18 |
| -# through y=0. |
19 |
| -ax.axis["y=0"] = ax.new_floating_axis(nth_coord=0, value=0, |
20 |
| - axis_direction="bottom") |
21 |
| -ax.axis["y=0"].toggle(all=True) |
22 |
| -ax.axis["y=0"].label.set_text("y = 0") |
23 | 46 |
|
24 |
| -ax.set_ylim(-2, 4) |
| 47 | +# Draw some sample data. |
| 48 | +x = np.arange(0, 2*np.pi, 0.01) |
| 49 | +ax0.plot(x, np.sin(x)) |
| 50 | +ax1.plot(x, np.sin(x)) |
25 | 51 |
|
26 | 52 | plt.show()
|
0 commit comments