Hi,
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.
Thanks,
Sarah
Example code:
import numpy as np
import matplotlib
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)
fig.add_subplot(ax)
ax2 = ax.twin()
#plot data
ax.plot(xvals, data)
ax.set_xlim(200.0, 1000.0)
#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
ax2.set_xticks(frequency_points)
ax2.set_xticklabels(wavelength_labels)
ax2.set_xlabel('Wavelength (mm)')
ax.set_xlabel('Frequency (GHz)')
plt.show()
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users