I've made a matplotlib plot with frequency on the x-axis, and I would like to add an additional x-axis at the top that is measured in wavelength , i.e. wavelength = 3e8 / frequency
Is there anyway to do this transformation automatically in matplotlib?
I tried to give a transformation argument to the ax.twin() axes_grid command, as shown in the axes_grid parasite_simple2.py example, but I've not managed to get this to work with a transformation more complicated than a scaling by a constant factor. I tried looking at the matplotlib.transforms documentation but I couldn't see a way to do this transformation there. I'm not sure I understood it very well though. I can't simply use the twiny( ) command and manually set the limits as the wavelength ticks will not occur at the points corresponding to the correct frequency.
At the moment I am using the twin() command, and then I manually choose a sensible set of tickvalues I want in wavelength units, calculate the corresponding frequency values, and then set the tick locations to be the frequency values and the tick labels to be the wavelength values.
import numpy as np
from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost
import matplotlib.pyplot as plt
#create xaxis range of values -- 200 -- 1000 Ghz
xvals = np.arange(199.9, 999.9, 0.1)
#make some test data
data = np.sin(0.03*xvals)
#set up the figure
fig = plt.figure()
ax = SubplotHost(fig, 111)
ax2 = ax.twin()
#set up ax2 with chosen values
wavelength_labels = np.array([0.4, 0.6, 0.8,1.0,1.2, 1.4]) #in mm
frequency_points = 3e2/wavelength_labels #in GHz