Making a plot with related y-axes



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...)

# make some data

# 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")

# 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]))

# default selection isn't very nice