Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 07aa92b

Browse filesBrowse files
authored
Replace use of pyplot with OO api in some examples (#19359)
* Replace use of pyplot with OO api in some examples * flake8 fix
1 parent b6294ff commit 07aa92b
Copy full SHA for 07aa92b

File tree

Expand file treeCollapse file tree

7 files changed

+64
-63
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+64
-63
lines changed

‎examples/event_handling/trifinder_event_demo.py

Copy file name to clipboardExpand all lines: examples/event_handling/trifinder_event_demo.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def on_mouse_move(event):
2929
else:
3030
tri = trifinder(event.xdata, event.ydata)
3131
update_polygon(tri)
32-
plt.title('In triangle %i' % tri)
32+
ax.set_title(f'In triangle {tri}')
3333
event.canvas.draw()
3434

3535

@@ -52,10 +52,10 @@ def on_mouse_move(event):
5252
trifinder = triang.get_trifinder()
5353

5454
# Setup plot and callbacks.
55-
plt.subplot(aspect='equal')
56-
plt.triplot(triang, 'bo-')
55+
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
56+
ax.triplot(triang, 'bo-')
5757
polygon = Polygon([[0, 0], [0, 0]], facecolor='y') # dummy data for (xs, ys)
5858
update_polygon(-1)
59-
plt.gca().add_patch(polygon)
60-
plt.gcf().canvas.mpl_connect('motion_notify_event', on_mouse_move)
59+
ax.add_patch(polygon)
60+
fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)
6161
plt.show()

‎examples/lines_bars_and_markers/psd_demo.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/psd_demo.py
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
cnse = cnse[:len(t)]
2727
s = 0.1 * np.sin(2 * np.pi * t) + cnse
2828

29-
plt.subplot(211)
30-
plt.plot(t, s)
31-
plt.subplot(212)
32-
plt.psd(s, 512, 1 / dt)
29+
fig, (ax0, ax1) = plt.subplots(2, 1)
30+
ax0.plot(t, s)
31+
ax1.psd(s, 512, 1 / dt)
3332

3433
plt.show()
3534

@@ -72,7 +71,7 @@
7271
ax2.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs)
7372
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 2, Fs=fs)
7473
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 4, Fs=fs)
75-
plt.title('zero padding')
74+
ax2.set_title('zero padding')
7675

7776
# Plot the PSD with different block sizes, Zero pad to the length of the
7877
# original data sequence.
@@ -81,7 +80,7 @@
8180
ax3.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs)
8281
ax3.psd(y, NFFT=len(t) // 4, pad_to=len(t), Fs=fs)
8382
ax3.set_ylabel('')
84-
plt.title('block size')
83+
ax3.set_title('block size')
8584

8685
# Plot the PSD with different amounts of overlap between blocks
8786
ax4 = fig.add_subplot(gs[1, 2], sharex=ax2, sharey=ax2)
@@ -91,7 +90,7 @@
9190
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t),
9291
noverlap=int(0.2 * len(t) / 2.), Fs=fs)
9392
ax4.set_ylabel('')
94-
plt.title('overlap')
93+
ax4.set_title('overlap')
9594

9695
plt.show()
9796

‎examples/misc/custom_projection.py

Copy file name to clipboardExpand all lines: examples/misc/custom_projection.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ def _get_core_transform(self, resolution):
443443
if __name__ == '__main__':
444444
import matplotlib.pyplot as plt
445445
# Now make a simple example using the custom projection.
446-
plt.subplot(projection="custom_hammer")
447-
p = plt.plot([-1, 1, 1], [-1, -1, 1], "o-")
448-
plt.grid(True)
446+
fig, ax = plt.subplots(subplot_kw={'projection': 'custom_hammer'})
447+
ax.plot([-1, 1, 1], [-1, -1, 1], "o-")
448+
ax.grid()
449449

450450
plt.show()

‎examples/scales/symlog_demo.py

