线段集合

使用Matplotlib绘制线条。

LineCollection 允许在图上绘制多条线。 下面我们展示它的一些属性。

  1. import matplotlib.pyplot as plt
  2. from matplotlib.collections import LineCollection
  3. from matplotlib import colors as mcolors
  4. import numpy as np
  5. # In order to efficiently plot many lines in a single set of axes,
  6. # Matplotlib has the ability to add the lines all at once. Here is a
  7. # simple example showing how it is done.
  8. x = np.arange(100)
  9. # Here are many sets of y to plot vs x
  10. ys = x[:50, np.newaxis] + x[np.newaxis, :]
  11. segs = np.zeros((50, 100, 2))
  12. segs[:, :, 1] = ys
  13. segs[:, :, 0] = x
  14. # Mask some values to test masked array support:
  15. segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)
  16. # We need to set the plot limits.
  17. fig, ax = plt.subplots()
  18. ax.set_xlim(x.min(), x.max())
  19. ax.set_ylim(ys.min(), ys.max())
  20. # colors is sequence of rgba tuples
  21. # linestyle is a string or dash tuple. Legal string values are
  22. # solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq)
  23. # where onoffseq is an even length tuple of on and off ink in points.
  24. # If linestyle is omitted, 'solid' is used
  25. # See :class:`matplotlib.collections.LineCollection` for more information
  26. colors = [mcolors.to_rgba(c)
  27. for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
  28. line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),
  29. colors=colors, linestyle='solid')
  30. ax.add_collection(line_segments)
  31. ax.set_title('Line collection with masked arrays')
  32. plt.show()

线段集合示例

为了在一组轴中有效地绘制多条线,Matplotlib能够一次性添加所有线。 这是一个简单的例子,展示了它是如何完成的。

  1. N = 50
  2. x = np.arange(N)
  3. # Here are many sets of y to plot vs x
  4. ys = [x + i for i in x]
  5. # We need to set the plot limits, they will not autoscale
  6. fig, ax = plt.subplots()
  7. ax.set_xlim(np.min(x), np.max(x))
  8. ax.set_ylim(np.min(ys), np.max(ys))
  9. # colors is sequence of rgba tuples
  10. # linestyle is a string or dash tuple. Legal string values are
  11. # solid|dashed|dashdot|dotted. The dash tuple is (offset, onoffseq)
  12. # where onoffseq is an even length tuple of on and off ink in points.
  13. # If linestyle is omitted, 'solid' is used
  14. # See :class:`matplotlib.collections.LineCollection` for more information
  15. # Make a sequence of x,y pairs
  16. line_segments = LineCollection([np.column_stack([x, y]) for y in ys],
  17. linewidths=(0.5, 1, 1.5, 2),
  18. linestyles='solid')
  19. line_segments.set_array(x)
  20. ax.add_collection(line_segments)
  21. axcb = fig.colorbar(line_segments)
  22. axcb.set_label('Line Number')
  23. ax.set_title('Line Collection with mapped colors')
  24. plt.sci(line_segments) # This allows interactive changing of the colormap.
  25. plt.show()

线段集合示例2

参考

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

  1. import matplotlib
  2. matplotlib.collections
  3. matplotlib.collections.LineCollection
  4. matplotlib.cm.ScalarMappable.set_array
  5. matplotlib.axes.Axes.add_collection
  6. matplotlib.figure.Figure.colorbar
  7. matplotlib.pyplot.colorbar
  8. matplotlib.pyplot.sci

下载这个示例