where is twiny ??

Hi,

I would need the equivalent of twinx, but for other axis, so a "twiny"
function. Shouldn't it be in mpl already?

thanks for your help,

Eric

At the time noone wanted it, but it's easy enough to do. I just added
the following to pylab svn:

def twiny(ax=None):
    """
    Make a second axes overlay ax (or the current axes if ax is None)
    sharing the yaxis. The ticks for ax2 will be placed on the top,
    and the ax2 instance is returned.
    """
    if ax is None:
        ax=gca()

    ax2 = gcf().add_axes(ax.get_position(), sharey=ax, frameon=False)
    ax2.xaxis.tick_top()
    ax2.xaxis.set_label_position('top')
    ax.xaxis.tick_bottom()
    draw_if_interactive()
    return ax2

···

On 2/13/07, Eric Emsellem <emsellem@...419...> wrote:

Hi,

I would need the equivalent of twinx, but for other axis, so a "twiny"
function. Shouldn't it be in mpl already?

thanks for your help,

twiny() doesn't exist, but the source code of twinx() is in
lib/matplotlib/pylab.py and it's about eight lines long, so it's easy
enough to duplicate it, changing right to top, left to bottom, etc.
See for example

http://www.nabble.com/How-can-I-avoid-sharey-tick-ovelap--t2700831.html

The one catch with this, practically, is that the "top" x axis is
written in the same place as the plot title. I have yet to play with
the placement of the title.

···

On Tue, 13 Feb 2007, Eric Emsellem wrote:

I would need the equivalent of twinx, but for other axis, so a "twiny"
function. Shouldn't it be in mpl already?

======================================================================
Office: 0.17 (Golm) Dr. John T. Whelan
Phone: +49 331 567 7117 Albert-Einstein Institute
FAX: +49 331 567 7298 Am Muehlenberg 1
http://www.aei.mpg.de/~whelan/ D-14476 Potsdam
john.whelan@...1436... john.whelan@...1437...

Thanks a lot for adding it in svn (I indeed did the change myself in my
pylab.py).

cheers and thanks again for the quick and positive response!

Eric

John Hunter wrote:

···

On 2/13/07, Eric Emsellem <emsellem@...419...> wrote:

At the time noone wanted it, but it's easy enough to do. I just added
the following to pylab svn:

def twiny(ax=None):
   """
   Make a second axes overlay ax (or the current axes if ax is None)
   sharing the yaxis. The ticks for ax2 will be placed on the top,
   and the ax2 instance is returned.
   """
   if ax is None:
       ax=gca()

   ax2 = gcf().add_axes(ax.get_position(), sharey=ax, frameon=False)
   ax2.xaxis.tick_top()
   ax2.xaxis.set_label_position('top')
   ax.xaxis.tick_bottom()
   draw_if_interactive()
   return ax2

--

Eric Emsellem emsellem@...419...
                           Centre de Recherche Astrophysique de Lyon
9 av. Charles-Andre tel: +33 (0)4 78 86 83 84
69561 Saint-Genis Laval Cedex fax: +33 (0)4 78 86 83 86
France http://www-obs.univ-lyon1.fr/eric.emsellem

A combination of moving the stop of the subplot down with the
subplots_adjust function and moving the title up by increasing the y
value (1 is the top of the axes) should do it

In [39]: fig = figure()
In [40]: subplots_adjust(top=0.8)
In [41]: ax1 = subplot(111)
In [42]: ax2 = twiny(ax1)
In [43]: ax1.plot(rand(100), rand(100), 'bo')
Out[43]: [<matplotlib.lines.Line2D instance at 0x93e8e8c>]
In [44]: ax2.plot(20*rand(100), rand(100), 'rs')
In [45]: ax1.set_title('this is a test', y=1.1)
Out[45]: <matplotlib.text.Text instance at 0x8e1c0ec>

···

On 2/13/07, John T Whelan <john.whelan@...1436...> wrote:

The one catch with this, practically, is that the "top" x axis is
written in the same place as the plot title. I have yet to play with
the placement of the title.