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

Commit414d4f8

Browse files
committed
Add tests
1 parent1b63df8 commit414d4f8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎lib/matplotlib/tests/test_legend.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
frommatplotlib.testing.decoratorsimportimage_comparison
1212
importmatplotlib.pyplotasplt
1313
importmatplotlibasmpl
14+
importmatplotlib.patchesasmpatches
1415
importmatplotlib.transformsasmtransforms
1516
importmatplotlib.collectionsasmcollections
1617
frommatplotlib.legend_handlerimportHandlerTuple
18+
frommatplotlib.legendimportLegend
19+
fromnumpy.testingimportassert_allclose
1720

1821

1922
@image_comparison(baseline_images=['legend_auto1'],remove_text=True)
@@ -51,6 +54,58 @@ def test_legend_auto3():
5154
ax.legend(loc=0)
5255

5356

57+
deftest_legend_auto4():
58+
"""Check that the legend location with automatic placement is the same,
59+
whatever the histogram type is. Related to issue #9580.
60+
"""
61+
# NB: barstacked is pointless with a single dataset.
62+
fig,axs=plt.subplots(ncols=3,figsize=(6.4,2.4))
63+
leg_bboxes= []
64+
forax,htinzip(axs.flat, ('bar','step','stepfilled')):
65+
ax.set_title(ht)
66+
# A high bar on the left but an even higher one on the right.
67+
ax.hist([0]+5*[9],bins=range(10),label="Legend",histtype=ht)
68+
leg=ax.legend(loc="best")
69+
fig.canvas.draw()
70+
leg_bboxes.append(
71+
leg.get_window_extent().inverse_transformed(ax.transAxes))
72+
73+
# The histogram type "bar" is assumed to be the correct reference.
74+
assert_allclose(leg_bboxes[1].bounds,leg_bboxes[0].bounds)
75+
assert_allclose(leg_bboxes[2].bounds,leg_bboxes[0].bounds)
76+
77+
78+
deftest_legend_auto5():
79+
"""Check that the automatic placement handle a rather complex
80+
case with non rectangular patch. Related to issue #9580.
81+
"""
82+
fig,axs=plt.subplots(ncols=2,figsize=(9.6,4.8))
83+
84+
leg_bboxes= []
85+
forax,locinzip(axs.flat, ("center","best")):
86+
# An Ellipse patch at the top, a U-shaped Polygon patch at the
87+
# bottom and a ring-like Wedge patch: the correct placement of
88+
# the legend should be in the center.
89+
for_patchin [
90+
mpatches.Ellipse(
91+
xy=(0.5,0.9),width=0.8,height=0.2,fc="C1"),
92+
mpatches.Polygon(np.array([
93+
[0,1], [0,0], [1,0], [1,1], [0.9,1.0], [0.9,0.1],
94+
[0.1,0.1], [0.1,1.0], [0.1,1.0]]),fc="C1"),
95+
mpatches.Wedge((0.5,0.5),0.5,0,360,width=0.05,fc="C0")
96+
]:
97+
ax.add_patch(_patch)
98+
99+
ax.plot([0.1,0.9], [0.9,0.9],label="A segment")# sthg to label
100+
101+
leg=ax.legend(loc=loc)
102+
fig.canvas.draw()
103+
leg_bboxes.append(
104+
leg.get_window_extent().inverse_transformed(ax.transAxes))
105+
106+
assert_allclose(leg_bboxes[1].bounds,leg_bboxes[0].bounds)
107+
108+
54109
@image_comparison(baseline_images=['legend_various_labels'],remove_text=True)
55110
deftest_various_labels():
56111
# tests all sorts of label types

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp