Skip to content

Use fig, ax = plt.subplots() in tests (part 2) #18566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 27 additions & 54 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,7 @@ def test_imshow():

# Reuse testcase from above for a labeled data test
data = {"r": r}
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.imshow("r", data=data)


Expand Down Expand Up @@ -1335,8 +1334,7 @@ def test_markevery():
y = np.sin(x) * np.sqrt(x/10 + 0.5)

# check marker only plot
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot(x, y, 'o', label='default')
ax.plot(x, y, 'd', markevery=None, label='mark all')
ax.plot(x, y, 's', markevery=10, label='mark every 10')
Expand All @@ -1350,8 +1348,7 @@ def test_markevery_line():
y = np.sin(x) * np.sqrt(x/10 + 0.5)

# check line/marker combos
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot(x, y, '-o', label='default')
ax.plot(x, y, '-d', markevery=None, label='mark all')
ax.plot(x, y, '-s', markevery=10, label='mark every 10')
Expand Down Expand Up @@ -1465,8 +1462,7 @@ def test_markevery_polar():
@image_comparison(['marker_edges'], remove_text=True)
def test_marker_edges():
x = np.linspace(0, 1, 10)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), 'y.', ms=30.0, mew=0, mec='r')
ax.plot(x+0.1, np.sin(x), 'y.', ms=30.0, mew=1, mec='r')
ax.plot(x+0.2, np.sin(x), 'y.', ms=30.0, mew=2, mec='b')
Expand All @@ -1480,8 +1476,7 @@ def test_bar_tick_label_single():

# Reuse testcase from above for a labeled data test
data = {"a": 0, "b": 1}
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax = plt.gca()
ax.bar("a", "b", align='edge', tick_label='0', data=data)

Expand Down Expand Up @@ -1683,8 +1678,7 @@ def test_pandas_minimal_plot(pd):
def test_hist_log():
data0 = np.linspace(0, 1, 200)**3
data = np.concatenate([1 - data0, 1 + data0])
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist(data, fill=False, log=True)


Expand Down Expand Up @@ -1975,8 +1969,7 @@ def contour_dat():
@image_comparison(['contour_hatching'], remove_text=True, style='mpl20')
def test_contour_hatching():
x, y, z = contour_dat()
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.contourf(x, y, z, 7, hatches=['/', '\\', '//', '-'],
cmap=plt.get_cmap('gray'),
extend='both', alpha=0.5)
Expand All @@ -1989,8 +1982,7 @@ def test_contour_colorbar():

x, y, z = contour_dat()

fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
cs = ax.contourf(x, y, z, levels=np.arange(-1.8, 1.801, 0.2),
cmap=plt.get_cmap('RdBu'),
vmin=-0.6,
Expand All @@ -2017,14 +2009,12 @@ def test_hist2d():
# make it not symmetric in case we switch x and y axis
x = np.random.randn(100)*2+5
y = np.random.randn(100)-2
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=10, rasterized=True)

# Reuse testcase from above for a labeled data test
data = {"x": x, "y": y}
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist2d("x", "y", bins=10, data=data, rasterized=True)


Expand All @@ -2038,8 +2028,7 @@ def test_hist2d_transpose():
# passing to pcolorfast
x = np.array([5]*100)
y = np.random.randn(100)-2
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=10, rasterized=True)


Expand Down Expand Up @@ -2877,8 +2866,7 @@ def test_boxplot_with_CIarray():

x = np.linspace(-7, 7, 140)
x = np.hstack([-25, x, 25])
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
CIs = np.array([[-1.5, 3.], [-1., 3.5]])

# show a boxplot with Matplotlib medians and confidence intervals, and
Expand Down Expand Up @@ -3413,14 +3401,12 @@ def test_hist_stacked_stepfilled():
# make some data
d1 = np.linspace(1, 3, 20)
d2 = np.linspace(0, 10, 50)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist((d1, d2), histtype="stepfilled", stacked=True)

# Reuse testcase from above for a labeled data test
data = {"x": (d1, d2)}
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist("x", histtype="stepfilled", stacked=True, data=data)


