Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use fig, ax = plt.subplots() in tests #18553

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 24, 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
50 changes: 20 additions & 30 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2425,8 +2425,7 @@ def test_pyplot_axes():

@image_comparison(['log_scales'])
def test_log_scales():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.plot(np.log(np.linspace(0.1, 100)))
ax.set_yscale('log', base=5.5)
ax.invert_yaxis()
Expand All @@ -2441,8 +2440,7 @@ def test_log_scales_no_data():


def test_log_scales_invalid():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.set_xscale('log')
with pytest.warns(UserWarning, match='Attempted to set non-positive'):
ax.set_xlim(-1, 10)
Expand All @@ -2465,8 +2463,7 @@ def test_stackplot():

# Reuse testcase from above for a labeled data test
data = {"x": x, "y1": y1, "y2": y2, "y3": y3}
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.stackplot("x", "y1", "y2", "y3", data=data)
ax.set_xlim((0, 10))
ax.set_ylim((0, 70))
Expand Down Expand Up @@ -3317,27 +3314,26 @@ def test_errorbar_limits():
yerr = 0.2
ls = 'dotted'

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

# standard error bars
plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls, color='blue')
ax.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls, color='blue')

# including upper limits
uplims = np.zeros_like(x)
uplims[[1, 5, 9]] = True
plt.errorbar(x, y+0.5, xerr=xerr, yerr=yerr, uplims=uplims, ls=ls,
color='green')
ax.errorbar(x, y+0.5, xerr=xerr, yerr=yerr, uplims=uplims, ls=ls,
color='green')

# including lower limits
lolims = np.zeros_like(x)
lolims[[2, 4, 8]] = True
plt.errorbar(x, y+1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls,
color='red')
ax.errorbar(x, y+1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls,
color='red')

# including upper and lower limits
plt.errorbar(x, y+1.5, marker='o', ms=8, xerr=xerr, yerr=yerr,
lolims=lolims, uplims=uplims, ls=ls, color='magenta')
ax.errorbar(x, y+1.5, marker='o', ms=8, xerr=xerr, yerr=yerr,
lolims=lolims, uplims=uplims, ls=ls, color='magenta')

# including xlower and xupper limits
xerr = 0.2
Expand All @@ -3349,10 +3345,10 @@ def test_errorbar_limits():
uplims = np.zeros_like(x)
lolims[[6]] = True
uplims[[3]] = True
plt.errorbar(x, y+2.1, marker='o', ms=8, xerr=xerr, yerr=yerr,
xlolims=xlolims, xuplims=xuplims, uplims=uplims,
lolims=lolims, ls='none', mec='blue', capsize=0,
color='cyan')
ax.errorbar(x, y+2.1, marker='o', ms=8, xerr=xerr, yerr=yerr,
xlolims=xlolims, xuplims=xuplims, uplims=uplims,
lolims=lolims, ls='none', mec='blue', capsize=0,
color='cyan')
ax.set_xlim((0, 5.5))
ax.set_title('Errorbar upper and lower limits')

Expand Down Expand Up @@ -3484,13 +3480,11 @@ def test_stem(use_line_collection):
ax.stem(x, np.cos(x),
linefmt='C2-.', markerfmt='k+', basefmt='C1-.', label=' ',
use_line_collection=use_line_collection)

ax.legend()


def test_stem_args():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()

x = list(range(10))
y = list(range(10))
Expand Down Expand Up @@ -4229,8 +4223,7 @@ def test_step_linestyle():
@image_comparison(['mixed_collection'], remove_text=True)
def test_mixed_collection():
# First illustrate basic pyplot interface, using defaults where possible.
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()

c = mpatches.Circle((8, 8), radius=4, facecolor='none', edgecolor='green')

Expand Down Expand Up @@ -4571,8 +4564,7 @@ def test_rcparam_grid_minor():

for locator, result in values:
matplotlib.rcParams['axes.grid.which'] = locator
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
assert (ax.xaxis._gridOnMajor, ax.xaxis._gridOnMinor) == result

matplotlib.rcParams['axes.grid'] = orig_grid
Expand Down Expand Up @@ -5786,16 +5778,14 @@ def test_title_no_move_off_page():

