matplotlib艺术家对象的参考

此示例显示使用matplotlib API绘制的几个matplotlib的图形基元(艺术家)。艺术家对象API提供完整的艺术家列表和文档。

Copyright (c) 2010, Bartosz Telenczuk BSD License

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.path as mpath
  4. import matplotlib.lines as mlines
  5. import matplotlib.patches as mpatches
  6. from matplotlib.collections import PatchCollection
  7. def label(xy, text):
  8. y = xy[1] - 0.15 # shift y-value for label so that it's below the artist
  9. plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14)
  10. fig, ax = plt.subplots()
  11. # create 3x3 grid to plot the artists
  12. grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T
  13. patches = []
  14. # add a circle
  15. circle = mpatches.Circle(grid[0], 0.1, ec="none")
  16. patches.append(circle)
  17. label(grid[0], "Circle")
  18. # add a rectangle
  19. rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")
  20. patches.append(rect)
  21. label(grid[1], "Rectangle")
  22. # add a wedge
  23. wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none")
  24. patches.append(wedge)
  25. label(grid[2], "Wedge")
  26. # add a Polygon
  27. polygon = mpatches.RegularPolygon(grid[3], 5, 0.1)
  28. patches.append(polygon)
  29. label(grid[3], "Polygon")
  30. # add an ellipse
  31. ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)
  32. patches.append(ellipse)
  33. label(grid[4], "Ellipse")
  34. # add an arrow
  35. arrow = mpatches.Arrow(grid[5, 0] - 0.05, grid[5, 1] - 0.05, 0.1, 0.1,
  36. width=0.1)
  37. patches.append(arrow)
  38. label(grid[5], "Arrow")
  39. # add a path patch
  40. Path = mpath.Path
  41. path_data = [
  42. (Path.MOVETO, [0.018, -0.11]),
  43. (Path.CURVE4, [-0.031, -0.051]),
  44. (Path.CURVE4, [-0.115, 0.073]),
  45. (Path.CURVE4, [-0.03, 0.073]),
  46. (Path.LINETO, [-0.011, 0.039]),
  47. (Path.CURVE4, [0.043, 0.121]),
  48. (Path.CURVE4, [0.075, -0.005]),
  49. (Path.CURVE4, [0.035, -0.027]),
  50. (Path.CLOSEPOLY, [0.018, -0.11])]
  51. codes, verts = zip(*path_data)
  52. path = mpath.Path(verts + grid[6], codes)
  53. patch = mpatches.PathPatch(path)
  54. patches.append(patch)
  55. label(grid[6], "PathPatch")
  56. # add a fancy box
  57. fancybox = mpatches.FancyBboxPatch(
  58. grid[7] - [0.025, 0.05], 0.05, 0.1,
  59. boxstyle=mpatches.BoxStyle("Round", pad=0.02))
  60. patches.append(fancybox)
  61. label(grid[7], "FancyBboxPatch")
  62. # add a line
  63. x, y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]])
  64. line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)
  65. label(grid[8], "Line2D")
  66. colors = np.linspace(0, 1, len(patches))
  67. collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)
  68. collection.set_array(np.array(colors))
  69. ax.add_collection(collection)
  70. ax.add_line(line)
  71. plt.axis('equal')
  72. plt.axis('off')
  73. plt.tight_layout()
  74. plt.show()

matplotlib艺术家对象的参考示例

参考

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

  1. import matplotlib
  2. matplotlib.path
  3. matplotlib.path.Path
  4. matplotlib.lines
  5. matplotlib.lines.Line2D
  6. matplotlib.patches
  7. matplotlib.patches.Circle
  8. matplotlib.patches.Ellipse
  9. matplotlib.patches.Wedge
  10. matplotlib.patches.Rectangle
  11. matplotlib.patches.Arrow
  12. matplotlib.patches.PathPatch
  13. matplotlib.patches.FancyBboxPatch
  14. matplotlib.patches.RegularPolygon
  15. matplotlib.collections
  16. matplotlib.collections.PatchCollection
  17. matplotlib.cm.ScalarMappable.set_array
  18. matplotlib.axes.Axes.add_collection
  19. matplotlib.axes.Axes.add_line

下载这个示例