Description
While there is an elegant option of twinx
/twiny
for plotting two curves with two independent x or y axes into a single plot, in science one sometimes faces a different task: plotting one curve on a dual axis defined by a function.
As an example this can be an optical spectrum with bottom x-axis showing the wavelength λ in nanometers and top x-axis photon energy E in electronvolts. Note the functional dependence of these axes is nonlinear E=hc/λ. For publication, one also needs both axes to have nice, round and correctly positioned numbers, so passing this functional dependence directly into an object like xaxisformatter
is out of question.
I have implemented a solution that generates correct secondary x- or y-axes, also with the possibility of logarithmic option:
https://gist.github.com/FilipDominec/9101258e8ba979c715a28bc527a43961
Since I consider this situation relatively common in scientific plotting, I suggest the 30-line long trick could be added to matplotlib for easier use by others. A real-life image featuring two pairs of functionally dependent axes follows: