Making a plot with related y-axes

Hello!

I’m trying to make a plot (using PyPlot) with two y-axes that are interrelated (one is essentially the inverse of the other). To clarify, I have one set of data, but want the two related y-axes in order to practically use the graph. I was wondering how to best accomplish this.

Thank you!

In order to force the axes to be in sync (so you can add data, change limits, etc.) you can use “parasite axes”. In principle one can create them with a transform, but it is easier to just tinker with tick locations and labels as follows:

using PyPlot
using PyCall
const axgr1 = PyNULL()
copy!(axgr1, pyimport("mpl_toolkits.axes_grid1"))

function host_subplot(args...; kwargs...)
    pycall(axgr1["host_subplot"],PyAny, args... ; kwargs...)
end

# make some data
n=100
x=randn(n)
y=exp(x+0.25*rand(n))

# set up figure
f = figure()
ax1 = host_subplot(111)
ax2 = ax1[:twin]() # create parasite axes: defaults to identity transform

# show your data
semilogy(x,y,"o") # just to prove a point

ax1[:set_ylabel]("Custom scale")
ax2[:set_ylabel]("Normal scale")
ax2[:axis]["top"][:toggle](all=false)

# regular plot functions apply to ax1
ytvals = [0.25,0.5,1,2,4,8,16] # usually construct based on extrema(y)
# you can use an arbitrary transformation and format however you like
yticks(ytvals,([string(log2(i)) for i in ytvals]))
ylim(0.05,50)

# default selection isn't very nice
ax2[:set_yticks]([0.1,0.2,0.4,0.8,1,2,4,8,10,20,40])