|
| 1 | + |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from matplotlib.colors import same_color |
| 5 | +from matplotlib.testing.decorators import image_comparison |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +from mpl_toolkits.mplot3d import art3d |
| 8 | + |
| 9 | + |
| 10 | +@image_comparison(['legend_plot.png'], remove_text=True) |
| 11 | +def test_legend_plot(): |
| 12 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 13 | + x = np.arange(10) |
| 14 | + ax.plot(x, 5 - x, 'o', zdir='y', label='z=1') |
| 15 | + ax.plot(x, x - 5, 'o', zdir='y', label='z=-1') |
| 16 | + ax.legend() |
| 17 | + |
| 18 | + |
| 19 | +@image_comparison(['legend_bar.png'], remove_text=True) |
| 20 | +def test_legend_bar(): |
| 21 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 22 | + x = np.arange(10) |
| 23 | + b1 = ax.bar(x, x, zdir='y', align='edge', color='m') |
| 24 | + b2 = ax.bar(x, x[::-1], zdir='x', align='edge', color='g') |
| 25 | + ax.legend([b1[0], b2[0]], ['up', 'down']) |
| 26 | + |
| 27 | + |
| 28 | +@image_comparison(['fancy.png'], remove_text=True) |
| 29 | +def test_fancy(): |
| 30 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 31 | + ax.plot(np.arange(10), np.full(10, 5), np.full(10, 5), 'o--', label='line') |
| 32 | + ax.scatter(np.arange(10), np.arange(10, 0, -1), label='scatter') |
| 33 | + ax.errorbar(np.full(10, 5), np.arange(10), np.full(10, 10), |
| 34 | + xerr=0.5, zerr=0.5, label='errorbar') |
| 35 | + ax.legend(loc='lower left', ncols=2, title='My legend', numpoints=1) |
| 36 | + |
| 37 | + |
| 38 | +def test_linecollection_scaled_dashes(): |
| 39 | + lines1 = [[(0, .5), (.5, 1)], [(.3, .6), (.2, .2)]] |
| 40 | + lines2 = [[[0.7, .2], [.8, .4]], [[.5, .7], [.6, .1]]] |
| 41 | + lines3 = [[[0.6, .2], [.8, .4]], [[.5, .7], [.1, .1]]] |
| 42 | + lc1 = art3d.Line3DCollection(lines1, linestyles="--", lw=3) |
| 43 | + lc2 = art3d.Line3DCollection(lines2, linestyles="-.") |
| 44 | + lc3 = art3d.Line3DCollection(lines3, linestyles=":", lw=.5) |
| 45 | + |
| 46 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 47 | + ax.add_collection(lc1) |
| 48 | + ax.add_collection(lc2) |
| 49 | + ax.add_collection(lc3) |
| 50 | + |
| 51 | + leg = ax.legend([lc1, lc2, lc3], ['line1', 'line2', 'line 3']) |
| 52 | + h1, h2, h3 = leg.legendHandles |
| 53 | + |
| 54 | + for oh, lh in zip((lc1, lc2, lc3), (h1, h2, h3)): |
| 55 | + assert oh.get_linestyles()[0] == lh._dash_pattern |
| 56 | + |
| 57 | + |
| 58 | +def test_handlerline3d(): |
| 59 | + # Test marker consistency for monolithic Line3D legend handler. |
| 60 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 61 | + ax.scatter([0, 1], [0, 1], marker="v") |
| 62 | + handles = [art3d.Line3D([0], [0], [0], marker="v")] |
| 63 | + leg = ax.legend(handles, ["Aardvark"], numpoints=1) |
| 64 | + assert handles[0].get_marker() == leg.legendHandles[0].get_marker() |
| 65 | + |
| 66 | + |
| 67 | +def test_contour_legend_elements(): |
| 68 | + from matplotlib.collections import LineCollection |
| 69 | + x, y = np.mgrid[1:10, 1:10] |
| 70 | + h = x * y |
| 71 | + colors = ['blue', '#00FF00', 'red'] |
| 72 | + |
| 73 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 74 | + cs = ax.contour(x, y, h, levels=[10, 30, 50], colors=colors, extend='both') |
| 75 | + |
| 76 | + artists, labels = cs.legend_elements() |
| 77 | + assert labels == ['$x = 10.0$', '$x = 30.0$', '$x = 50.0$'] |
| 78 | + assert all(isinstance(a, LineCollection) for a in artists) |
| 79 | + assert all(same_color(a.get_color(), c) |
| 80 | + for a, c in zip(artists, colors)) |
| 81 | + |
| 82 | + |
| 83 | +def test_contourf_legend_elements(): |
| 84 | + from matplotlib.patches import Rectangle |
| 85 | + x, y = np.mgrid[1:10, 1:10] |
| 86 | + h = x * y |
| 87 | + |
| 88 | + fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) |
| 89 | + cs = ax.contourf(x, y, h, levels=[10, 30, 50], |
| 90 | + colors=['#FFFF00', '#FF00FF', '#00FFFF'], |
| 91 | + extend='both') |
| 92 | + cs.cmap.set_over('red') |
| 93 | + cs.cmap.set_under('blue') |
| 94 | + cs.changed() |
| 95 | + artists, labels = cs.legend_elements() |
| 96 | + assert labels == ['$x \\leq -1e+250s$', |
| 97 | + '$10.0 < x \\leq 30.0$', |
| 98 | + '$30.0 < x \\leq 50.0$', |
| 99 | + '$x > 1e+250s$'] |
| 100 | + expected_colors = ('blue', '#FFFF00', '#FF00FF', 'red') |
| 101 | + assert all(isinstance(a, Rectangle) for a in artists) |
| 102 | + assert all(same_color(a.get_facecolor(), c) |
| 103 | + for a, c in zip(artists, expected_colors)) |
0 commit comments