8000 Merge pull request #8898 from dmgt/egs_to_OO_status · matplotlib/matplotlib@a6ceb3b · GitHub
[go: up one dir, main page]

Skip to content

Commit a6ceb3b

Browse files
authored
Merge pull request #8898 from dmgt/egs_to_OO_status
[WIP] Update some pylab examples to OO format
2 parents 241a051 + 95a8d39 commit a6ceb3b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

examples/pylab_examples/simple_plot.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
import matplotlib.pyplot as plt
99
import numpy as np
1010

11+
# Data for plotting
1112
t = np.arange(0.0, 2.0, 0.01)
1213
s = 1 + np.sin(2*np.pi*t)
13-
plt.plot(t, s)
1414

15-
plt.xlabel('time (s)')
16-
plt.ylabel('voltage (mV)')
17-
plt.title('About as simple as it gets, folks')
18-
plt.grid(True)
19-
plt.savefig("test.png")
15+
# Note that using plt.subplots below is equivalent to using
16+
# fig = plt.figure and then ax = fig.add_subplot(111)
17+
fig, ax = plt.subplots()
18+
ax.plot(t, s)
19+
20+
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
21+
title='About as simple as it gets, folks')
22+
ax.grid()
23+
24+
fig.savefig("test.png")
2025
plt.show()
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
"""
2-
============
3-
Subplot Demo
4-
============
2+
==================
3+
Basic Subplot Demo
4+
==================
55
6-
Simple demo with multiple subplots.
6+
Demo with two subplots.
7+
For more options, see
8+
:ref:`sphx_glr_gallery_subplots_axes_and_figures_subplots_demo.py`
79
"""
810
import numpy as np
911
import matplotlib.pyplot as plt
1012

11-
13+
# Data for plotting
1214
x1 = np.linspace(0.0, 5.0)
1315
x2 = np.linspace(0.0, 2.0)
14-
1516
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
1617
y2 = np.cos(2 * np.pi * x2)
1718

18-
plt.subplot(2, 1, 1)
19-
plt.plot(x1, y1, 'ko-')
20-
plt.title('A tale of 2 subplots')
21-
plt.ylabel('Damped oscillation')
19+
# Create two subplots sharing y axis
20+
fig, (ax1, ax2) = plt.subplots(2, sharey=True)
21+
22+
ax1.plot(x1, y1, 'ko-')
23+
ax1.set(title='A tale of 2 subplots', ylabel='Damped oscillation')
2224

23-
plt.subplot(2, 1, 2)
24-
plt.plot(x2, y2, 'r.-')
25-
plt.xlabel('time (s)')
26-
plt.ylabel('Undamped')
25+
ax2.plot(x2, y2, 'r.-')
26+
ax2.set(xlabel='time (s)', ylabel='Undamped')
2727

2828
plt.show()

0 commit comments

Comments
 (0)
0