开发者

How do I get handle to axes in pyplot twinned axes?

I need some clarification of add_subplot: in example below, i have 2 subplots, twinned on the y axis. I want to manipulate each axis, but don't get it!

开发者_如何学JAVA
import matplotlib.pyplot as plt
fig = plt.figure()
axes1 = fig.add_subplot(111)
axes2 = axes1.twiny()    # share the y-axis
axes1.plot( somedata[:,2], somedata[:,1], label='axis1' )
axes2.plot( somedata[:,3], somedata[:,1], label='axis2' )
plt.legend()
plt.show()

I tried to set the limits on each x axis: axes1.set_data_interval(0, 300), which fails.

Labels in the legend only appear for the second axis, axes2. Again, I don't understand how this worked, or how to change it. I guess the latest command (axes2.plt()) leaves that axis as the active one for the plot. How would I manipulate this explicitly?


1) To set the limits of each x axis, axes1.set_xlim(0,300) and axes2.set_xlim(0, 300) do work for me.

The method set_data_interval is a method of the 'axis' (the real axis of the plot), while set_xlim is a method of the 'axes' object (the area your plot appears in, with the axis, lines, text, etc). And axes1 and axes2 are 'axes' (returned by the add_subplot method).

2) To get the labels of both axes in the legend, have a look at the solution given by this question: Secondary axis with twinx(): how to add to legend?


axes.gca() did not work for me. axes.set_ylim(y0,y1) and axes.set_xlim(x0,x1) worked perfectly!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