Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitde87612

Browse files
authored
Merge pull request#18566 from timhoffm/test-subplots3
TST: Use fig, ax = plt.subplots() in tests (part 2)
2 parents34f99a9 +f70557f commitde87612

File tree

7 files changed

+41
-83
lines changed

7 files changed

+41
-83
lines changed

‎lib/matplotlib/tests/test_axes.py

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,7 @@ def test_imshow():
862862

863863
# Reuse testcase from above for a labeled data test
864864
data= {"r":r}
865-
fig=plt.figure()
866-
ax=fig.add_subplot()
865+
fig,ax=plt.subplots()
867866
ax.imshow("r",data=data)
868867

869868

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

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

13521350
# check line/marker combos
1353-
fig=plt.figure()
1354-
ax=fig.add_subplot()
1351+
fig,ax=plt.subplots()
13551352
ax.plot(x,y,'-o',label='default')
13561353
ax.plot(x,y,'-d',markevery=None,label='mark all')
13571354
ax.plot(x,y,'-s',markevery=10,label='mark every 10')
@@ -1465,8 +1462,7 @@ def test_markevery_polar():
14651462
@image_comparison(['marker_edges'],remove_text=True)
14661463
deftest_marker_edges():
14671464
x=np.linspace(0,1,10)
1468-
fig=plt.figure()
1469-
ax=fig.add_subplot()
1465+
fig,ax=plt.subplots()
14701466
ax.plot(x,np.sin(x),'y.',ms=30.0,mew=0,mec='r')
14711467
ax.plot(x+0.1,np.sin(x),'y.',ms=30.0,mew=1,mec='r')
14721468
ax.plot(x+0.2,np.sin(x),'y.',ms=30.0,mew=2,mec='b')
@@ -1480,8 +1476,7 @@ def test_bar_tick_label_single():
14801476

14811477
# Reuse testcase from above for a labeled data test
14821478
data= {"a":0,"b":1}
1483-
fig=plt.figure()
1484-
ax=fig.add_subplot()
1479+
fig,ax=plt.subplots()
14851480
ax=plt.gca()
14861481
ax.bar("a","b",align='edge',tick_label='0',data=data)
14871482

@@ -1683,8 +1678,7 @@ def test_pandas_minimal_plot(pd):
16831678
deftest_hist_log():
16841679
data0=np.linspace(0,1,200)**3
16851680
data=np.concatenate([1-data0,1+data0])
1686-
fig=plt.figure()
1687-
ax=fig.add_subplot()
1681+
fig,ax=plt.subplots()
16881682
ax.hist(data,fill=False,log=True)
16891683

16901684

@@ -1975,8 +1969,7 @@ def contour_dat():
19751969
@image_comparison(['contour_hatching'],remove_text=True,style='mpl20')
19761970
deftest_contour_hatching():
19771971
x,y,z=contour_dat()
1978-
fig=plt.figure()
1979-
ax=fig.add_subplot()
1972+
fig,ax=plt.subplots()
19801973
ax.contourf(x,y,z,7,hatches=['/','\\','//','-'],
19811974
cmap=plt.get_cmap('gray'),
19821975
extend='both',alpha=0.5)
@@ -1989,8 +1982,7 @@ def test_contour_colorbar():
19891982

19901983
x,y,z=contour_dat()
19911984

