具有自动缩放功能的Line,Poly和RegularPoly Collection

对于前两个子图,我们将使用螺旋。它们的大小将以图表单位设置,而不是数据单位。它们的位置将通过使用LineCollection和PolyCollection的“偏移”和“transOffset”kwargs以数据单位设置。

第三个子图将生成正多边形,具有与前两个相同类型的缩放和定位。

最后一个子图说明了使用 “offsets =(xo,yo)”,即单个元组而不是元组列表来生成连续的偏移曲线,其中偏移量以数据单位给出。 此行为仅适用于LineCollection。

  1. import matplotlib.pyplot as plt
  2. from matplotlib import collections, colors, transforms
  3. import numpy as np
  4. nverts = 50
  5. npts = 100
  6. # Make some spirals
  7. r = np.arange(nverts)
  8. theta = np.linspace(0, 2*np.pi, nverts)
  9. xx = r * np.sin(theta)
  10. yy = r * np.cos(theta)
  11. spiral = np.column_stack([xx, yy])
  12. # Fixing random state for reproducibility
  13. rs = np.random.RandomState(19680801)
  14. # Make some offsets
  15. xyo = rs.randn(npts, 2)
  16. # Make a list of colors cycling through the default series.
  17. colors = [colors.to_rgba(c)
  18. for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
  19. fig, axes = plt.subplots(2, 2)
  20. fig.subplots_adjust(top=0.92, left=0.07, right=0.97,
  21. hspace=0.3, wspace=0.3)
  22. ((ax1, ax2), (ax3, ax4)) = axes # unpack the axes
  23. col = collections.LineCollection([spiral], offsets=xyo,
  24. transOffset=ax1.transData)
  25. trans = fig.dpi_scale_trans + transforms.Affine2D().scale(1.0/72.0)
  26. col.set_transform(trans) # the points to pixels transform
  27. # Note: the first argument to the collection initializer
  28. # must be a list of sequences of x,y tuples; we have only
  29. # one sequence, but we still have to put it in a list.
  30. ax1.add_collection(col, autolim=True)
  31. # autolim=True enables autoscaling. For collections with
  32. # offsets like this, it is neither efficient nor accurate,
  33. # but it is good enough to generate a plot that you can use
  34. # as a starting point. If you know beforehand the range of
  35. # x and y that you want to show, it is better to set them
  36. # explicitly, leave out the autolim kwarg (or set it to False),
  37. # and omit the 'ax1.autoscale_view()' call below.
  38. # Make a transform for the line segments such that their size is
  39. # given in points:
  40. col.set_color(colors)
  41. ax1.autoscale_view() # See comment above, after ax1.add_collection.
  42. ax1.set_title('LineCollection using offsets')
  43. # The same data as above, but fill the curves.
  44. col = collections.PolyCollection([spiral], offsets=xyo,
  45. transOffset=ax2.transData)
  46. trans = transforms.Affine2D().scale(fig.dpi/72.0)
  47. col.set_transform(trans) # the points to pixels transform
  48. ax2.add_collection(col, autolim=True)
  49. col.set_color(colors)
  50. ax2.autoscale_view()
  51. ax2.set_title('PolyCollection using offsets')
  52. # 7-sided regular polygons
  53. col = collections.RegularPolyCollection(
  54. 7, sizes=np.abs(xx) * 10.0, offsets=xyo, transOffset=ax3.transData)
  55. trans = transforms.Affine2D().scale(fig.dpi / 72.0)
  56. col.set_transform(trans) # the points to pixels transform
  57. ax3.add_collection(col, autolim=True)
  58. col.set_color(colors)
  59. ax3.autoscale_view()
  60. ax3.set_title('RegularPolyCollection using offsets')
  61. # Simulate a series of ocean current profiles, successively
  62. # offset by 0.1 m/s so that they form what is sometimes called
  63. # a "waterfall" plot or a "stagger" plot.
  64. nverts = 60
  65. ncurves = 20
  66. offs = (0.1, 0.0)
  67. yy = np.linspace(0, 2*np.pi, nverts)
  68. ym = np.max(yy)
  69. xx = (0.2 + (ym - yy) / ym) ** 2 * np.cos(yy - 0.4) * 0.5
  70. segs = []
  71. for i in range(ncurves):
  72. xxx = xx + 0.02*rs.randn(nverts)
  73. curve = np.column_stack([xxx, yy * 100])
  74. segs.append(curve)
  75. col = collections.LineCollection(segs, offsets=offs)
  76. ax4.add_collection(col, autolim=True)
  77. col.set_color(colors)
  78. ax4.autoscale_view()
  79. ax4.set_title('Successive data offsets')
  80. ax4.set_xlabel('Zonal velocity component (m/s)')
  81. ax4.set_ylabel('Depth (m)')
  82. # Reverse the y-axis so depth increases downward
  83. ax4.set_ylim(ax4.get_ylim()[::-1])
  84. plt.show()

缩放功能示例

参考

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

  1. import matplotlib
  2. matplotlib.figure.Figure
  3. matplotlib.collections
  4. matplotlib.collections.LineCollection
  5. matplotlib.collections.RegularPolyCollection
  6. matplotlib.axes.Axes.add_collection
  7. matplotlib.axes.Axes.autoscale_view
  8. matplotlib.transforms.Affine2D
  9. matplotlib.transforms.Affine2D.scale

下载这个示例