|
10 | 10 | from matplotlib.testing._markers import needs_usetex
|
11 | 11 | import matplotlib.pyplot as plt
|
12 | 12 | import matplotlib as mpl
|
| 13 | +import matplotlib.patches as mpatches |
13 | 14 | import matplotlib.transforms as mtransforms
|
14 | 15 | import matplotlib.collections as mcollections
|
15 | 16 | import matplotlib.lines as mlines
|
16 | 17 | from matplotlib.legend_handler import HandlerTuple
|
17 | 18 | import matplotlib.legend as mlegend
|
18 | 19 | from matplotlib import rc_context
|
19 | 20 | from matplotlib.font_manager import FontProperties
|
| 21 | +from numpy.testing import assert_allclose |
20 | 22 |
|
21 | 23 |
|
22 | 24 | def test_legend_ordereddict():
|
@@ -69,6 +71,60 @@ def test_legend_auto3():
|
69 | 71 | ax.legend(loc='best')
|
70 | 72 |
|
71 | 73 |
|
| 74 | +def test_legend_auto4(): |
| 75 | + """ |
| 76 | + Check that the legend location with automatic placement is the same, |
| 77 | + whatever the histogram type is. Related to issue #9580. |
| 78 | + """ |
| 79 | + # NB: barstacked is pointless with a single dataset. |
| 80 | + fig, axs = plt.subplots(ncols=3, figsize=(6.4, 2.4)) |
| 81 | + leg_bboxes = [] |
| 82 | + for ax, ht in zip(axs.flat, ('bar', 'step', 'stepfilled')): |
| 83 | + ax.set_title(ht) |
| 84 | + # A high bar on the left but an even higher one on the right. |
| 85 | + ax.hist([0] + 5*[9], bins=range(10), label="Legend", histtype=ht) |
| 86 | + leg = ax.legend(loc="best") |
| 87 | + fig.canvas.draw() |
| 88 | + leg_bboxes.append( |
| 89 | + leg.get_window_extent().transformed(ax.transAxes.inverted())) |
| 90 | + |
| 91 | + # The histogram type "bar" is assumed to be the correct reference. |
| 92 | + assert_allclose(leg_bboxes[1].bounds, leg_bboxes[0].bounds) |
| 93 | + assert_allclose(leg_bboxes[2].bounds, leg_bboxes[0].bounds) |
| 94 | + |
| 95 | + |
| 96 | +def test_legend_auto5(): |
| 97 | + """ |
| 98 | + Check that the automatic placement handle a rather complex |
| 99 | + case with non rectangular patch. Related to issue #9580. |
| 100 | + """ |
| 101 | + fig, axs = plt.subplots(ncols=2, figsize=(9.6, 4.8)) |
| 102 | + |
| 103 | + leg_bboxes = [] |
| 104 | + for ax, loc in zip(axs.flat, ("center", "best")): |
| 105 | + # An Ellipse patch at the top, a U-shaped Polygon patch at the |
| 106 | + # bottom and a ring-like Wedge patch: the correct placement of |
| 107 | + # the legend should be in the center. |
| 108 | + for _patch in [ |
| 109 | + mpatches.Ellipse( |
| 110 | + xy=(0.5, 0.9), width=0.8, height=0.2, fc="C1"), |
| 111 | + mpatches.Polygon(np.array([ |
| 112 | + [0, 1], [0, 0], [1, 0], [1, 1], [0.9, 1.0], [0.9, 0.1], |
| 113 | + [0.1, 0.1], [0.1, 1.0], [0.1, 1.0]]), fc="C1"), |
| 114 | + mpatches.Wedge((0.5, 0.5), 0.5, 0, 360, width=0.05, fc="C0") |
| 115 | + ]: |
| 116 | + ax.add_patch(_patch) |
| 117 | + |
| 118 | + ax.plot([0.1, 0.9], [0.9, 0.9], label="A segment") # sthg to label |
| 119 | + |
| 120 | + leg = ax.legend(loc=loc) |
| 121 | + fig.canvas.draw() |
| 122 | + leg_bboxes.append( |
| 123 | + leg.get_window_extent().transformed(ax.transAxes.inverted())) |
| 124 | + |
| 125 | + assert_allclose(leg_bboxes[1].bounds, leg_bboxes[0].bounds) |
| 126 | + |
| 127 | + |
72 | 128 | @image_comparison(['legend_various_labels'], remove_text=True)
|
73 | 129 | def test_various_labels():
|
74 | 130 | # tests all sorts of label types
|
|
0 commit comments