Skip to content

Commit b152acd

Browse files
authored
Merge pull request #12001 from timhoffm/no-explicit-fignum-part2
Do not use an explicit figum in plt.figure(1, ...) in simple cases
2 parents f6ea9a5 + e84b23b commit b152acd

27 files changed

+53
-54
lines changed

examples/axes_grid1/demo_axes_divider.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ def demo_simple_image(ax):
2626
plt.setp(cb.ax.get_yticklabels(), visible=False)
2727

2828

29-
def demo_locatable_axes_hard(fig1):
29+
def demo_locatable_axes_hard(fig):
3030

3131
from mpl_toolkits.axes_grid1 import SubplotDivider, Size
3232
from mpl_toolkits.axes_grid1.mpl_axes import Axes
3333

34-
divider = SubplotDivider(fig1, 2, 2, 2, aspect=True)
34+
divider = SubplotDivider(fig, 2, 2, 2, aspect=True)
3535

3636
# axes for image
37-
ax = Axes(fig1, divider.get_position())
37+
ax = Axes(fig, divider.get_position())
3838

3939
# axes for colorbar
40-
ax_cb = Axes(fig1, divider.get_position())
40+
ax_cb = Axes(fig, divider.get_position())
4141

4242
h = [Size.AxesX(ax), # main axes
4343
Size.Fixed(0.05), # padding, 0.1 inch
@@ -52,8 +52,8 @@ def demo_locatable_axes_hard(fig1):
5252
ax.set_axes_locator(divider.new_locator(nx=0, ny=0))
5353
ax_cb.set_axes_locator(divider.new_locator(nx=2, ny=0))
5454

55-
fig1.add_axes(ax)
56-
fig1.add_axes(ax_cb)
55+
fig.add_axes(ax)
56+
fig.add_axes(ax_cb)
5757

5858
ax_cb.axis["left"].toggle(all=False)
5959
ax_cb.axis["right"].toggle(ticks=True)
@@ -71,8 +71,8 @@ def demo_locatable_axes_easy(ax):
7171
divider = make_axes_locatable(ax)
7272

7373
ax_cb = divider.new_horizontal(size="5%", pad=0.05)
74-
fig1 = ax.get_figure()
75-
fig1.add_axes(ax_cb)
74+
fig = ax.get_figure()
75+
fig.add_axes(ax_cb)
7676

7777
Z, extent = get_demo_image()
7878
im = ax.imshow(Z, extent=extent, interpolation="nearest")
@@ -99,31 +99,30 @@ def demo_images_side_by_side(ax):
9999

100100
def demo():
101101

102-
fig1 = plt.figure(1, (6, 6))
103-
fig1.clf()
102+
fig = plt.figure(figsize=(6, 6))
104103

105104
# PLOT 1
106105
# simple image & colorbar
107-
ax = fig1.add_subplot(2, 2, 1)
106+
ax = fig.add_subplot(2, 2, 1)
108107
demo_simple_image(ax)
109108

110109
# PLOT 2
111110
# image and colorbar whose location is adjusted in the drawing time.
112111
# a hard way
113112

114-
demo_locatable_axes_hard(fig1)
113+
demo_locatable_axes_hard(fig)
115114

116115
# PLOT 3
117116
# image and colorbar whose location is adjusted in the drawing time.
118117
# a easy way
119118

120-
ax = fig1.add_subplot(2, 2, 3)
119+
ax = fig.add_subplot(2, 2, 3)
121120
demo_locatable_axes_easy(ax)
122121

123122
# PLOT 4
124123
# two images side by side with fixed padding.
125124

126-
ax = fig1.add_subplot(2, 2, 4)
125+
ax = fig.add_subplot(2, 2, 4)
127126
demo_images_side_by_side(ax)
128127

129128
plt.show()

examples/axes_grid1/demo_fixed_size_axes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111

1212

1313
def demo_fixed_size_axes():
14-
fig1 = plt.figure(1, (6, 6))
14+
fig = plt.figure(figsize=(6, 6))
1515

1616
# The first items are for padding and the second items are for the axes.
1717
# sizes are in inch.
1818
h = [Size.Fixed(1.0), Size.Fixed(4.5)]
1919
v = [Size.Fixed(0.7), Size.Fixed(5.)]
2020

21-
divider = Divider(fig1, (0.0, 0.0, 1., 1.), h, v, aspect=False)
21+
divider = Divider(fig, (0.0, 0.0, 1., 1.), h, v, aspect=False)
2222
# the width and height of the rectangle is ignored.
2323

24-
ax = Axes(fig1, divider.get_position())
24+
ax = Axes(fig, divider.get_position())
2525
ax.set_axes_locator(divider.new_locator(nx=1, ny=1))
2626

27-
fig1.add_axes(ax)
27+
fig.add_axes(ax)
2828

2929
ax.plot([1, 2, 3])
3030

3131

3232
def demo_fixed_pad_axes():
33-
fig = plt.figure(2, (6, 6))
33+
fig = plt.figure(figsize=(6, 6))
3434

3535
# The first & third items are for padding and the second items are for the
3636
# axes. Sizes are in inches.

examples/axes_grid1/simple_axes_divider2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
from mpl_toolkits.axes_grid1 import Divider
99
import matplotlib.pyplot as plt
1010

11-
fig1 = plt.figure(1, (5.5, 4.))
11+
fig = plt.figure(figsize=(5.5, 4.))
1212

1313
# the rect parameter will be ignore as we will set axes_locator
1414
rect = (0.1, 0.1, 0.8, 0.8)
15-
ax = [fig1.add_axes(rect, label="%d" % i) for i in range(4)]
15+
ax = [fig.add_axes(rect, label="%d" % i) for i in range(4)]
1616

1717
horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.),
1818
Size.Scaled(.5)]
1919

