从两个轴交换单个艺术家的 zorder [英] Interchanging the zorders on individual artists from two axes

查看:84
本文介绍了从两个轴交换单个艺术家的 zorder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,在该图上绘制了两个轴对象,即 ax1 ax2 ,其中 ax2 = ax1.twinx().

如何从每个轴互换个体艺术家的zorder?例如,我希望 ax1 中的一位艺术家位于背景中,然后将 ax2 中的所有艺术家置于其之上,然后绘制其余的艺术家ax1.例如,我想按照编写顺序绘制以下内容(即 (x3, y3) on top of (x2, y2) on top of (x2, y2) on(x1,y1))

的顶部

 将matplotlib.pyplot导入为plt图,ax1 = plt.subplots()ax2 = ax1.twinx()ax1.plot(x1, y1)ax2.plot(x2,y2)ax1.plot(x3, y3)

这怎么办?

使用变换

此方法仅使用2个轴,但是使用来自 ax1 的数据坐标在 ax2 上绘制绿线.该方法的问题在于 ax1 不会自动缩放,因此需要调用 set_ylim().另外,使用较大的代码来跟踪哪个艺术家使用了哪种转换可能会造成很大的混乱.

  fig,ax1 = plt.subplots()ax2 = ax1.twinx()l_bottom,= ax1.plot([1,2,3],[4,6,6],lw = 10,c ='C1')l2, = ax2.plot([1,2,3], [60,30,40], lw=10, c='C2')l_top,= ax2.plot([1,2,3],[5,10,3],lw = 10,c ='C3',transform = ax1.transData)ax1.set_ylim(2.65,10.35)#匹配上一张图的限制ax2.set_ylim(28.5,61.5)ax2.legend([l_bottom,l2,l_top],['left','right','left'])ax2.set_title('使用转换')

I have a figure on which I plot two axes-objects, ax1 and ax2, where ax2 = ax1.twinx().

How can I interchange the zorders on individual artists from each of the axes? E.g., I would like one artist from ax1 to be in the very background, then have all artists from ax2 on top of that, and then plot the rest of the artists from ax1. As an example, I would like to plot the following in the order in which it is written (i.e., (x3, y3) on top of (x2, y2) on top of (x1, y1))

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x1, y1)
ax2.plot(x2, y2)
ax1.plot(x3, y3)

How can this be done?

This answer shows how to reverse the zorders of the axes, but not on individual items.

解决方案

You cannot simply adjust the z-order of artists across axes. The z-order of the Artists belonging to one Axes are relative to that axes only.

You can, however, obtain the desired effect, but no easily. Here are two options:

Using 3 axes

The simplest way in my opinion is to use 3 axes, with the 3rd axis being a complete "clone" of the first one, but with a z-order above ax2.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

ax3 = fig.add_axes(ax1.get_position(), sharex=ax1, sharey=ax1)
ax3.set_facecolor('none') #prevents axes background from hiding artists below
ax3.set_axis_off() # pervents superimposed ticks from being drawn

l_bottom, = ax1.plot([1,2,3], [4,6,6], lw=10, c='C1')
l2, = ax2.plot([1,2,3], [60,30,40], lw=10, c='C2')
l_top, = ax3.plot([1,2,3],[5,10,3], lw=10, c='C3')
ax3.legend([l_bottom, l2, l_top],['left','right','left'])
ax3.set_title('using a 3rd axe')

Using transforms

This methods only uses 2 axes, but plots the green line on ax2 using the data-coordinates from ax1. The problem with that method is that ax1 will not autoscale automatically, hence the need to call set_ylim(). Plus it could get quite confusing in a larger code to keep track of which transform is used by which artist.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()

l_bottom, = ax1.plot([1,2,3], [4,6,6], lw=10, c='C1')
l2, = ax2.plot([1,2,3], [60,30,40], lw=10, c='C2')
l_top, = ax2.plot([1,2,3],[5,10,3], lw=10, c='C3',transform=ax1.transData)
ax1.set_ylim(2.65,10.35)  # matches the limits on the previous graph
ax2.set_ylim(28.5,61.5)
ax2.legend([l_bottom, l2, l_top],['left','right','left'])
ax2.set_title('using transforms')

这篇关于从两个轴交换单个艺术家的 zorder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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