首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在matplotlib中跨twinx控制zorder?

如何在matplotlib中跨twinx控制zorder?
EN

Stack Overflow用户
提问于 2021-04-09 04:25:46
回答 1查看 137关注 0票数 0

我正在尝试控制twinx轴上不同绘图的zorder。如何在this plot中使蓝色噪声图显示在背景中,使橙色平滑图显示在前景中

代码语言:javascript
复制
from matplotlib import pyplot as plt
import numpy as np
from  scipy.signal import savgol_filter

random = np.random.RandomState(0)
x1 = np.linspace(-10,10,500)**3 + random.normal(0, 100, size=500)
x2 = np.linspace(-10,10,500)**2 + random.normal(0, 100, size=500)

fig,ax1 = plt.subplots()
ax1.plot(x1, zorder=0)
ax1.plot(savgol_filter(x1,99,2), zorder=1)
ax2 = ax1.twinx()
ax2.plot(x2, zorder=0)
ax2.plot(savgol_filter(x2,99,2), zorder=1)
plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-09 04:58:58

虽然不是很理想,但与this thread类似,这是一种结合使用twinytwinx的方法。

代码语言:javascript
复制
# 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()

输出:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67011393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档