使用 2 y 轴时如何定义 zorder? [英] How to define zorder when using 2 y-axis?

查看:31
本文介绍了使用 2 y 轴时如何定义 zorder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I plot using two y-axis, on the left and the right of a matplotlib figure and use zorder to control the position of the plots. I need to define the zorder across axes in the same figure.

问题

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)

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

In the previous diagram, I would expect the blue line to appear behind the red plot.

使用双轴时如何控制zorder?

我正在使用:

python: 3.4.3 + numpy: 1.11.0 + matplotlib: 1.5.1

python: 3.4.3 + numpy: 1.11.0 + matplotlib: 1.5.1

推荐答案

这应该有效

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

这篇关于使用 2 y 轴时如何定义 zorder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