Expand All @@ -3429,8 +3415,7 @@ def test_hist_offset():
# make some data
d1 = np.linspace(0, 10, 50)
d2 = np.linspace(1, 3, 20)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist(d1, bottom=5)
ax.hist(d2, bottom=15)

Expand All @@ -3439,8 +3424,7 @@ def test_hist_offset():
def test_hist_step():
# make some data
d1 = np.linspace(1, 3, 20)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist(d1, histtype="step")
ax.set_ylim(0, 10)
ax.set_xlim(-1, 5)
Expand All @@ -3451,8 +3435,7 @@ def test_hist_step_horiz():
# make some data
d1 = np.linspace(0, 10, 50)
d2 = np.linspace(1, 3, 20)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist((d1, d2), histtype="step", orientation="horizontal")


Expand All @@ -3463,8 +3446,7 @@ def test_hist_stacked_weighted():
d2 = np.linspace(1, 3, 20)
w1 = np.linspace(0.01, 3.5, 50)
w2 = np.linspace(0.05, 2., 20)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist((d1, d2), weights=(w1, w2), histtype="stepfilled", stacked=True)


Expand Down Expand Up @@ -3521,8 +3503,7 @@ def test_hist_stacked_stepfilled_alpha():
# make some data
d1 = np.linspace(1, 3, 20)
d2 = np.linspace(0, 10, 50)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist((d1, d2), histtype="stepfilled", stacked=True, alpha=0.5)


Expand All @@ -3531,8 +3512,7 @@ def test_hist_stacked_step():
# make some data
d1 = np.linspace(1, 3, 20)
d2 = np.linspace(0, 10, 50)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist((d1, d2), histtype="step", stacked=True)


Expand All @@ -3549,8 +3529,7 @@ def test_hist_stacked_density():
def test_hist_step_bottom():
# make some data
d1 = np.linspace(1, 3, 20)
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist(d1, bottom=np.arange(10), histtype="stepfilled")


Expand Down Expand Up @@ -3696,8 +3675,7 @@ def test_hist_stacked_bar():
(0.0, 1.0, 0.6549834156005998), (0.0, 0.6569064625276622, 1.0),
(0.28302699607823545, 0.0, 1.0), (0.6849123462299822, 0.0, 1.0)]
labels = ['green', 'orange', ' yellow', 'magenta', 'black']
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist(d, bins=10, histtype='barstacked', align='mid', color=colors,
label=labels)
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncol=1)
Expand All @@ -3710,8 +3688,7 @@ def test_hist_barstacked_bottom_unchanged():


def test_hist_emptydata():
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.hist([[], range(10), range(10)], histtype="step")


Expand All @@ -3735,8 +3712,7 @@ def test_transparent_markers():
np.random.seed(0)
data = np.random.random(50)

fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot(data, 'D', mfc='none', markersize=100)


Expand Down Expand Up @@ -3814,8 +3790,7 @@ def test_alpha():
np.random.seed(0)
data = np.random.random(50)

fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()

