views:

53

answers:

1

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

A: 

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)
honk
This plots two lines in the same figure, scaled with respect to different x axes. I'm trying to plot one line, scaled with respect to the lower x axis, while the upper x axis is just to indicate that same line in different units. I'll add an example to my question, which is very similar to what I need to do---variance on the bottom, mass on the top, where variance(mass) and vice versa. Thanks again.
Kyle
@Kyle I updated the answer for that case.
honk
Thanks. Ok...this is closer, but the problem is that the second axis is effectively a new plot just overlaid on the first. My top axis needs to know about the bottom axis so that the relationship between the two is correct. In my example plot the relationship between M and sigma may be complicated, so you couldn't just have the ticks on both linearly divided between their min and max. There may not be an easy way to do this automatically in matplotlib (though I'm told there is in IDL...).
Kyle
@Kyle Now that's what `xticks()` from `matplotlib.pyplot` is there for.
honk
All right---I've been able to use twiny() and xticks() to manually add tickmarks in the right places. I thought there might be a way to do this automatically, but I guess not. Thanks for the help.
Kyle