Skip to content

Commit 5bb185e

Browse files
ivanovmdboom
authored andcommitted
use plt.subplots() in examples as much as possible
At the recent LBL Software Carpentry Workshop, it was pointed out that there's an inconsistency within our documentation for how to create new figures with subplots. Indeed, most examples were using the old way, something like: fig = plt.figure() ax = plt.subplot(111) # or plt.add_subplot(111) This patch changes a whole bunch of instances like the above to: fig, ax = plt.subplots() We should strive to have a minimal amount of constants in our code, especially unusual ones like `111`, which only make sense to Matlab refugees. I have left unchanged examples which were using axes keywords passed to subplot() and add_subplot(), since those end up transforming things like: figure() subplot(111, axisbg='w') to plt.subplots(subplot_kw=dict(axisbg='w')) which isn't necessarily better. I also did not touch most of the user_interfaces examples, since those did not involve using plt, but instead explicitly imported Figure, and used the OO approach on Figure instances. Also updated instaces where the old "import pylab as p" convention was used to use our standard "import matplotlib.pyplot as plt" I have also updated some, but not all uses of subplot(121) etc, but I'm a bit exhausted after doing all of these.
1 parent e783faa commit 5bb185e

File tree

121 files changed

+191
-330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+191
-330
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090

9191
2012-11-09 Make plt.subplot() without arguments act as subplot(111) - PI
9292

93+
2012-11-08 Replaced plt.figure and plt.subplot calls by the newer, more
94+
convenient single call to plt.subplots() in the documentation
95+
examples - PI
96+
9397
2012-10-05 Add support for saving animations as animated GIFs. - JVDP
9498

9599
2012-08-11 Fix path-closing bug in patches.Polygon, so that regardless

examples/animation/animate_decay.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ def data_gen():
1111
yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
1212
data_gen.t = 0
1313

14-
fig = plt.figure()
15-
ax = fig.add_subplot(111)
14+
fig, ax = plt.subplots()
1615
line, = ax.plot([], [], lw=2)
1716
ax.set_ylim(-1.1, 1.1)
1817
ax.set_xlim(0, 5)

examples/animation/histogram.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import matplotlib.path as path
1010
import matplotlib.animation as animation
1111

12-
fig = plt.figure()
13-
ax = fig.add_subplot(111)
12+
fig, ax = plt.subplots()
1413

1514
# histogram our data with numpy
1615
data = np.random.randn(1000)

examples/animation/old_animation/animate_decay_tk_blit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ def data_gen():
1010
return np.sin(2*np.pi*t) * np.exp(-t/10.)
1111
data_gen.t = 0
1212

13-
fig = plt.figure()
14-
ax = fig.add_subplot(111)
13+
fig, ax = plt.subplots()
1514
line, = ax.plot([], [], animated=True, lw=2)
1615
ax.set_ylim(-1.1, 1.1)
1716
ax.set_xlim(0, 5)

examples/animation/old_animation/animation_blit_gtk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import matplotlib.pyplot as plt
1717

1818

19-
fig = plt.figure()
20-
ax = fig.add_subplot(111)
19+
fig, ax = plt.subplots()
2120
canvas = fig.canvas
2221

2322
fig.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs

examples/animation/old_animation/animation_blit_gtk2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ def update_line(self, *args):
148148

149149

150150
plt.rcParams["text.usetex"] = False
151-
fig = plt.figure()
152151

153-
ax = fig.add_subplot(111)
152+
fig, ax = plt.subplots()
154153
ax.xaxis.set_animated(True)
155154
ax.yaxis.set_animated(True)
156155
canvas = fig.canvas

examples/animation/old_animation/animation_blit_tk.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
matplotlib.use('TkAgg')
88

99
import sys
10-
import pylab as p
10+
import matplotlib.pyplot as plt
1111
import numpy as npy
1212
import time
1313

14-
ax = p.subplot(111)
14+
fig, ax = plt.subplots()
1515
canvas = ax.figure.canvas
1616

1717

1818
# create the initial line
1919
x = npy.arange(0,2*npy.pi,0.01)
20-
line, = p.plot(x, npy.sin(x), animated=True, lw=2)
20+
line, = plt.plot(x, npy.sin(x), animated=True, lw=2)
2121

2222
def run(*args):
2323
background = canvas.copy_from_bbox(ax.bbox)
@@ -43,12 +43,12 @@ def run(*args):
4343
run.cnt = 0
4444

4545

46-
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
47-
p.grid() # to ensure proper background restore
48-
manager = p.get_current_fig_manager()
46+
plt.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
47+
plt.grid() # to ensure proper background restore
48+
manager = plt.get_current_fig_manager()
4949
manager.window.after(100, run)
5050

51-
p.show()
51+
plt.show()
5252

5353

5454

examples/animation/old_animation/animation_blit_wx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
matplotlib.backends.backend_wxagg._use_accelerator(False)
2525

2626

27-
ax = p.subplot(111)
27+
fig, ax = plt.subplots()
2828
canvas = ax.figure.canvas
2929

3030

examples/animation/old_animation/draggable_legend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import matplotlib.pyplot as plt
22

3-
4-
ax = plt.subplot(111)
3+
fig, ax = plt.subplots()
54
ax.plot([1,2,3], label="test")
65

76
l = ax.legend()
87
d1 = l.draggable()
98

10-
xy = 1, 2
9+
xy = 1, 2
1110
txt = ax.annotate("Test", xy, xytext=(-30, 30),
1211
textcoords="offset points",
1312
bbox=dict(boxstyle="round",fc=(0.2, 1, 1)),

examples/animation/old_animation/gtk_timeout.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import matplotlib.pyplot as plt
77

8-
fig = plt.figure()
9-
ax = fig.add_subplot(111)
8+
fig, ax = plt.subplots()
109
line, = ax.plot(np.random.rand(10))
1110
ax.set_ylim(0, 1)
1211

0 commit comments

Comments
 (0)