|
9 | 9 | sigmoid function. |
10 | 10 |
|
11 | 11 | `~.axes.Axes.axline` draws infinite straight lines in arbitrary directions. |
| 12 | +
|
| 13 | +.. redirect-from:: /gallery/pyplot/axline |
12 | 14 | """ |
13 | 15 |
|
14 | 16 | import matplotlib.pyplot as plt |
|
17 | 19 | t = np.linspace(-10, 10, 100) |
18 | 20 | sig = 1 / (1 + np.exp(-t)) |
19 | 21 |
|
20 | | -plt.axhline(y=0, color="black", linestyle="--") |
21 | | -plt.axhline(y=0.5, color="black", linestyle=":") |
22 | | -plt.axhline(y=1.0, color="black", linestyle="--") |
23 | | -plt.axvline(color="grey") |
24 | | -plt.axline((0, 0.5), slope=0.25, color="black", linestyle=(0, (5, 5))) |
25 | | -plt.plot(t, sig, linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$") |
26 | | -plt.xlim(-10, 10) |
27 | | -plt.xlabel("t") |
28 | | -plt.legend(fontsize=14) |
| 22 | +fig, ax = plt.subplots() |
| 23 | +ax.axhline(y=0, color="black", linestyle="--") |
| 24 | +ax.axhline(y=0.5, color="black", linestyle=":") |
| 25 | +ax.axhline(y=1.0, color="black", linestyle="--") |
| 26 | +ax.axvline(color="grey") |
| 27 | +ax.axline((0, 0.5), slope=0.25, color="black", linestyle=(0, (5, 5))) |
| 28 | +ax.plot(t, sig, linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$") |
| 29 | +ax.set(xlim=(-10, 10), xlabel="t") |
| 30 | +ax.legend(fontsize=14) |
29 | 31 | plt.show() |
30 | 32 |
|
31 | 33 | # %% |
32 | | -# `~.axes.Axes.axline` can also be used with a ``transform`` parameter, which |
| 34 | +# `~.axes.Axes.axline` can also be used with a *transform* parameter, which |
33 | 35 | # applies to the point, but not to the slope. This can be useful for drawing |
34 | 36 | # diagonal grid lines with a fixed slope, which stay in place when the |
35 | 37 | # plot limits are moved. |
36 | 38 |
|
| 39 | +fig, ax = plt.subplots() |
37 | 40 | for pos in np.linspace(-2, 1, 10): |
38 | | - plt.axline((pos, 0), slope=0.5, color='k', transform=plt.gca().transAxes) |
| 41 | + ax.axline((pos, 0), slope=0.5, color='k', transform=ax.transAxes) |
39 | 42 |
|
40 | | -plt.ylim([0, 1]) |
41 | | -plt.xlim([0, 1]) |
| 43 | +ax.set(xlim=(0, 1), ylim=(0, 1)) |
42 | 44 | plt.show() |
43 | 45 |
|
44 | 46 | # %% |
|
57 | 59 | # |
58 | 60 | # `~.Axes.axhspan`, `~.Axes.axvspan` draw rectangles that span the Axes in one |
59 | 61 | # direction and are bounded in the other direction. |
| 62 | +# |
| 63 | +# .. tags:: component: annotation |
0 commit comments