1992-
fig=plt.figure()
1993-
ax=fig.add_subplot()
1985+
fig,ax=plt.subplots()
19941986
cs=ax.contourf(x,y,z,levels=np.arange(-1.8,1.801,0.2),
19951987
cmap=plt.get_cmap('RdBu'),
19961988
vmin=-0.6,
@@ -2017,14 +2009,12 @@ def test_hist2d():
20172009
# make it not symmetric in case we switch x and y axis
20182010
x=np.random.randn(100)*2+5
20192011
y=np.random.randn(100)-2
2020-
fig=plt.figure()
2021-
ax=fig.add_subplot()
2012+
fig,ax=plt.subplots()
20222013
ax.hist2d(x,y,bins=10,rasterized=True)
20232014

20242015
# Reuse testcase from above for a labeled data test
20252016
data= {"x":x,"y":y}
2026-
fig=plt.figure()
2027-
ax=fig.add_subplot()
2017+
fig,ax=plt.subplots()
20282018
ax.hist2d("x","y",bins=10,data=data,rasterized=True)
20292019

20302020

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

20452034

@@ -2877,8 +2866,7 @@ def test_boxplot_with_CIarray():
28772866

28782867
x=np.linspace(-7,7,140)
28792868
x=np.hstack([-25,x,25])
2880-
fig=plt.figure()
2881-
ax=fig.add_subplot()
2869+
fig,ax=plt.subplots()
28822870
CIs=np.array([[-1.5,3.], [-1.,3.5]])
28832871

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

34203407
# Reuse testcase from above for a labeled data test
34213408
data= {"x": (d1,d2)}
3422-
fig=plt.figure()
3423-
ax=fig.add_subplot()
3409+
fig,ax=plt.subplots()
34243410
ax.hist("x",histtype="stepfilled",stacked=True,data=data)
34253411

34263412

@@ -3429,8 +3415,7 @@ def test_hist_offset():
34293415
# make some data
34303416
d1=np.linspace(0,10,50)
34313417
d2=np.linspace(1,3,20)
3432-
fig=plt.figure()
3433-
ax=fig.add_subplot()
3418+
fig,ax=plt.subplots()
34343419
ax.hist(d1,bottom=5)
34353420
ax.hist(d2,bottom=15)
34363421

@@ -3439,8 +3424,7 @@ def test_hist_offset():
34393424
deftest_hist_step():
34403425
# make some data
34413426
d1=np.linspace(1,3,20)
3442-
fig=plt.figure()
3443-
ax=fig.add_subplot()
3427+
fig,ax=plt.subplots()
34443428
ax.hist(d1,histtype="step")
34453429
ax.set_ylim(0,10)
34463430
ax.set_xlim(-1,5)
@@ -3451,8 +3435,7 @@ def test_hist_step_horiz():
34513435
# make some data
34523436
d1=np.linspace(0,10,50)
34533437
d2=np.linspace(1,3,20)
3454-
fig=plt.figure()
3455-
ax=fig.add_subplot()
3438+
fig,ax=plt.subplots()
34563439
ax.hist((d1,d2),histtype="step",orientation="horizontal")
34573440

34583441

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

34703452

@@ -3521,8 +3503,7 @@ def test_hist_stacked_stepfilled_alpha():
35213503
# make some data
35223504
d1=np.linspace(1,3,20)
35233505
d2=np.linspace(0,10,50)
3524-
fig=plt.figure()
3525-
ax=fig.add_subplot()
3506+
fig,ax=plt.subplots()
35263507
ax.hist((d1,d2),histtype="stepfilled",stacked=True,alpha=0.5)
35273508

35283509

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

35383518

@@ -3549,8 +3529,7 @@ def test_hist_stacked_density():
35493529
deftest_hist_step_bottom():
35503530
# make some data
35513531
d1=np.linspace(1,3,20)
3552-
fig=plt.figure()
3553-
ax=fig.add_subplot()
3532+
fig,ax=plt.subplots()
35543533
ax.hist(d1,bottom=np.arange(10),histtype="stepfilled")
35553534

35563535

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

37113689

37123690
deftest_hist_emptydata():
3713-
fig=plt.figure()
3714-
ax=fig.add_subplot()
3691+
fig,ax=plt.subplots()
37153692
ax.hist([[],range(10),range(10)],histtype="step")
37163693

37173694

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

3738-
fig=plt.figure()
3739-
ax=fig.add_subplot()
3715+
fig,ax=plt.subplots()
37403716
ax.plot(data,'D',mfc='none',markersize=100)
37413717

37423718

@@ -3814,8 +3790,7 @@ def test_alpha():
38143790
np.random.seed(0)
38153791
data=np.random.random(50)
38163792

3817-
fig=plt.figure()
3818-
ax=fig.add_subplot()
3793+
fig,ax=plt.subplots()
38193794

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

39793954
@image_comparison(['marker_styles.png'],remove_text=True)
39803955
deftest_marker_styles():
3981-
fig=plt.figure()
3982-
ax=fig.add_subplot()
3956+
fig,ax=plt.subplots()
39833957
fory,markerinenumerate(sorted(matplotlib.markers.MarkerStyle.markers,
39843958
key=lambdax:str(type(x))+str(x))):
39853959
ax.plot((y%2)*5+np.arange(10)*10,np.ones(10)*10*y,linestyle='',
@@ -4001,8 +3975,7 @@ def test_vertex_markers():
40013975
data=list(range(10))
40023976
marker_as_tuple= ((-1,-1), (1,-1), (1,1), (-1,1))
40033977
marker_as_list= [(-1,-1), (1,-1), (1,1), (-1,1)]
4004-
fig=plt.figure()
4005-
ax=fig.add_subplot()
3978+
fig,ax=plt.subplots()
40063979
ax.plot(data,linestyle='',marker=marker_as_tuple,mfc='k')
40073980
ax.plot(data[::-1],linestyle='',marker=marker_as_list,mfc='b')
40083981
ax.set_xlim([-1,10])

‎lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ def test_use14corefonts():
4242
deftest_type42():
4343
rcParams['pdf.fonttype']=42
4444

45-
fig=plt.figure()
46-
ax=fig.add_subplot()
45+
fig,ax=plt.subplots()
4746
ax.plot([1,2,3])
4847
fig.savefig(io.BytesIO())
4948

5049

5150
deftest_multipage_pagecount():
5251
withPdfPages(io.BytesIO())aspdf:
5352
assertpdf.get_pagecount()==0
54-
fig=plt.figure()
55-
ax=fig.add_subplot()
53+
fig,ax=plt.subplots()
5654
ax.plot([1,2,3])
5755
fig.savefig(pdf,format="pdf")
5856
assertpdf.get_pagecount()==1
@@ -64,8 +62,7 @@ def test_multipage_properfinalize():
6462
pdfio=io.BytesIO()
6563
withPdfPages(pdfio)aspdf:
6664
foriinrange(10):
67-
fig=plt.figure()
68-
ax=fig.add_subplot()
65+
fig,ax=plt.subplots()
6966
ax.set_title('This is a long title')
7067
fig.savefig(pdf,format="pdf")
7168
pdfio.seek(0)
@@ -86,8 +83,7 @@ def test_multipage_keep_empty():
8683
pass
8784
assertnotos.path.exists(filename)
8885
# test pdf files with content, they should never be deleted
89-
fig=plt.figure()
90-
ax=fig.add_subplot()
86+
fig,ax=plt.subplots()
9187
ax.plot([1,2,3])
9288
# test that a non-empty pdf is left behind with keep_empty=True (default)
9389
withNamedTemporaryFile(delete=False)astmp:

‎lib/matplotlib/tests/test_bbox_tight.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def test_bbox_inches_tight_clipping():
8686
remove_text=True,savefig_kwarg={'bbox_inches':'tight'})
8787
deftest_bbox_inches_tight_raster():
8888
"""Test rasterization with tight_layout"""
89-
fig=plt.figure()
90-
ax=fig.add_subplot()
89+
fig,ax=plt.subplots()
9190
ax.plot([1.0,2.0],rasterized=True)
9291

9392

‎lib/matplotlib/tests/test_legend.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def test_legend_ordereddict():
3636
@image_comparison(['legend_auto1'],remove_text=True)
3737
deftest_legend_auto1():
3838
"""Test automatic legend placement"""
39-
fig=plt.figure()
40-
ax=fig.add_subplot()
39+
fig,ax=plt.subplots()
4140
x=np.arange(100)
4241
ax.plot(x,50-x,'o',label='y=1')
4342
ax.plot(x,x-50,'o',label='y=-1')
@@ -47,8 +46,7 @@ def test_legend_auto1():
4746
@image_comparison(['legend_auto2'],remove_text=True)
4847
deftest_legend_auto2():
4948
"""Test automatic legend placement"""
50-
fig=plt.figure()
51-
ax=fig.add_subplot()
49+
fig,ax=plt.subplots()
5250
x=np.arange(100)
5351
b1=ax.bar(x,x,align='edge',color='m')
5452
b2=ax.bar(x,x[::-1],align='edge',color='g')
@@ -58,8 +56,7 @@ def test_legend_auto2():
5856
@image_comparison(['legend_auto3'])
5957
deftest_legend_auto3():
6058
"""Test automatic legend placement"""
61-
fig=plt.figure()
62-
ax=fig.add_subplot()
59+
fig,ax=plt.subplots()
6360
x= [0.9,0.1,0.1,0.9,0.9,0.5]
6461
y= [0.95,0.95,0.05,0.05,0.5,0.5]
6562
ax.plot(x,y,'o-',label='line')
@@ -82,8 +79,7 @@ def test_various_labels():
8279
@image_comparison(['legend_labels_first.png'],remove_text=True)
8380
deftest_labels_first():
8481
# test labels to left of markers
85-
fig=plt.figure()
86-
ax=fig.add_subplot()
82+
fig,ax=plt.subplots()
8783
ax.plot(np.arange(10),'-o',label=1)
8884
ax.plot(np.ones(10)*5,':x',label="x")
8985
ax.plot(np.arange(20,10,-1),'d',label="diamond")
@@ -93,8 +89,7 @@ def test_labels_first():
9389
@image_comparison(['legend_multiple_keys.png'],remove_text=True)
9490
deftest_multiple_keys():
9591
# test legend entries with multiple keys
96-
fig=plt.figure()
97-
ax=fig.add_subplot()
92+
fig,ax=plt.subplots()
9893
p1,=ax.plot([1,2,3],'-o')
9994
p2,=ax.plot([2,3,4],'-x')
10095
p3,=ax.plot([3,4,5],'-d')
@@ -389,8 +384,7 @@ def test_warn_args_kwargs(self):
389384
deftest_legend_stackplot():
390385
"""Test legend for PolyCollection using stackplot."""
391386
# related to #1341, #1943, and PR #3303
392-
fig=plt.figure()
393-
ax=fig.add_subplot()
387+
fig,ax=plt.subplots()
394388
x=np.linspace(0,10,10)
395389
y1=1.0*x
396390
y2=2.0*x+1

‎lib/matplotlib/tests/test_patches.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def test_negative_rect():
8686

8787
@image_comparison(['clip_to_bbox'])
8888
deftest_clip_to_bbox():
89-
fig=plt.figure()
90-
91-
ax=fig.add_subplot()
89+
fig,ax=plt.subplots()
9290
ax.set_xlim([-18,20])
9391
ax.set_ylim([-150,100])
9492

‎lib/matplotlib/tests/test_text.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def find_matplotlib_font(**kw):
3434
UserWarning,
3535
module='matplotlib.font_manager')
3636

37-
plt.figure()
38-
ax=plt.subplot(1,1,1)
37+
fig,ax=plt.subplots()
3938

4039
normalFont=find_matplotlib_font(
4140
family="sans-serif",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp