绘制海豚

此示例显示如何使用PathPathPatchtransforms类绘制和操作给定顶点和节点的形状。

  1. import matplotlib.cm as cm
  2. import matplotlib.pyplot as plt
  3. from matplotlib.patches import Circle, PathPatch
  4. from matplotlib.path import Path
  5. from matplotlib.transforms import Affine2D
  6. import numpy as np
  7. # Fixing random state for reproducibility
  8. np.random.seed(19680801)
  9. r = np.random.rand(50)
  10. t = np.random.rand(50) * np.pi * 2.0
  11. x = r * np.cos(t)
  12. y = r * np.sin(t)
  13. fig, ax = plt.subplots(figsize=(6, 6))
  14. circle = Circle((0, 0), 1, facecolor='none',
  15. edgecolor=(0, 0.8, 0.8), linewidth=3, alpha=0.5)
  16. ax.add_patch(circle)
  17. im = plt.imshow(np.random.random((100, 100)),
  18. origin='lower', cmap=cm.winter,
  19. interpolation='spline36',
  20. extent=([-1, 1, -1, 1]))
  21. im.set_clip_path(circle)
  22. plt.plot(x, y, 'o', color=(0.9, 0.9, 1.0), alpha=0.8)
  23. # Dolphin from OpenClipart library by Andy Fitzsimon
  24. # <cc:License rdf:about="http://web.resource.org/cc/PublicDomain">
  25. # <cc:permits rdf:resource="http://web.resource.org/cc/Reproduction"/>
  26. # <cc:permits rdf:resource="http://web.resource.org/cc/Distribution"/>
  27. # <cc:permits rdf:resource="http://web.resource.org/cc/DerivativeWorks"/>
  28. # </cc:License>
  29. dolphin = """
  30. M -0.59739425,160.18173 C -0.62740401,160.18885 -0.57867129,160.11183
  31. -0.57867129,160.11183 C -0.57867129,160.11183 -0.5438361,159.89315
  32. -0.39514638,159.81496 C -0.24645668,159.73678 -0.18316813,159.71981
  33. -0.18316813,159.71981 C -0.18316813,159.71981 -0.10322971,159.58124
  34. -0.057804323,159.58725 C -0.029723983,159.58913 -0.061841603,159.60356
  35. -0.071265813,159.62815 C -0.080250183,159.65325 -0.082918513,159.70554
  36. -0.061841203,159.71248 C -0.040763903,159.7194 -0.0066711426,159.71091
  37. 0.077336307,159.73612 C 0.16879567,159.76377 0.28380306,159.86448
  38. 0.31516668,159.91533 C 0.3465303,159.96618 0.5011127,160.1771
  39. 0.5011127,160.1771 C 0.63668998,160.19238 0.67763022,160.31259
  40. 0.66556395,160.32668 C 0.65339985,160.34212 0.66350443,160.33642
  41. 0.64907098,160.33088 C 0.63463742,160.32533 0.61309688,160.297
  42. 0.5789627,160.29339 C 0.54348657,160.28968 0.52329693,160.27674
  43. 0.50728856,160.27737 C 0.49060916,160.27795 0.48965803,160.31565
  44. 0.46114204,160.33673 C 0.43329696,160.35786 0.4570711,160.39871
  45. 0.43309565,160.40685 C 0.4105108,160.41442 0.39416631,160.33027
  46. 0.3954995,160.2935 C 0.39683269,160.25672 0.43807996,160.21522
  47. 0.44567915,160.19734 C 0.45327833,160.17946 0.27946869,159.9424
  48. -0.061852613,159.99845 C -0.083965233,160.0427 -0.26176109,160.06683
  49. -0.26176109,160.06683 C -0.30127962,160.07028 -0.21167141,160.09731
  50. -0.24649368,160.1011 C -0.32642366,160.11569 -0.34521187,160.06895
  51. -0.40622293,160.0819 C -0.467234,160.09485 -0.56738444,160.17461
  52. -0.59739425,160.18173
  53. """
  54. vertices = []
  55. codes = []
  56. parts = dolphin.split()
  57. i = 0
  58. code_map = {
  59. 'M': (Path.MOVETO, 1),
  60. 'C': (Path.CURVE4, 3),
  61. 'L': (Path.LINETO, 1)}
  62. while i < len(parts):
  63. code = parts[i]
  64. path_code, npoints = code_map[code]
  65. codes.extend([path_code] * npoints)
  66. vertices.extend([[float(x) for x in y.split(',')] for y in
  67. parts[i + 1:i + npoints + 1]])
  68. i += npoints + 1
  69. vertices = np.array(vertices, float)
  70. vertices[:, 1] -= 160
  71. dolphin_path = Path(vertices, codes)
  72. dolphin_patch = PathPatch(dolphin_path, facecolor=(0.6, 0.6, 0.6),
  73. edgecolor=(0.0, 0.0, 0.0))
  74. ax.add_patch(dolphin_patch)
  75. vertices = Affine2D().rotate_deg(60).transform(vertices)
  76. dolphin_path2 = Path(vertices, codes)
  77. dolphin_patch2 = PathPatch(dolphin_path2, facecolor=(0.5, 0.5, 0.5),
  78. edgecolor=(0.0, 0.0, 0.0))
  79. ax.add_patch(dolphin_patch2)
  80. plt.show()

绘制海豚示例

参考

此示例中显示了以下函数,方法,类和模块的使用:

  1. import matplotlib
  2. matplotlib.path
  3. matplotlib.path.Path
  4. matplotlib.patches
  5. matplotlib.patches.PathPatch
  6. matplotlib.patches.Circle
  7. matplotlib.axes.Axes.add_patch
  8. matplotlib.transforms
  9. matplotlib.transforms.Affine2D
  10. matplotlib.transforms.Affine2D.rotate_deg

下载这个示例