Ahhh - Thanks! I was wondering if both axes would scale when doing a=20
zooming operation but I hadn't had time to try it out. Thanks for the=20
example, that's exactly what I was looking for.
Ted
ps: I too am on the list.
At 03:12 PM 5/20/2005, Baptiste Carvello wrote:
>Ted Drain a =E9crit :
>>I was playing with twinx() a little tonight from the pylab.py file and=20
>>have a small suggestion. I think twinx() should call (before creating=20
>>the new axes):
>>gca().yaxis.tick_left()
>>Currently, after calling twinx(), then new axis tick marks correctly=20
>>appear on the right side of the plot. However, the major tick marks from=
=20
>>the left hand side Y axis also appear on the right side of the plot which=
=20
>>is a little confusing.
>indeed, this should be fixed.
>
>>Also, since we have twinx(), it would be nice to have twiny():
>>def twiny(ax=3DNone):
>> """
>> 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. See examples/two_scales.py
>> """
>> if ax is None:
>> ax=3Dgca()
>> ax.xaxis.tick_bottom()
>>
>> ax2 =3D gcf().add_axes(ax.get_position(), sharey=3Dax, frameon=3DFals=
e)
>> ax2.xaxis.tick_top()
>> ax2.xaxis.set_label_position('top')
>> draw_if_interactive()
>> return ax2
>We could do that. The only reason we didn't yet is because nobody thought=
=20
>of a clear use case. If you have one, it can be done. However, see below.
>
>>The case I'm thinking of where twiny() would would be useful is one that=
=20
>>we hit a lot at work. Time is represented on the X axis and we want=20
>>multiple label types. So we'd like to have the bottom axis be time in=20
>>UTC and the upper axis to be PST or some other local time frame. I'm not=
=20
>>sure whether or not it would be best to replot the data using twiny() or=
=20
>>to try and set up a custom formatter on the second X scale since in my=20
>>scenario there aren't two sets of data.
>I wouldn't do that. The 2 x axes won't stay in sync if you do a zoom or a=
=20
>rescale. What twiny is for would be if you need a different scale.
>
>On the contrary, you should use twinx for that, as follows:
>
> >>> from pylab import *
> >>> from pytz import timezone
> >>> from datetime import datetime
> >>> from matplotlib.dates import DateFormatter, HourLocator, DayLocator,=
=20
> date2num
> >>> from matplotlib.axis import Ticker
>
> >>> tz=3Dtimezone('US/Pacific')
> >>> utc=3Dtimezone('UTC')
> >>> start=3Ddate2num(datetime( 2005, 05, 20, 21, 12 ,tzinfo=3Dtz))
> >>> T=3Darange(0,1.5,0.01)
> >>> V=3Drand(len(T))
>
> >>> plot(start+T,V)
> >>> ax=3Dgca()
> >>> ax2=3Dtwinx()
> >>> gcf().sca(ax)
>
> >>> ax2.yaxis.set_visible(False)
>
> >>> ax.xaxis.set_major_locator(DayLocator(tz=3Dtz))
> >>> ax.xaxis.set_major_formatter(DateFormatter('%d %b',tz=3Dtz))
> >>> ax.xaxis.set_minor_locator(HourLocator(range(0,25,6), tz=3Dtz))
> >>> ax.xaxis.set_minor_formatter(DateFormatter('%H:%M',tz=3Dtz))
> >>> ax.xaxis.tick_bottom()
> >>> ax.set_xlabel('US/Pacific')
>
>
> >>> ax2.xaxis.major=3DTicker()
> >>> ax2.xaxis.minor=3DTicker()
> >>> ax2.xaxis.set_major_locator(DayLocator(tz=3Dutc))
> >>> ax2.xaxis.set_major_formatter(DateFormatter('%d %b',tz=3Dutc))
> >>> ax2.xaxis.set_minor_locator(HourLocator(range(0,25,6), tz=3Dutc))
> >>> ax2.xaxis.set_minor_formatter(DateFormatter('%H:%M',tz=3Dutc))
> >>> ax2.xaxis.tick_top()
> >>> ax2.set_xlabel('UTC')
> >>> ax2.xaxis.set_label_position('top')
>
> >>> draw_if_interactive()
> >>> show()
>
>note that if you do not track cvs, the minor tick labels on the top axis=20
>will not print, due to a known bug, fixed in cvs.
>
>Cheers,
>Baptiste
>
>PS: don't CC me in replies, I'm subscribed to the list. If you also are,=20
>please say so.
|