2020
vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)]
2121

2222
# divide the axes rectangle into grid whose size is specified by horiz * vert
23-
divider = Divider(fig1, rect, horiz, vert, aspect=False)
23+
divider = Divider(fig, rect, horiz, vert, aspect=False)
2424

2525
ax[0].set_axes_locator(divider.new_locator(nx=0, ny=0))
2626
ax[1].set_axes_locator(divider.new_locator(nx=0, ny=2))

examples/axes_grid1/simple_axes_divider3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
import matplotlib.pyplot as plt
1010

1111

12-
fig1 = plt.figure(1, (5.5, 4))
12+
fig = plt.figure(figsize=(5.5, 4))
1313

1414
# the rect parameter will be ignore as we will set axes_locator
1515
rect = (0.1, 0.1, 0.8, 0.8)
16-
ax = [fig1.add_axes(rect, label="%d" % i) for i in range(4)]
16+
ax = [fig.add_axes(rect, label="%d" % i) for i in range(4)]
1717

1818

1919
horiz = [Size.AxesX(ax[0]), Size.Fixed(.5), Size.AxesX(ax[1])]
2020
vert = [Size.AxesY(ax[0]), Size.Fixed(.5), Size.AxesY(ax[2])]
2121

2222
# divide the axes rectangle into grid whose size is specified by horiz * vert
23-
divider = Divider(fig1, rect, horiz, vert, aspect=False)
23+
divider = Divider(fig, rect, horiz, vert, aspect=False)
2424

2525

2626
ax[0].set_axes_locator(divider.new_locator(nx=0, ny=0))

examples/axisartist/demo_axis_direction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def add_floating_axis2(ax1):
7373
return axis
7474

7575

76-
fig = plt.figure(1, figsize=(8, 4))
76+
fig = plt.figure(figsize=(8, 4))
7777
fig.clf()
7878
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99,
7979
wspace=0.01, hspace=0.01)

examples/axisartist/demo_curvelinear_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def curvelinear_test2(fig):
131131
ax1.grid(True, zorder=0)
132132

133133
if 1:
134-
fig = plt.figure(1, figsize=(7, 4))
134+
fig = plt.figure(figsize=(7, 4))
135135
fig.clf()
136136

137137
curvelinear_test1(fig)

examples/axisartist/demo_curvelinear_grid2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def inv_tr(x, y):
6565

6666

6767
if 1:
68-
fig = plt.figure(1, figsize=(7, 4))
68+
fig = plt.figure(figsize=(7, 4))
6969
fig.clf()
7070

