matplotlib colororder和twinx

Mor*_*itz 3 python matplotlib

我通过调整图的颜色顺序 rcParams['axes.color_cycle'] = [some nice and carefully chosen colours]

但是,当我将twinx用于第二个轴时,将重置颜色顺序:

 from matplotlib import pyplot as plt
 from matplotlib import rcParams
 rcParams['axes.color_cycle'] = ['r','g','k']
 fig = plt.figure()
 ax = fig.add_subplot(1,1,1)
 ax1 = plt.twinx(ax) 
 ax.plot([1,2,3],[4,5,6])
 ax.plot([1,2,3],[7,6,4])
 ax1.plot([1,2,3],[5,3,1])
Run Code Online (Sandbox Code Playgroud)

有办法避免这种情况吗?画在上面的线ax1应该是黑色的。

小智 5

可以不使用第二个轴的颜色循环器(如其他答案中所建议的那样),而实际上可以对两个轴使用单个颜色循环器:

fig, ax1 = plt.subplots()
ax1.plot(...)

ax2 = ax1.twinx()
ax2._get_lines.prop_cycler = ax1._get_lines.prop_cycler
ax2.plot(...)
Run Code Online (Sandbox Code Playgroud)