Plotting a data set with two different units in Matplotlib

In my thesis, I handle polarimetric observations of Venus with Venus Express. Because of the north polar elliptical orbit of the spacecraft, when observing in nadir there is a link between the phase angle and the latitude of observation1.
Though, this is not always easy to understand so I found the need to plot my polarimetric measurements as a function of phase angle, but also as a function of latitude. I was thinking of plotting the phase angle on the bottom x-axis, and the latitude on the top x-axis. Can we do that in Python/Matplotlib? 

Of course you can!

Matplotlib has brilliant functions called twinx and twiny that take an axis object and duplicate it keeping the same x or y axis that the original. Therefore only the x/y axis is editable as the other is ruled by the original axes.
This can be used to plot two independent curves with different y-axis, but also to plot one value with two units (e.g. Celsius degrees and Farenheit degrees). In the latter case, the relation is known theoretically, so you could just set the new x-ticks with a function of the old ticks.

An example of this:

> fig = figure()  #you can proceed without objects, but it's easier with
> ax = fig.add_subplot(111)
> ax.plot(X,Y)  # plot the data
> ax2 = ax.twiny()  # copy the x-axis
> ax2.plot(X,Y,alpha=0) # blank plot
> ax.set_xticks([a,b,c])  #we set the ticks on the x_1 axis
> ax2.set_xticks([a,b,c])  # idem on x_2
> ax2.set_xticklabels(function([a,b,c])) #function just does something

You may also not know the exact relation between x and x2, like in my case where for each point of measure there is an associated phase angle value and a latitude value, but each evolving according to a different rule. And again, because of the elliptical orbit of Venus Express, the latitude evolves much faster when the spacecraft is close to the pericentre. So, no theoretical relation for me.

At first glance, and following this post on Stackexchange, I thought that plotting my polarization as a function of phase angle on the ax object and as a function of latitude in the ax2 object was the thing to do. It is not.
If you do so, you get yourself with a shift. I could change the ticks as much as I can, it would not work.

The trick here is to plot the same data on the new axes, in order to get the same scaling, but in an invisible manner, hence the alpha=0. Then you can tweak the axes as you wish.

A measurement of SPICAV-IR: the degree of linear polarization as a function of phase angle and the corresponding latitude. Note how the latitude evolves much faster at high phase angles.




Sources:
http://stackoverflow.com/questions/3136800/matplotlib-one-line-plotted-against-two-related-x-axes-in-different-units

Notes:
1 the phase angle is the angle between the Sun, the point observed on Venus, and the observer. From Earth, if Venus is seen with a phase angle of 90 degrees it means that your see a half-Venus. Nadir is the opposite of zenith, so it is right below you, towards the center of the Earth/body you're on/around. Because of the low inclination of Venus axis, the subsolar point is nearly on the equator at noon on Venus. Then if you look towards the centre of the planet, a point seen from a low phase angle must be close to the equator (you have the Sun on your back). If you go towards 6am or 6pm, you can have a larger phase angle at low latitude, but not very large. On the other hand, if you get close to the pole, you necessarily have a high phase angle in nadir. Hence, the common variation of the latitude with the phase angle. Note that (1) if Venus Express was on an equatorial orbit, the link would be between phase angle and local time; (2) if you're not in nadir, i.e. you can look at Venus without looking towards its center, then the link between latitude and phase is broken.

Commentaires

Posts les plus consultés de ce blog

Scienca vojaĝo en Danujo

Les plantes sont-elles vraiment vertes ?