def test_offset_label_color():
# Tests issue 6440
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.plot([1.01e9, 1.02e9, 1.03e9])
ax.yaxis.set_tick_params(labelcolor='red')
assert ax.yaxis.get_offset_text().get_color() == 'red'


def test_offset_text_visible():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.plot([1.01e9, 1.02e9, 1.03e9])
ax.yaxis.set_tick_params(label1On=False, label2On=True)
assert ax.yaxis.get_offset_text().get_visible()
Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def test_use14corefonts():
and containing some French characters and the euro symbol:
"Merci pépé pour les 10 €"'''

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.set_title('Test PDF backend with option use14corefonts=True')
ax.text(0.5, 0.5, text, horizontalalignment='center',
verticalalignment='bottom',
Expand Down Expand Up @@ -111,8 +110,7 @@ def test_composite_image():
# (on a single set of axes) into a single composite image.
X, Y = np.meshgrid(np.arange(-5, 5, 1), np.arange(-5, 5, 1))
Z = np.sin(Y ** 2)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.set_xlim(0, 3)
ax.imshow(Z, extent=[0, 1, 0, 1])
ax.imshow(Z[::-1], extent=[2, 3, 0, 1])
Expand Down
12 changes: 4 additions & 8 deletions lib/matplotlib/tests/test_backend_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def test_visibility():

@image_comparison(['fill_black_with_alpha.svg'], remove_text=True)
def test_fill_black_with_alpha():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.scatter(x=[0, 0.1, 1], y=[0, 0, 0], c='k', alpha=0.1, s=10000)


Expand All @@ -50,8 +49,7 @@ def test_noscale():
X, Y = np.meshgrid(np.arange(-5, 5, 1), np.arange(-5, 5, 1))
Z = np.sin(Y ** 2)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.imshow(Z, cmap='gray', interpolation='none')


Expand All @@ -71,8 +69,7 @@ def test_text_urls():

@image_comparison(['bold_font_output.svg'])
def test_bold_font_output():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.plot(np.arange(10), np.arange(10))
ax.set_xlabel('nonbold-xlabel')
ax.set_ylabel('bold-ylabel', fontweight='bold')
Expand All @@ -82,8 +79,7 @@ def test_bold_font_output():
@image_comparison(['bold_font_output_with_none_fonttype.svg'])
def test_bold_font_output_with_none_fonttype():
plt.rcParams['svg.fonttype'] = 'none'
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.plot(np.arange(10), np.arange(10))
ax.set_xlabel('nonbold-xlabel')
ax.set_ylabel('bold-ylabel', fontweight='bold')
Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def generate_EventCollection_plot():
antialiased=antialiased
)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.add_collection(coll)
ax.set_title('EventCollection: default')
props = {'positions': positions,
Expand Down Expand Up @@ -519,8 +518,7 @@ def test_joinstyle():

@image_comparison(['cap_and_joinstyle.png'])
def test_cap_and_joinstyle_image():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([-0.5, 2.5])

Expand Down
29 changes: 11 additions & 18 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,16 @@ def test_date_empty():
# make sure we do the right thing when told to plot dates even
# if no date data has been presented, cf
# http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.xaxis_date()


@image_comparison(['date_axhspan.png'])
def test_date_axhspan():
# test ax hspan with date inputs
# test axhspan with date inputs
t0 = datetime.datetime(2009, 1, 20)
tf = datetime.datetime(2009, 1, 21)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.axhspan(t0, tf, facecolor="blue", alpha=0.25)
ax.set_ylim(t0 - datetime.timedelta(days=5),
tf + datetime.timedelta(days=5))
Expand All @@ -94,11 +92,10 @@ def test_date_axhspan():

@image_comparison(['date_axvspan.png'])
def test_date_axvspan():
# test ax hspan with date inputs
# test axvspan with date inputs
t0 = datetime.datetime(2000, 1, 20)
tf = datetime.datetime(2010, 1, 21)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.axvspan(t0, tf, facecolor="blue", alpha=0.25)
ax.set_xlim(t0 - datetime.timedelta(days=720),
tf + datetime.timedelta(days=720))
Expand All @@ -107,11 +104,10 @@ def test_date_axvspan():

@image_comparison(['date_axhline.png'])
def test_date_axhline():
# test ax hline with date inputs
# test axhline with date inputs
t0 = datetime.datetime(2009, 1, 20)
tf = datetime.datetime(2009, 1, 31)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.axhline(t0, color="blue", lw=3)
ax.set_ylim(t0 - datetime.timedelta(days=5),
tf + datetime.timedelta(days=5))
Expand All @@ -120,11 +116,10 @@ def test_date_axhline():

@image_comparison(['date_axvline.png'])
def test_date_axvline():
# test ax hline with date inputs
# test axvline with date inputs
t0 = datetime.datetime(2000, 1, 20)
tf = datetime.datetime(2000, 1, 21)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.axvline(t0, color="red", lw=3)
ax.set_xlim(t0 - datetime.timedelta(days=5),
tf + datetime.timedelta(days=5))
Expand All @@ -140,8 +135,7 @@ def test_too_many_date_ticks(caplog):
caplog.set_level("WARNING")
t0 = datetime.datetime(2000, 1, 20)
tf = datetime.datetime(2000, 1, 20)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
with pytest.warns(UserWarning) as rec:
ax.set_xlim((t0, tf), auto=True)
assert len(rec) == 1
Expand Down Expand Up @@ -733,8 +727,7 @@ def test_date_inverted_limit():
# test ax hline with date inputs
t0 = datetime.datetime(2009, 1, 20)
tf = datetime.datetime(2009, 1, 31)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.axhline(t0, color="blue", lw=3)
ax.set_ylim(t0 - datetime.timedelta(days=5),
tf + datetime.timedelta(days=5))
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ def test_hatching():


def test_legend_remove():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
lines = ax.plot(range(10))
leg = fig.legend(lines, "test")
leg.remove()
Expand Down
15 changes: 5 additions & 10 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def test_invisible_Line_rendering():


def test_set_line_coll_dash():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
np.random.seed(0)
# Testing setting linestyles for line collections.
# This should not produce an error.
Expand All @@ -83,15 +82,13 @@ def test_set_line_coll_dash():

@image_comparison(['line_dashes'], remove_text=True)
def test_line_dashes():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()

ax.plot(range(10), linestyle=(0, (3, 3)), lw=5)


def test_line_colors():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.plot(range(10), color='none')
ax.plot(range(10), color='r')
ax.plot(range(10), color='.3')
Expand All @@ -107,8 +104,7 @@ def test_valid_colors():


def test_linestyle_variants():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
for ls in ["-", "solid", "--", "dashed",
"-.", "dashdot", ":", "dotted"]:
ax.plot(range(10), linestyle=ls)
Expand Down Expand Up @@ -155,8 +151,7 @@ def test_set_drawstyle():

@image_comparison(['line_collection_dashes'], remove_text=True, style='mpl20')
def test_set_line_coll_dash_image():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
np.random.seed(0)
ax.contour(np.random.randn(20, 30), linestyles=[(0, (3, 3))])

Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ def test_patch_linestyle_accents():
linestyles = ["-", "--", "-.", ":",
"solid", "dashed", "dashdot", "dotted"]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
for i, ls in enumerate(linestyles):
star = mpath.Path(verts + i, codes)
patch = mpatches.PathPatch(star,
Expand Down
9 changes: 3 additions & 6 deletions lib/matplotlib/tests/test_spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def test_spines_axes_positions():

@image_comparison(['spines_data_positions'])
def test_spines_data_positions():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.spines['left'].set_position(('data', -1.5))
ax.spines['top'].set_position(('data', 0.5))
ax.spines['right'].set_position(('data', -0.5))
Expand Down Expand Up @@ -58,15 +57,13 @@ def test_spine_nonlinear_data_positions(fig_test, fig_ref):
def test_spines_capstyle():
# issue 2542
plt.rc('axes', linewidth=20)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
ax.set_xticks([])
ax.set_yticks([])


def test_label_without_ticks():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.3, bottom=0.3)
ax.plot(np.arange(10))
ax.yaxis.set_ticks_position('left')
Expand Down