7171
curvelinear_test1(fig)

examples/axisartist/demo_floating_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def setup_axes3(fig, rect):
143143

144144

145145
##########################################################
146-
fig = plt.figure(1, figsize=(8, 4))
146+
fig = plt.figure(figsize=(8, 4))
147147
fig.subplots_adjust(wspace=0.3, left=0.05, right=0.95)
148148

149149
ax1, aux_ax1 = setup_axes1(fig, 131)

examples/axisartist/demo_floating_axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def curvelinear_test2(fig):
6464

6565
ax1.grid(True)
6666

67-
fig = plt.figure(1, figsize=(5, 5))
67+
fig = plt.figure(figsize=(5, 5))
6868
fig.clf()
6969

7070
curvelinear_test2(fig)

examples/axisartist/demo_ticklabel_alignment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setup_axes(fig, rect):
2222
return ax
2323

2424

25-
fig = plt.figure(1, figsize=(3, 5))
25+
fig = plt.figure(figsize=(3, 5))
2626
fig.subplots_adjust(left=0.5, hspace=0.7)
2727

2828
ax = setup_axes(fig, 311)

examples/axisartist/demo_ticklabel_direction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def setup_axes(fig, rect):
1919
return ax
2020

2121

22-
fig = plt.figure(1, figsize=(6, 3))
22+
fig = plt.figure(figsize=(6, 3))
2323
fig.subplots_adjust(bottom=0.2)
2424

2525
ax = setup_axes(fig, 131)

examples/axisartist/simple_axis_direction03.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def setup_axes(fig, rect):
1919
return ax
2020

2121

22-
fig = plt.figure(1, figsize=(5, 2))
22+
fig = plt.figure(figsize=(5, 2))
2323
fig.subplots_adjust(wspace=0.4, bottom=0.3)
2424

2525
ax1 = setup_axes(fig, "121")

examples/axisartist/simple_axis_pad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def add_floating_axis2(ax1):
7373
return axis
7474

7575

76-
fig = plt.figure(1, figsize=(9, 3.))
76+
fig = plt.figure(figsize=(9, 3.))
7777
fig.clf()
7878
fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99,
7979
wspace=0.01, hspace=0.01)

examples/axisartist/simple_axisline2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from mpl_toolkits.axisartist.axislines import SubplotZero
99
import numpy as np
1010

11-
fig = plt.figure(1, (4, 3))
11+
fig = plt.figure(figsize=(4, 3))
1212

1313
# a subplot with two additional axis, "xzero" and "yzero". "xzero" is
1414
# y=0 line, and "yzero" is x=0 line.

examples/axisartist/simple_axisline3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import matplotlib.pyplot as plt
88
from mpl_toolkits.axisartist.axislines import Subplot
99

10-
fig = plt.figure(1, (3, 3))
10+
fig = plt.figure(figsize=(3, 3))
1111

1212
ax = Subplot(fig, 111)
1313
fig.add_subplot(ax)

examples/lines_bars_and_markers/scatter_hist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
rect_histy = [left_h, bottom, 0.2, height]
3232

3333
# start with a rectangular Figure
34-
plt.figure(1, figsize=(8, 8))
34+
plt.figure(figsize=(8, 8))
3535

3636
axScatter = plt.axes(rect_scatter)
3737
axHistx = plt.axes(rect_histx)

examples/misc/demo_agg_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def light_filter_pie(ax):
309309

310310
if 1:
311311

312-
plt.figure(1, figsize=(6, 6))
312+
plt.figure(figsize=(6, 6))
313313
plt.subplots_adjust(left=0.05, right=0.95)
314314

315315
ax = plt.subplot(221)

examples/misc/patheffect_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010

