# inverse scale on twinx()

Hello to everybody,

since I looked serveral days in vain to find a solution to my problem, I would like to ask you for help!

Problem:
I want to plot at least three timeseries in one chart. Two timeseries are to be plotted as lines in a normal scale. The third timeseries (in my case these are precipitation-mesurements) are to be plotted as bars and - now the problem occurs... - this third timeseries should be scaled inversely from the top. In openoffice-Calc this function is called "reverse direction" at the scaling properties of the y-axis, even excel can handle that - so I am quite confident that matplotlib will do so, too. I just have no idea how...

Do you know how to scale an y-axis inversely (or 'reversly')?

Simon

ps - here's my demo, perhaps it helps understanding...

···

#############################
import datetime, scipy
from pylab import *

# creating dates
date1 = datetime.date( 1982, 1, 1 )
date2 = datetime.date( 1982, 2, 1 )
date3 = datetime.date( 1982, 3, 1 )
date4 = datetime.date( 1982, 4, 1 )

dates = scipy.array([date1, date2, date3, date4])

# creating some dummy-data
f1 = [1,5,3,4] # could be a simulated discharge
f2 = [2,5,4,5] # could be an observed discharge
f3 = [45, 36, 53, 21] # could be precipitation

figure()

# creating two x-axes in one plot
ax_q = axes()
ax_p=twinx()

# plotting 'discharges'
ax_q.plot(dates, f2, 'g-', dates, f1, 'b--')
# plotting 'precipitation'
ax_p.bar(date2num(dates), f3, width=5, bottom=0)

# doing some scaling on the x-axis
months = MonthLocator(range(1,13), bymonthday=1)
monthfmt = DateFormatter("%b '%y")
ax_q.xaxis.set_major_locator(months)
ax_q.xaxis.set_major_formatter(monthfmt)

# scaling 'discharge' on the left and 'precipitation' on the right y-axis
ax_q.yaxis.tick_left()
ax_p.yaxis.tick_right()

# some other scaling ...
ax_q.yaxis.set_major_locator(MultipleLocator(5) )

show()
##########################################

just now - after I wrote you - I found the solution in your mailing
list… hmmm

for the case anybody is interessted: You have to add

��� ax_p.set_ylim(ax_p.get_ylim()[::-1])

to get an inverse scaling on an y-axis.

The solution is from Eric Firing and to cite him: "The method in line 3
should work on new and old versions of matplotlib.

The basic idea is that when axis limits are set, you specify the
bottom,

then the top (or the right, then the left), not min and then max."

Sorry for being to late with my own work of research (I will surely be
back another time ).
greetings,
Simon
Simon schrieb:

···

Hello to
everybody,

since I looked serveral days in vain to find a solution to my problem,
I would like to ask you for help!

Problem:

I want to plot at least three timeseries in one chart. Two timeseries
are to be plotted as lines in a normal scale. The third timeseries (in
my case these are precipitation-mesurements) are to be plotted as bars
and - now the problem occurs… - this third timeseries should be
scaled inversely from the top. In openoffice-Calc this function is
called “reverse direction” at the scaling properties of the y-axis,
even excel can handle that - so I am quite confident that matplotlib
will do so, too. I just have no idea how…

Do you know how to scale an y-axis inversely (or ‘reversly’)?

Simon

ps - here’s my demo, perhaps it helps understanding…

#############################

import datetime, scipy

from pylab import *

# creating dates

date1 = datetime.date( 1982, 1, 1 )

date2 = datetime.date( 1982, 2, 1 )

date3 = datetime.date( 1982, 3, 1 )

date4 = datetime.date( 1982, 4, 1 )

dates = scipy.array([date1, date2, date3, date4])

# creating some dummy-data

f1 = [1,5,3,4]��������� # could be a simulated discharge

f2 = [2,5,4,5]��������� # could be an observed discharge

f3 = [45, 36, 53, 21]�� # could be precipitation

figure()

ax_q = axes()

ax_p=twinx()

# plotting ‘discharges’

ax_q.plot(dates, f2, ‘g-’, dates, f1, ‘b–’)

# plotting ‘precipitation’

ax_p.bar(date2num(dates), f3, width=5, bottom=0)

# doing some scaling on the x-axis

months = MonthLocator(range(1,13), bymonthday=1)

monthfmt = DateFormatter("%b '%y")

ax_q.xaxis.set_major_locator(months)

ax_q.xaxis.set_major_formatter(monthfmt)

# scaling ‘discharge’ on the left and ‘precipitation’ on the right

y-axis

ax_q.yaxis.tick_left()

ax_p.yaxis.tick_right()

# some other scaling …

ax_q.yaxis.set_major_locator(MultipleLocator(5) )

show()

##########################################