|
1 | 1 | """
|
2 | 2 | ============
|
3 |
| -Figure Title |
| 3 | +Figure title |
4 | 4 | ============
|
5 | 5 |
|
6 |
| -Create a figure with separate subplot titles and a centered figure title. |
| 6 | +Each subplot can have its own title (`.Axes.set_title`). Additionally, |
| 7 | +`.Figure.suptitle` adds a centered title at the top of the figure. |
7 | 8 | """
|
8 | 9 | import matplotlib.pyplot as plt
|
9 | 10 | import numpy as np
|
10 | 11 |
|
11 | 12 |
|
12 |
| -def f(t): |
13 |
| - s1 = np.cos(2*np.pi*t) |
14 |
| - e1 = np.exp(-t) |
15 |
| - return s1 * e1 |
| 13 | +x = np.linspace(0.0, 5.0, 501) |
16 | 14 |
|
17 |
| -t1 = np.arange(0.0, 5.0, 0.1) |
18 |
| -t2 = np.arange(0.0, 5.0, 0.02) |
19 |
| -t3 = np.arange(0.0, 2.0, 0.01) |
| 15 | +fig, (ax1, ax2) = plt.subplots(1, 2, constrained_layout=True, sharey=True) |
| 16 | +ax1.plot(x, np.cos(6*x) * np.exp(-x)) |
| 17 | +ax1.set_title('damped') |
| 18 | +ax1.set_xlabel('time (s)') |
| 19 | +ax1.set_ylabel('amplitude') |
20 | 20 |
|
| 21 | +ax2.plot(x, np.cos(6*x)) |
| 22 | +ax2.set_xlabel('time (s)') |
| 23 | +ax2.set_title('undamped') |
21 | 24 |
|
22 |
| -fig, axs = plt.subplots(2, 1, constrained_layout=True) |
23 |
| -axs[0].plot(t1, f(t1), 'o', t2, f(t2), '-') |
24 |
| -axs[0].set_title('subplot 1') |
25 |
| -axs[0].set_xlabel('distance (m)') |
26 |
| -axs[0].set_ylabel('Damped oscillation') |
27 |
| -fig.suptitle('This is a somewhat long figure title', fontsize=16) |
28 |
| - |
29 |
| -axs[1].plot(t3, np.cos(2*np.pi*t3), '--') |
30 |
| -axs[1].set_xlabel('time (s)') |
31 |
| -axs[1].set_title('subplot 2') |
32 |
| -axs[1].set_ylabel('Undamped') |
| 25 | +fig.suptitle('Different types of oscillations', fontsize=16) |
33 | 26 |
|
34 | 27 | plt.show()
|
0 commit comments