Python 如何在matplotlib中控制twinx中的zorder?

Python 如何在matplotlib中控制twinx中的zorder?,python,matplotlib,z-order,twinx,Python,Matplotlib,Z Order,Twinx,我试图控制twinx轴上不同绘图的顺序。如何使蓝色的噪波图显示在背景中,橙色的平滑图显示在前景中 与之类似,但并不理想,这是一种使用twiny和twinx的方法 # set up plots fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax3 = ax1.twiny() ax4 = ax2.twiny() # background ax1.plot(x1) ax2.plot(x2) # smoothed ax3.plot(savgol_filte

我试图控制twinx轴上不同绘图的顺序。如何使蓝色的噪波图显示在背景中,橙色的平滑图显示在前景中

与之类似,但并不理想,这是一种使用
twiny
twinx
的方法

# set up plots
fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax4 = ax2.twiny()

# background
ax1.plot(x1)
ax2.plot(x2)

# smoothed
ax3.plot(savgol_filter(x1,99,2), c='orange')
ax4.plot(savgol_filter(x2,99,2), c='orange')

# turn off extra ticks and labels
ax3.tick_params(axis='x', which='both', bottom=False, top=False)
ax4.tick_params(axis='x', which='both', bottom=False, top=False)
ax3.set_xticklabels([])
ax4.set_xticklabels([])

# fix zorder
ax1.set_zorder(1)
ax2.set_zorder(2)
ax3.set_zorder(3)
ax4.set_zorder(4)

plt.show()
输出:


我不太喜欢这个解决方案,但我接受了这个答案,因为似乎没有更好的解决方案。
# set up plots
fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax3 = ax1.twiny()
ax4 = ax2.twiny()

# background
ax1.plot(x1)
ax2.plot(x2)

# smoothed
ax3.plot(savgol_filter(x1,99,2), c='orange')
ax4.plot(savgol_filter(x2,99,2), c='orange')

# turn off extra ticks and labels
ax3.tick_params(axis='x', which='both', bottom=False, top=False)
ax4.tick_params(axis='x', which='both', bottom=False, top=False)
ax3.set_xticklabels([])
ax4.set_xticklabels([])

# fix zorder
ax1.set_zorder(1)
ax2.set_zorder(2)
ax3.set_zorder(3)
ax4.set_zorder(4)

plt.show()