圆,楔和多边形

此示例演示如何使用修补程序集合。

  1. import numpy as np
  2. from matplotlib.patches import Circle, Wedge, Polygon
  3. from matplotlib.collections import PatchCollection
  4. import matplotlib.pyplot as plt
  5. # Fixing random state for reproducibility
  6. np.random.seed(19680801)
  7. fig, ax = plt.subplots()
  8. resolution = 50 # the number of vertices
  9. N = 3
  10. x = np.random.rand(N)
  11. y = np.random.rand(N)
  12. radii = 0.1*np.random.rand(N)
  13. patches = []
  14. for x1, y1, r in zip(x, y, radii):
  15. circle = Circle((x1, y1), r)
  16. patches.append(circle)
  17. x = np.random.rand(N)
  18. y = np.random.rand(N)
  19. radii = 0.1*np.random.rand(N)
  20. theta1 = 360.0*np.random.rand(N)
  21. theta2 = 360.0*np.random.rand(N)
  22. for x1, y1, r, t1, t2 in zip(x, y, radii, theta1, theta2):
  23. wedge = Wedge((x1, y1), r, t1, t2)
  24. patches.append(wedge)
  25. # Some limiting conditions on Wedge
  26. patches += [
  27. Wedge((.3, .7), .1, 0, 360), # Full circle
  28. Wedge((.7, .8), .2, 0, 360, width=0.05), # Full ring
  29. Wedge((.8, .3), .2, 0, 45), # Full sector
  30. Wedge((.8, .3), .2, 45, 90, width=0.10), # Ring sector
  31. ]
  32. for i in range(N):
  33. polygon = Polygon(np.random.rand(N, 2), True)
  34. patches.append(polygon)
  35. colors = 100*np.random.rand(len(patches))
  36. p = PatchCollection(patches, alpha=0.4)
  37. p.set_array(np.array(colors))
  38. ax.add_collection(p)
  39. fig.colorbar(p, ax=ax)
  40. plt.show()

圆,楔和多边形示例

参考

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

  1. import matplotlib
  2. matplotlib.patches
  3. matplotlib.patches.Circle
  4. matplotlib.patches.Wedge
  5. matplotlib.patches.Polygon
  6. matplotlib.collections.PatchCollection
  7. matplotlib.collections.Collection.set_array
  8. matplotlib.axes.Axes.add_collection
  9. matplotlib.figure.Figure.colorbar

下载这个示例