1111
if 1:
12-
plt.figure(1, figsize=(8, 3))
12+
plt.figure(figsize=(8, 3))
1313
ax1 = plt.subplot(131)
1414
ax1.imshow([[1, 2], [2, 3]])
1515
txt = ax1.annotate("test", (1., 1.), (0., 0),

examples/misc/svg_filter_pie.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from matplotlib.patches import Shadow
1515

1616
# make a square figure and axes
17-
fig1 = plt.figure(1, figsize=(6, 6))
18-
ax = fig1.add_axes([0.1, 0.1, 0.8, 0.8])
17+
fig = plt.figure(figsize=(6, 6))
18+
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
1919

2020
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
2121
fracs = [15, 30, 45, 10]

examples/pyplots/whats_new_98_4_fancy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import matplotlib.pyplot as plt
1010

1111
figheight = 8
12-
fig = plt.figure(1, figsize=(9, figheight), dpi=80)
12+
fig = plt.figure(figsize=(9, figheight), dpi=80)
1313
fontsize = 0.4 * fig.dpi
1414

1515
def make_boxstyles(ax):

examples/shapes_and_collections/fancybox_demo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
spacing = 1.2
2121

2222
figheight = (spacing * len(styles) + .5)
23-
fig1 = plt.figure(1, (4 / 1.5, figheight / 1.5))
23+
fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5))
2424
fontsize = 0.3 * 72
2525

2626
for i, stylename in enumerate(sorted(styles)):
27-
fig1.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename,
27+
fig.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename,
2828
ha="center",
2929
size=fontsize,
30-
transform=fig1.transFigure,
30+
transform=fig.transFigure,
3131
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
3232

3333
plt.show()

examples/subplots_axes_and_figures/axes_zoom_effect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def zoom_effect02(ax1, ax2, **kwargs):
106106

107107
import matplotlib.pyplot as plt
108108

109-
plt.figure(1, figsize=(5, 5))
109+
plt.figure(figsize=(5, 5))
110110
ax1 = plt.subplot(221)
111111
ax2 = plt.subplot(212)
112112
ax2.set_xlim(0, 1)

examples/text_labels_and_annotations/demo_text_rotation_mode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_rotation_mode(fig, mode, subplot_location):
4444

4545
if 1:
4646
import matplotlib.pyplot as plt
47-
fig = plt.figure(1, figsize=(5.5, 4))
47+
fig = plt.figure(figsize=(5.5, 4))
4848
fig.clf()
4949

5050
test_rotation_mode(fig, "default", 121)

examples/text_labels_and_annotations/fancyarrow_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
ncol = 2
1313
nrow = (len(styles) + 1) // ncol
1414
figheight = (nrow + 0.5)
15-
fig1 = plt.figure(1, (4 * ncol / 1.5, figheight / 1.5))
15+
fig = plt.figure(figsize=(4 * ncol / 1.5, figheight / 1.5))
1616
fontsize = 0.2 * 70
1717

1818

19-
ax = fig1.add_axes([0, 0, 1, 1], frameon=False, aspect=1.)
19+
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1.)
2020

2121
ax.set_xlim(0, 4 * ncol)
2222
ax.set_ylim(0, figheight)

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
14471447
y = np.sin(x**2)
14481448
14491449
# Create a figure
1450-
plt.figure(1, clear=True)
1450+
plt.figure()
14511451
14521452
# Creates a subplot
14531453
ax = fig.subplots()

lib/matplotlib/tests/test_arrow_patches.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def test_boxarrow():
3737
spacing = 1.2
3838

3939
figheight = (n * spacing + .5)
40-
fig1 = plt.figure(1, figsize=(4 / 1.5, figheight / 1.5))
40+
fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5))
4141

4242
fontsize = 0.3 * 72
4343

4444
for i, stylename in enumerate(sorted(styles)):
45-
fig1.text(0.5, ((n - i) * spacing - 0.5)/figheight, stylename,
45+
fig.text(0.5, ((n - i) * spacing - 0.5)/figheight, stylename,
4646
ha="center",
4747
size=fontsize,
48-
transform=fig1.transFigure,
48+
transform=fig.transFigure,
4949
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
5050

5151

tutorials/introductory/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
names = ['group_a', 'group_b', 'group_c']
132132
values = [1, 10, 100]
133133

134-
plt.figure(1, figsize=(9, 3))
134+
plt.figure(figsize=(9, 3))
135135

136136
plt.subplot(131)
137137
plt.bar(names, values)

0 commit comments

Comments
 (0)