|
11 | 11 | frommatplotlib.testing.decoratorsimportimage_comparison
|
12 | 12 | importmatplotlib.pyplotasplt
|
13 | 13 | importmatplotlibasmpl
|
| 14 | +importmatplotlib.patchesasmpatches |
14 | 15 | importmatplotlib.transformsasmtransforms
|
15 | 16 | importmatplotlib.collectionsasmcollections
|
16 | 17 | frommatplotlib.legend_handlerimportHandlerTuple
|
| 18 | +frommatplotlib.legendimportLegend |
| 19 | +fromnumpy.testingimportassert_allclose |
17 | 20 |
|
18 | 21 |
|
19 | 22 | @image_comparison(baseline_images=['legend_auto1'],remove_text=True)
|
@@ -51,6 +54,58 @@ def test_legend_auto3():
|
51 | 54 | ax.legend(loc=0)
|
52 | 55 |
|
53 | 56 |
|
| 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 | + |
54 | 109 | @image_comparison(baseline_images=['legend_various_labels'],remove_text=True)
|
55 | 110 | deftest_various_labels():
|
56 | 111 | # tests all sorts of label types
|
|