用cycler定型

演示自定义特性-循环设置以控制多行绘制的颜色和其他样式特性。

此示例演示了两种不同的API:

  1. from cycler import cycler
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. x = np.linspace(0, 2 * np.pi)
  5. offsets = np.linspace(0, 2*np.pi, 4, endpoint=False)
  6. # Create array with shifted-sine curve along each column
  7. yy = np.transpose([np.sin(x + phi) for phi in offsets])
  8. # 1. Setting prop cycle on default rc parameter
  9. plt.rc('lines', linewidth=4)
  10. plt.rc('axes', prop_cycle=(cycler(color=['r', 'g', 'b', 'y']) +
  11. cycler(linestyle=['-', '--', ':', '-.'])))
  12. fig, (ax0, ax1) = plt.subplots(nrows=2, constrained_layout=True)
  13. ax0.plot(yy)
  14. ax0.set_title('Set default color cycle to rgby')
  15. # 2. Define prop cycle for single set of axes
  16. # For the most general use-case, you can provide a cycler to
  17. # `.set_prop_cycle`.
  18. # Here, we use the convenient shortcut that we can alternatively pass
  19. # one or more properties as keyword arguements. This creates and sets
  20. # a cycler iterating simultaneously over all properties.
  21. ax1.set_prop_cycle(color=['c', 'm', 'y', 'k'], lw=[1, 2, 3, 4])
  22. ax1.plot(yy)
  23. ax1.set_title('Set axes color cycle to cmyk')
  24. plt.show()

cycler定型示例

参考

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

  1. import matplotlib
  2. matplotlib.axes.Axes.plot
  3. matplotlib.axes.Axes.set_prop_cycle

下载这个示例