Rohit Pk -5 years ago 214
Python Question

# To find the value on X axis corresponding to an arbitrary value of Y that doesn't contain in the list used to plot the data in python

I have a set of values for redshift and have the corresponding set of values for deceleration parameter of universe. The plot is given:

Now I need the value of z accurately from the graph for q=0. I have tried many commands and they all lead to some kind of errors. Since I am not that much in python I can't even try how to rectify all those.

When I tried the command:

`z1 = interp1d(z,q,0)`

the result was:

scipy.interpolate.interpolate.interp1d object at 0x051899F0

How can I solve this?

My code:

``````while z0<zf:' '
z.append(z0)
a.append(1/(1+z0))
term=((1+(omega/(B-1)))*a[k]**(3*(B-1)))
H.append(((term-(omega/(B-1)))*H02)**0.5)
q.append(-1-((H0*term*3*(B-1)*(term-(omega/(B-1)))-0.5)/(2*H[k])))
print '%.2f \t%.4f \t%.4f \t%.15f'%(z[k],a[k],H[k],q[k])
k=k+1
z0=z0+h
``````

Your code had some issues. I only have Python 3 here so if you have any problem understanding something just ask (changed a bit the `print` and the `input` function parts).

You did not define the `H02` so I just stated it was equal to `H0`. Correct that if it is wrong. Here is your corrected code (see the comments for important stuff):

``````from pylab import*
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

fig, ax = subplots()
k = 0
omega, B, H0 = 0.6911, 0.019921992722139001, 67.74
H02 = H0 + 0 # You didn't specify this so I had to make up something
z0 = 0
h = 0.05

z, a, H, q = [], [], [], []
print('Value of H0, Omega and Beta:%.3f,%.4f,%.18f'%(H0, omega, B))
zf = float(input('Enter the final value of redshift:')) # Remember that this needs to output a number
print('Red shift Scale factor Hubble parameter q value') # I chose 10 in my test.
print('===========================================================')

while z0 < zf:
z.append(z0)
a.append(1/(1+z0))
term=((1+(omega/(B-1)))*a[k]**(3*(B-1)))
H.append(((term-(omega/(B-1)))*H02)**0.5)
q.append(-1-((H0*term*3*(B-1)*(term-(omega/(B-1)))-0.5)/(2*H[k])))
print('%.2f \t%.4f \t%.4f \t%.15f'%(z[k], a[k], H[k], q[k]))
k = k+1
z0 = z0+h

title('Decceleration parameter(q) versus Red shift(z) graph ')
xlabel('Redshift z')
ylabel('Decceleration parameter q')

z1 = interp1d(q, z)
print(z1(40000)) # I used a redshift parameter of 10 and 0 does not exist in the data limits so I just used 40 000.
plot(z, q)
plot([z1(40000), z1(40000)], [0, 40000], c='r')
plot([0, z1(40000)], [40000, 40000], c='r')

ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.yaxis.tick_left()
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.tick_bottom()
show()
``````

, and it results in this:

I added the red line to show you the result of the interpolation which is made like this:

``````z1 = interp1d(q, z) # Create your function with your q (input) and z (output)
result = z1(40000) # This is checking the value of z when q=40000
``````

The thing you need to remember is that `interp1D` will only be able to interpolate in the region of the data you gave. So if `q` is within `10` and `100` you cannot interpolate `q=0`.

I used a redshift of 10 (did not know what to use although the subject seemed interesting). This does not seem to affect the plot so I'm guessing something went wrong when you've put your code in here or when I tried to understand it from your comments (because your plot is different from mine). Make sure everything is as should be and just use the parts you can use to solve your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download