Cycler造型

演示自定义属性周期设置,以控制多行打印的颜色和其他样式属性。

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

  1. 设置默认值 rc parameter 指定属性循环。这会影响所有后续轴(但不会影响已创建的轴)。
  2. 为一对轴设置属性循环。
from cycler import cycler
import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(0, 2 * np.pi)
offsets = np.linspace(0, 2*np.pi, 4, endpoint=False)
# Create array with shifted-sine curve along each column
yy = np.transpose([np.sin(x + phi) for phi in offsets])

# 1. Setting prop cycle on default rc parameter
plt.rc('lines', linewidth=4)
plt.rc('axes', prop_cycle=(cycler(color=['r', 'g', 'b', 'y']) +
                           cycler(linestyle=['-', '--', ':', '-.'])))
fig, (ax0, ax1) = plt.subplots(nrows=2, constrained_layout=True)
ax0.plot(yy)
ax0.set_title('Set default color cycle to rgby')

# 2. Define prop cycle for single set of axes
#    For the most general use-case, you can provide a cycler to
#    `.set_prop_cycle`.
#    Here, we use the convenient shortcut that we can alternatively pass
#    one or more properties as keyword arguments. This creates and sets
#    a cycler iterating simultaneously over all properties.
ax1.set_prop_cycle(color=['c', 'm', 'y', 'k'], lw=[1, 2, 3, 4])
ax1.plot(yy)
ax1.set_title('Set axes color cycle to cmyk')

plt.show()
Set default color cycle to rgby, Set axes color cycle to cmyk

工具书类

以下函数、方法、类和模块的使用如本例所示:

出:

<function _AxesBase.set_prop_cycle at 0x7f6239b19a60>

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery