MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / test_boxplot_legend_labels

Function test_boxplot_legend_labels

lib/matplotlib/tests/test_legend.py:1649–1678  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1647
1648
1649def test_boxplot_legend_labels():
1650 # Test that legend entries are generated when passing `label`.
1651 np.random.seed(19680801)
1652 data = np.random.random((10, 4))
1653 fig, axs = plt.subplots(nrows=1, ncols=4)
1654 legend_labels = ['box A', 'box B', 'box C', 'box D']
1655
1656 # Testing legend labels and patch passed to legend.
1657 bp1 = axs[0].boxplot(data, patch_artist=True, label=legend_labels)
1658 assert [v.get_label() for v in bp1['boxes']] == legend_labels
1659 handles, labels = axs[0].get_legend_handles_labels()
1660 assert labels == legend_labels
1661 assert all(isinstance(h, mpl.patches.PathPatch) for h in handles)
1662
1663 # Testing legend without `box`.
1664 bp2 = axs[1].boxplot(data, label=legend_labels, showbox=False)
1665 # Without a box, The legend entries should be passed from the medians.
1666 assert [v.get_label() for v in bp2['medians']] == legend_labels
1667 handles, labels = axs[1].get_legend_handles_labels()
1668 assert labels == legend_labels
1669 assert all(isinstance(h, mpl.lines.Line2D) for h in handles)
1670
1671 # Testing legend with number of labels different from number of boxes.
1672 with pytest.raises(ValueError, match='values must have same the length'):
1673 bp3 = axs[2].boxplot(data, label=legend_labels[:-1])
1674
1675 # Test that for a string label, only the first box gets a label.
1676 bp4 = axs[3].boxplot(data, label='box A')
1677 assert bp4['medians'][0].get_label() == 'box A'
1678 assert all(x.get_label().startswith("_") for x in bp4['medians'][1:])
1679
1680
1681def test_legend_linewidth():

Callers

nothing calls this directly

Calls 4

boxplotMethod · 0.80
subplotsMethod · 0.45
get_labelMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…