图左侧的两个y轴 [英] Two y-axis on the left side of the figure
问题描述
我想绘制具有相同x轴的y轴不同的曲线.我以前使用过twinx
函数,但是将它们绘制在图的另一侧.有没有办法将它们都绘制在左侧.我正在寻找类似以下内容的
I want to plot curves with different y-axis that share the same x-axis. I have used the twinx
function before, but it plot them on different side of the figure. Is there a way to plot both of them on the left hand side. I am looking for something like the following
,但两个轴都在同一侧.上面示例的代码是此处.
but with both the axis on the same side. The code for the above example is here.
在另一种不同的情况下,可以按某些特定顺序绘制曲线,因为z顺序不适用于twinx
On a different not, can one plot the curves in some particular order, as z-order do not work for twinx
推荐答案
红色表示的是默认的twinx()
行为. 示例中的额外修改适用于以绿色显示的内容.
Whats shown in red is the default twinx()
behavior. The extra modification in the example applies to whats shown in green.
您可以修改两个与green
相似的新轴,但是选择left
脊线并应用负偏移.因此,使用以下示例添加/更改示例:
You can modify both new axes similar as the green
one, but select the left
spine and apply a negative offset. So add/change the example with:
par1.spines["left"].set_position(("axes", -0.4)) # red one
par2.spines["left"].set_position(("axes", -0.2)) # green one
make_patch_spines_invisible(par1)
make_patch_spines_invisible(par2)
par1.spines["left"].set_visible(True)
par1.yaxis.set_label_position('left')
par1.yaxis.set_ticks_position('left')
par2.spines["left"].set_visible(True)
par2.yaxis.set_label_position('left')
par2.yaxis.set_ticks_position('left')
仅将轴中的zorder
纳入考虑范围(否则会出现?),因为彼此之间有单独的轴,因此应修改轴的zorder
:
The zorder
from lines is only taken into account within the axes (or so it appears?), since you have separate axes on top of each other, you should modify the zorder
of the axes:
host.set_zorder(1)
par1.set_zorder(2)
par2.set_zorder(3)
请注意,host
具有白色背景,除非将背景设置为透明,否则将其放置在顶部将隐藏其他行.
Note that the host
has a white background, placing it on top will hide the other lines unless you set the background to be transparent.
这篇关于图左侧的两个y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!