1

I have a file containing three columns of data, (0,1,2). Column 1 contains y-values, and column 0 and 2 contain x-values.

I would like to have a single plot with a shared y-axis and the two x-axis with the correct scaling. Column 2 is a non-linear function of column 0, for which I don't have an analytical expression, so I think I cannot follow the steps of How to add a second x-axis in matplotlib.

I (naively) tried this:

data = pylab.loadtxt('./datafile.txt')
fig = pylab.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax2 = ax1.twiny() #set of axes sharing y

ax1.plot(data[:,0], data[:,1])
ax2.plot(data[:,2], data[:,1])

but clearly what I obtain is a figure with two linear x-axis, and therefore two curves. Can I get matplotlib to keep ax1 linear, scale ax2 accordingly and have a single curve in figure?

Thanks!

Community
  • 1
  • 1
Giulia
  • 297
  • 1
  • 2
  • 11
  • Did you try `ax2 = ax1.twinx()` insetad of `ax2 = ax1.twiny()`? – ssm Nov 19 '14 at 08:41
  • Hi, if I use twinx I do get a single curve in my plot, but I also get the y-axis repeated identical on the left and the right side, and no ticks for the ax2 x-axis. Do I need to add some other instruction? – Giulia Nov 19 '14 at 09:27
  • Just so I understand correctly, you want to plot `(x, y)` and `(x1, y)` but obtain a single curve by scaling one of the axis? And if you plot `(x, x1)` you will get a really nonlinear curve which you cannot obtain an expression for? – ssm Nov 20 '14 at 01:55
  • Yes. Maybe I could get an expression for x1=f(x) from a fit and then use it to scale the ticks. I had this idea and thought it was a complicated solution to a simple problem, but maybe it would be ok. – Giulia Nov 20 '14 at 13:46

0 Answers0