differences between twinx and twiny

Hi,

I just noticed a bug in twinx/twiny in axes.py. twinx has:

        ax2 = self.figure.add_axes(self.get_position(True), # sharex=self,
            frameon=False)

while twiny has:

        ax2 = self.figure.add_axes(self.get_position(True), sharey=self,
            frameon=False)

Therefore twiny will share the y axis, while twinx will not share the x
axis. I am not sure what the "desired" behavior is, but one has to be
wrong. As the principle use for this is making plots of two curves
sharing one axis, but different in the other, I imagine that the twiny
behavior is the desired one. If not, then the following doesn't look
quite right:

from numpy import *
from matplotlib.pylab import *
x = linspace(0,pi,20)
y = sin(x)
x2 = x + 0.1 * randn(*x.shape)
y2 = 10 + y + 0.1 * randn(*y.shape)

a1 = gca()
plot(x,y)

a2 = twinx()
plot(x2,y2, 'o')

The pylab_examples/two_scales.py only works because the two curves have
identical x values.

However, forcing them to share has the undesirable consequence that both
x-axes must have the same labels and formatting, producing overlayed
labels that are slightly noticeable.

I have committed to SVN the change making twinx work like twiny. As an
aside, this would not be necessary if there was an easy after the fact
way of sharing and unsharing axes (i.e., ax.set_shared_x_axes(ax2)).

Cheers,
David

···

--
**********************************
David M. Kaplan
Charge de Recherche 1
Institut de Recherche pour le Developpement
Centre de Recherche Halieutique Mediterraneenne et Tropicale
av. Jean Monnet
B.P. 171
34203 Sete cedex
France

Phone: +33 (0)4 99 57 32 27
Fax: +33 (0)4 99 57 32 95
http://www.ur097.ird.fr/team/dkaplan/index.html
**********************************

Yes, the sharex attr should be set on twinx -- perhaps someone
commented this out while debugging and forgot to restore it. I have
been known to make this mistake on more than one occasion, so thanks
for catching it. As for the duplicated ticklabels, I think we should
make the ticklabels invisible for ax2

for tick in ax2.xaxis.get_major_ticks() + ax2.get_minor_ticks():
    tick.set_visible(False)

Are there any potential problems with this that you can see?
Obviously we would want to document and mention it in the CHANGELOG,
because it might affect some code that is using the twin axes to
configure tick labels, but it should be an easy fix for those users to
simply use the original axes and it will get it right in most cases
(no duplicate labels or ticks).

I agree that being able to set the sharex and sharey after the fact
would be useful.

JDH

···

On Fri, Jul 25, 2008 at 5:23 AM, David Kaplan <David.Kaplan@...622...> wrote:

I have committed to SVN the change making twinx work like twiny. As an
aside, this would not be necessary if there was an easy after the fact
way of sharing and unsharing axes (i.e., ax.set_shared_x_axes(ax2)).