# alpha=.5 markers, solid line
ax.plot(data, '-D', color=[1, 0, 0], mfc=[1, 0, 0, .5],
Expand Down Expand Up @@ -3978,8 +3953,7 @@ def test_eventplot_orientation(data, orientation):

@image_comparison(['marker_styles.png'], remove_text=True)
def test_marker_styles():
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
for y, marker in enumerate(sorted(matplotlib.markers.MarkerStyle.markers,
key=lambda x: str(type(x))+str(x))):
ax.plot((y % 2)*5 + np.arange(10)*10, np.ones(10)*10*y, linestyle='',
Expand All @@ -4001,8 +3975,7 @@ def test_vertex_markers():
data = list(range(10))
marker_as_tuple = ((-1, -1), (1, -1), (1, 1), (-1, 1))
marker_as_list = [(-1, -1), (1, -1), (1, 1), (-1, 1)]
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot(data, linestyle='', marker=marker_as_tuple, mfc='k')
ax.plot(data[::-1], linestyle='', marker=marker_as_list, mfc='b')
ax.set_xlim([-1, 10])
Expand Down
12 changes: 4 additions & 8 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ def test_use14corefonts():
def test_type42():
rcParams['pdf.fonttype'] = 42

fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
fig.savefig(io.BytesIO())


def test_multipage_pagecount():
with PdfPages(io.BytesIO()) as pdf:
assert pdf.get_pagecount() == 0
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
fig.savefig(pdf, format="pdf")
assert pdf.get_pagecount() == 1
Expand All @@ -64,8 +62,7 @@ def test_multipage_properfinalize():
pdfio = io.BytesIO()
with PdfPages(pdfio) as pdf:
for i in range(10):
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.set_title('This is a long title')
fig.savefig(pdf, format="pdf")
pdfio.seek(0)
Expand All @@ -86,8 +83,7 @@ def test_multipage_keep_empty():
pass
assert not os.path.exists(filename)
# test pdf files with content, they should never be deleted
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
# test that a non-empty pdf is left behind with keep_empty=True (default)
with NamedTemporaryFile(delete=False) as tmp:
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_bbox_tight.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def test_bbox_inches_tight_clipping():
remove_text=True, savefig_kwarg={'bbox_inches': 'tight'})
def test_bbox_inches_tight_raster():
"""Test rasterization with tight_layout"""
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot([1.0, 2.0], rasterized=True)


Expand Down
18 changes: 6 additions & 12 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def test_legend_ordereddict():
@image_comparison(['legend_auto1'], remove_text=True)
def test_legend_auto1():
"""Test automatic legend placement"""
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
x = np.arange(100)
ax.plot(x, 50 - x, 'o', label='y=1')
ax.plot(x, x - 50, 'o', label='y=-1')
Expand All @@ -47,8 +46,7 @@ def test_legend_auto1():
@image_comparison(['legend_auto2'], remove_text=True)
def test_legend_auto2():
"""Test automatic legend placement"""
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
x = np.arange(100)
b1 = ax.bar(x, x, align='edge', color='m')
b2 = ax.bar(x, x[::-1], align='edge', color='g')
Expand All @@ -58,8 +56,7 @@ def test_legend_auto2():
@image_comparison(['legend_auto3'])
def test_legend_auto3():
"""Test automatic legend placement"""
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5]
y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5]
ax.plot(x, y, 'o-', label='line')
Expand All @@ -82,8 +79,7 @@ def test_various_labels():
@image_comparison(['legend_labels_first.png'], remove_text=True)
def test_labels_first():
# test labels to left of markers
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.plot(np.arange(10), '-o', label=1)
ax.plot(np.ones(10)*5, ':x', label="x")
ax.plot(np.arange(20, 10, -1), 'd', label="diamond")
Expand All @@ -93,8 +89,7 @@ def test_labels_first():
@image_comparison(['legend_multiple_keys.png'], remove_text=True)
def test_multiple_keys():
# test legend entries with multiple keys
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
p1, = ax.plot([1, 2, 3], '-o')
p2, = ax.plot([2, 3, 4], '-x')
p3, = ax.plot([3, 4, 5], '-d')
Expand Down Expand Up @@ -389,8 +384,7 @@ def test_warn_args_kwargs(self):
def test_legend_stackplot():
"""Test legend for PolyCollection using stackplot."""
# related to #1341, #1943, and PR #3303
fig = plt.figure()
ax = fig.add_subplot()
fig, ax = plt.subplots()
x = np.linspace(0, 10, 10)
y1 = 1.0 * x
y2 = 2.0 * x + 1
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/tests/test_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def test_negative_rect():

@image_comparison(['clip_to_bbox'])
def test_clip_to_bbox():
fig = plt.figure()

ax = fig.add_subplot()
fig, ax = plt.subplots()
ax.set_xlim([-18, 20])
ax.set_ylim([-150, 100])

Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def find_matplotlib_font(**kw):
UserWarning,
module='matplotlib.font_manager')

plt.figure()
ax = plt.subplot(1, 1, 1)
fig, ax = plt.subplots()

normalFont = find_matplotlib_font(
family="sans-serif",
Expand Down
Loading