Tutorial :Matplotlib: one line, plotted against two related x axes in different units?



Question:

I have one y variable, which I am trying to plot against two related x axes, on the top and bottom of the figure (e.g. y="number of things in cube", x1="side length of cube", x2="volume of cube"). I have y, x1, x2 in numpy arrays. The relationship between my x1 and x2 is one-to-one and monotonic, but not simple, and they increase in different directions, like "side length" and "inverse volume". I've tried using twiny() and twin(), but these seem to be designed for plotting different y variables. Any ideas? Thanks everyone!

Below is an example of the kind of thing I'm trying to do, except with a single line rather than symbols. The idea is that, say, sigma=0.4 and M=2e15 are equivalent and interchangeable labels for one point.

alt text http://img580.imageshack.us/img580/4554/screenshotuy.png


Solution:1

For different x-scales use twiny() (think of this as "shared y-axes"). An example slightly adapted from the matplotlib documentation:

import numpy as np  import matplotlib.pyplot as plt    # plot f(x)=x for two different x ranges  x1 = np.linspace(0, 1, 50)  x2 = np.linspace(0, 2, 50)  fig = plt.figure()    ax1 = fig.add_subplot(111)  ax1.plot(x1, x1,'b--')    ax2 = ax1.twiny()  ax2.plot(x2, x2, 'go')    plt.show()  

If you just wanted a second axis plot the second data set as invisible.

ax2.plot(x2, x2, alpha=0)  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »