1

Using matplotlib, two x-axes for 1 line plot can easily be obtained using twiny().

If the transform between the two x-scales can be described by a function, the corresponding ticks can be set by applying this transform function. (this is described here: How to add a second x-axis in matplotlib)

How can I achieve this, if the transform function between the scales is unknown?

Edit: Imagine the following situation: You have 2 thermometers, both measuring the temperature. Thermometer 1 is measuring in °C and thermometer 2 in an imaginary unit, lets call it °D. Basically, what you know is that with increasing °C °D is increasing as well. Additionally, both thermometers have some degree of inaccuracy. Both thermometers measure the same physical quantity, hence I should be able to represent them with a single line and two scales. However, in contrast to plotting tempoeratures in °C vs. K or °F, the transformation between the scales is unknown.

This means for example I have:

     import numpy as np
     from matplotlib import pyplot as plt
     temp1 = np.sort(np.random.uniform(size=21))
     temp2 = np.sort(np.random.uniform(low=-20, high=20, size=21))
     y = np.linspace(0,1,21, endpoint=True)

A transform function between temp1 and temp2 is existent, but unknow. Y, however, is the same.

Additionally, I know that temp1 and y are confined to the range (0,1)

Now we may plot like this:

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.set_aspect('equal')
    ax2 = plt.twiny(ax1)
    ax1.plot(x1,y, 'k-')
    ax2.plot(x2,y, 'r:')
    ax1.set_xlabel(r'1st x-axis')
    ax2.set_xlabel(r'2nd x-axis')
    ax1.set_xlim([0,1])
    ax1.set_ylim([0,1])
    fig.savefig('dual_x_faulty.png', format='png')

This leads to the following plot: output plot You can see that both curves are not the same, and the plot is not square (as it would be without twinning the y axis).

So, here is what I want (and can't achieve on my own):

  • Plotting a 3d-array (temp1, temp2, y) in a 2d line plot by having two x-axes
  • Matplotlib shoud 'automagically' set the ticks of temp2 such, that the curves (temp1, y) and (temp2, y) are congruent

Is there a workaround?

Thanks for your help!

Community
  • 1
  • 1
user2051916
  • 1,401
  • 1
  • 10
  • 10
  • You've got too many questions all at once, and it's also very unclear what you are really looking for - **1)** Why would you think the curves should be the same, when you are plotting different data? **2)** If you don't know the x2/x1 relationship, it is impossible to set it, by definition, so I don't know what you're trying to do. **3)** What do either of those have to do with plotting 3D data? – Ajean Sep 13 '14 at 00:53
  • Ok - here my replys to the data 3) There are three vectors (or 1d arrays): x1, x2, y - this is 3d 2) I want to plot (x1,y) and (x2,y) - however, I know that x1 and x2 are just different scalings of each other, but the transfer function is unknow - so I need matplotlib to automagically put the ticks such, that both lines are congruent 1) that data is essentially the same - but the transformation between x1 and x2 is unknwon and possibly obscured by noise. I have edited the question accordingly. – user2051916 Sep 15 '14 at 07:09

0 Answers0