Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29ba9a9

Browse files
authored
Merge pull request #18123 from timhoffm/doc-figure-title
Cleanup figure title example
2 parents f794206 + fabbbdc commit 29ba9a9

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
"""
22
============
3-
Figure Title
3+
Figure title
44
============
55
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.
78
"""
89
import matplotlib.pyplot as plt
910
import numpy as np
1011

1112

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)
1614

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')
2020

21+
ax2.plot(x, np.cos(6*x))
22+
ax2.set_xlabel('time (s)')
23+
ax2.set_title('undamped')
2124

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)
3326

3427
plt.show()

0 commit comments

Comments
 (0)