我想你想使用Axes
method set_color_cycle
。正如您可以想象的那样,它设置默认分配颜色时(即没有向颜色关键字提供时)循环的颜色列表。plot
称呼。这是示例的扩展版本:
import matplotlib.pyplot as plt
import numpy as np
X = np.arange(1990, 1994)
Y = [[ 1.50615936e+08, 5.88252480e+07, 2.60363587e+08],
[ 1.53193798e+08, 5.91663430e+07, 2.63123995e+08],
[ 1.55704596e+08, 5.94899260e+07, 2.65840188e+08],
[ 1.58175186e+08, 5.97843680e+07, 2.68559452e+08]]
colors = [(0.99609375, 0.3984375, 0.3984375),
(0.796875, 0.0, 0.99609375),
(0.59765625, 0.99609375, 0.0)]
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title('old way')
ax1.plot(X, Y)
[ax1.lines[i].set_color(color) for i, color in enumerate(colors)]
ax2 = fig.add_subplot(212)
ax2.set_title('new way')
ax2.set_color_cycle(colors)
ax2.plot(X, Y)
fig.savefig('manycolors.py')
plt.show()
这会产生两个具有相同颜色线条的子图: