test_legend3d.py 4.2 KB

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