Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

matplotlib - Two y-axis on the left side of the figure

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 followingenter image description here

but with both the axis on the same side. The code for the above example is here.

On a different not, can one plot the curves in some particular order, as z-order do not work for twinx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Whats shown in red is the default twinx() behavior. The extra modification in the example applies to whats shown in green.

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')

enter image description here

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)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...