# more than two y-axes

Hi,
I'd like to have figure with 3 (or 4) plots having different scales
but sharing the same x-axis.
Basically I want an extension of the twinx command (see, e.g,
two_scales.py demo).
I'm using 0.91.2svn on MacOSX10.5.1 from http://trichech.us/?page_id=5

I've tried the following hack (creating very narrow axes for the 3rd
y-axis), which seems to work reasonably well but leads to a thick tick line. As expected pan&zoom doesn't work for all axes.

···

-------------------------------------------------------------------
#!/usr/bin/env python
from pylab import *

# some functions to plot
x=linspace(0,2*pi,100)
y1=sin(x)
y2=10*cos(x)
y3=0.5*cos(2*x)

# create main axes
factor=0.9 # how much x-space is used by subaxes
am=axes(xticks=[])
pos = am.get_position()
pos[2] = factor*pos[2] # frame for subaxes

# first plot
a1=axes(pos,frameon=True)
p1=plot(x,y1,'b')
xlim(min(x),max(x))
xlabel("x")
a1.yaxis.tick_left()
ylabel("axis 1")

# second plot, y-axis on the right side
a2=axes(pos,frameon=False)
p2=plot(x,y2,'r')
xlim(a1.get_xlim())
a2.yaxis.tick_right()
a2.yaxis.set_label_position("right")
ylabel("axis 2")

# third plot, 2nd y-axis on the right side taken over by main axes
a3=axes(pos,frameon=False,yticks=[],xticks=[])
p3=plot(x,y3,'g')
xlim(min(x),max(x))

# legend for all three plots
legend((p1,p2,p3),('plot 1','plot 2','plot 3'),loc=0)

# now adjust settings for main axes
# draw y-axis of third plot
axes(am)
pos = am.get_position()
# move to right and make it a line
pos[0] += pos[2]
pos[2] = 0.001 # 0 doesn't work
am.set_position(pos)
# draw third y-axis
ylim(a3.get_ylim())
ylabel("axis 3")
am.yaxis.tick_right()
am.yaxis.set_label_position("right")

show()
-------------------------------------------------------------------

However, if I try to extend it to 4 y-axes (2 on each side)
I encounter several problems:

-------------------------------------------------------------------
#!/usr/bin/env python
from pylab import *

# some functions to plot
x=linspace(0,2*pi,100)
y1=sin(x)
y2=10*cos(x)
y3=0.5*cos(2*x)
y4=2*sin(0.5*x)

# create main axes
factor=0.1 # how much x-space is used by y-axes
am=axes(xticks=[])
pos = am.get_position() # frame for subaxes
pos[0] += factor*pos[2]
pos[2] = (1-2*factor)*pos[2] # frame for subaxes

# first plot, 2nd y-axis on the left side taken over by main axes
a1=axes(pos,frameon=False,yticks=[],xticks=[])
p1=plot(x,y1,'b')
xlim(min(x),max(x))
ylim1= a1.get_ylim() # bug: save as it is overwritten by plot4

# second plot, axis left
a2=axes(pos,frameon=True)
p2=plot(x,y2,'r')
xlim(a1.get_xlim())
xlabel("x")
a2.yaxis.tick_left()
ylabel("axis 2")

# third plot, y-axis on the right side
a3=axes(pos,frameon=False)
p3=plot(x,y3,'g')
xlim(a1.get_xlim())
a3.yaxis.tick_right()
a3.yaxis.set_label_position("right")
ylabel("axis 3")

# fourth plot, 2nd y-axis on the right side taken over by main axes
a4=axes(pos,frameon=False,yticks=[],xticks=[])
p4=plot(x,y4,'m')
xlim(a1.get_xlim())
ylim4 = a4.get_ylim()

# legend for all three plots
legend((p1,p2,p3,p4),('plot 1','plot 2','plot 3','plot 4'))

# now adjust settings for main axes
# draw y-axis of third plot
axes(am)
pos = am.get_position()
pos2 = array(pos)
pos[2] = 0.001
am.set_position(pos)
# draw third y-axis
am.set_ylim(ylim1)
a1.set_ylim(ylim1)
ylabel("axis 1")
am.yaxis.tick_left()
am.yaxis.set_label_position("left")

# now adjust settings for main axes
# draw y-axis of fourth plot

# move to right and make it a line
pos2[0] += pos2[2]
pos2[2] = 0.001
am2 = axes(pos2,xticks=[])
# draw fourth y-axis
am2.set_ylim(ylim4)
a4.set_ylim(ylim4)
ylabel("axis 4")
am2.yaxis.tick_right()
am2.yaxis.set_label_position("right")

show()
-------------------------------------------------------------------

The 4th plot overwrites the ylim of the 1st plot (bug?).
Is there a limit of 3 axes per figure?
The ylim of plot4 is still ignored.
Why do I have to add the axes a1 and a4 the main axes to make them visible?

Any ideas?

cheers,
--
Thomas Tanner ----------------
email: tanner@...368...
phone: (+49) 7071 601 608
GnuPG key id: 1024/5924D4DD
Max Planck Institute for Biological Cybernetics
Spemannstrasse 38
72076 Tübingen, Germany

Hi,
I'd like to have figure with 3 (or 4) plots having different scales
but sharing the same x-axis.
Basically I want an extension of the twinx command (see, e.g,
two_scales.py demo).
I'm using 0.91.2svn on MacOSX10.5.1 from http://trichech.us/?page_id=5

[snip]

The 4th plot overwrites the ylim of the 1st plot (bug?).
Is there a limit of 3 axes per figure?
The ylim of plot4 is still ignored.
Why do I have to add the axes a1 and a4 the main axes to make them visible?

I'm working on an app that has 8 axes per figure (X1, X2, Y1 ... Y4)
and it works fine. Well, the MPL bits do.

One of the unexpected things that we had to do in order to get the
multiple plots to work as expected was to ensure that each additional
subplot had its own label. MPL has some 'helper' code that tries to
consolidate the axes so that if you create two axes with identical
parameters, you only end up with one axes object (I think -- someone
more knowledgeable can fill you in on the details).

Now, we do things using the API rather than using pylab, some I'm not
entirely certain (and haven't tested it) but I think that the pylab
command 'axes' has an optional argument of 'label', which you should
set to be a unique string for each subplot.

I've only scanned through your code, and I'm not at all familiar with
the pylab syntax, but I'm puzzled that you haven't used twinx (or in
API parlance, sharex) anywhere. Regardless, the API code for
overlaying subplots is of the form: