python - 使用 2 y 轴时如何定义 zorder?

标签 python numpy matplotlib

我在 matplotlib 图的左侧和右侧使用两个 y 轴进行绘图,并使用 zorder 来控制绘图的位置。我需要在同一图中定义 zorder across 轴。


问题

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10,0.01)

fig, ax1 = plt.subplots( 1, 1, figsize=(9,3) )
ax1.plot( x, np.sin(x), color='red', linewidth=10, zorder=1 )
ax2 = ax1.twinx()
ax2.plot( x, x, color='blue', linewidth=10, zorder=-1)

enter image description here

在上图中,我希望蓝线出现在红色图的后面

如何在使用双轴时控制 zorder


我正在使用:

python: 3.4.3 + numpy: 1.11.0 + matplotlib: 1.5.1

最佳答案

这应该可行

ax1.set_zorder(ax2.get_zorder()+1)
ax1.patch.set_visible(False)

关于python - 使用 2 y 轴时如何定义 zorder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38687887/

相关文章:

python - Python 多线程协助

Python:如何计算 pandas 数据框中 1 的分数?

python - 在Python openCV中获取视频每一帧的耗时

Matplotlib:设置x限制还会强制打勾标签吗?

python - 如何在matplotlib上生成一系列直方图?

python - BeautifulSoup 查找有条件的结果

python - 在 Numpy 中排列对称方阵的有效方法

python - 使用 OpenCV python 绑定(bind)索引 channel 数据(numpy 数组)

python - 切片 3d numpy 数组 - 误解

python - 具有不同颜色图的 matplotlib 表列