Copy file name to clipboard
+20-20Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
"""
23
===========
34
Symlog Demo
@@ -12,24 +13,23 @@
1213
x = np.arange(-50.0, 50.0, dt)
1314
y = np.arange(0, 100.0, dt)
1415

15-
plt.subplot(311)
16-
plt.plot(x, y)
17-
plt.xscale('symlog')
18-
plt.ylabel('symlogx')
19-
plt.grid(True)
20-
plt.gca().xaxis.grid(True, which='minor') # minor grid on too
21-
22-
plt.subplot(312)
23-
plt.plot(y, x)
24-
plt.yscale('symlog')
25-
plt.ylabel('symlogy')
26-
27-
plt.subplot(313)
28-
plt.plot(x, np.sin(x / 3.0))
29-
plt.xscale('symlog')
30-
plt.yscale('symlog', linthresh=0.015)
31-
plt.grid(True)
32-
plt.ylabel('symlog both')
33-
34-
plt.tight_layout()
16+
fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)
17+
18+
ax0.plot(x, y)
19+
ax0.set_xscale('symlog')
20+
ax0.set_ylabel('symlogx')
21+
ax0.grid()
22+
ax0.xaxis.grid(which='minor') # minor grid on too
23+
24+
ax1.plot(y, x)
25+
ax1.set_yscale('symlog')
26+
ax1.set_ylabel('symlogy')
27+
28+
ax2.plot(x, np.sin(x / 3.0))
29+
ax2.set_xscale('symlog')
30+
ax2.set_yscale('symlog', linthresh=0.015)
31+
ax2.grid()
32+
ax2.set_ylabel('symlog both')
33+
34+
fig.tight_layout()
3535
plt.show()

‎examples/shapes_and_collections/ellipse_demo.py

Copy file name to clipboardExpand all lines: examples/shapes_and_collections/ellipse_demo.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@
4949
angle_step = 45 # degrees
5050
angles = np.arange(0, 180, angle_step)
5151

52-
ax = plt.subplot(aspect='equal')
52+
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
5353

5454
for angle in angles:
5555
ellipse = Ellipse((0, 0), 4, 2, angle=angle, alpha=0.1)
5656
ax.add_artist(ellipse)
5757

58-
plt.xlim(-2.2, 2.2)
59-
plt.ylim(-2.2, 2.2)
58+
ax.set_xlim(-2.2, 2.2)
59+
ax.set_ylim(-2.2, 2.2)
6060

6161
plt.show()
6262

‎examples/text_labels_and_annotations/multiline.py

Copy file name to clipboardExpand all lines: examples/text_labels_and_annotations/multiline.py
+17-17Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,39 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10-
plt.figure(figsize=(7, 4))
11-
ax = plt.subplot(121)
12-
ax.set_aspect(1)
13-
plt.plot(np.arange(10))
14-
plt.xlabel('this is a xlabel\n(with newlines!)')
15-
plt.ylabel('this is vertical\ntest', multialignment='center')
16-
plt.text(2, 7, 'this is\nyet another test',
10+
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(7, 4))
11+
12+
ax0.set_aspect(1)
13+
ax0.plot(np.arange(10))
14+
ax0.set_xlabel('this is a xlabel\n(with newlines!)')
15+
ax0.set_ylabel('this is vertical\ntest', multialignment='center')
16+
ax0.text(2, 7, 'this is\nyet another test',
1717
rotation=45,
1818
horizontalalignment='center',
1919
verticalalignment='top',
2020
multialignment='center')
2121

22-
plt.grid(True)
22+
ax0.grid()
2323

24-
plt.subplot(122)
2524

26-
plt.text(0.29, 0.4, "Mat\nTTp\n123", size=18,
25+
ax1.text(0.29, 0.4, "Mat\nTTp\n123", size=18,
2726
va="baseline", ha="right", multialignment="left",
2827
bbox=dict(fc="none"))
2928

30-
plt.text(0.34, 0.4, "Mag\nTTT\n123", size=18,
29+
ax1.text(0.34, 0.4, "Mag\nTTT\n123", size=18,
3130
va="baseline", ha="left", multialignment="left",
3231
bbox=dict(fc="none"))
3332

34-
plt.text(0.95, 0.4, "Mag\nTTT$^{A^A}$\n123", size=18,
33+
ax1.text(0.95, 0.4, "Mag\nTTT$^{A^A}$\n123", size=18,
3534
va="baseline", ha="right", multialignment="left",
3635
bbox=dict(fc="none"))
3736

38-
plt.xticks([0.2, 0.4, 0.6, 0.8, 1.],
39-
["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009", "May\n2009"])
37+
ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.])
38+
ax1.set_xticklabels(["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009",
39+
"May\n2009"])
4040

41-
plt.axhline(0.4)
42-
plt.title("test line spacing for multiline text")
41+
ax1.axhline(0.4)
42+
ax1.set_title("test line spacing for multiline text")
4343

44-
plt.subplots_adjust(bottom=0.25, top=0.75)
44+
fig.subplots_adjust(bottom=0.25, top=0.75)
4545
plt.show()

‎examples/userdemo/simple_legend01.py

Copy file name to clipboardExpand all lines: examples/userdemo/simple_legend01.py
+10-8Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
import matplotlib.pyplot as plt
88

99

10-
plt.subplot(211)
11-
plt.plot([1, 2, 3], label="test1")
12-
plt.plot([3, 2, 1], label="test2")
10+
fig = plt.figure()
11+
12+
ax = fig.add_subplot(211)
13+
ax.plot([1, 2, 3], label="test1")
14+
ax.plot([3, 2, 1], label="test2")
1315
# Place a legend above this subplot, expanding itself to
1416
# fully use the given bounding box.
15-
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
17+
ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc='lower left',
1618
ncol=2, mode="expand", borderaxespad=0.)
1719

18-
plt.subplot(223)
19-
plt.plot([1, 2, 3], label="test1")
20-
plt.plot([3, 2, 1], label="test2")
20+
ax = fig.add_subplot(223)
21+
ax.plot([1, 2, 3], label="test1")
22+
ax.plot([3, 2, 1], label="test2")
2123
# Place a legend to the right of this smaller subplot.
22-
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
24+
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
2325

2426
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.