[英]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. 我为redshift设置了一组值,并为Universe的减速度参数设置了相应的值。 The plot is given:
该图给出:
Now I need the value of z accurately from the graph for q=0. 现在,我需要从图中准确得出q = 0时的z值。 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.
由于我对python的了解不多,所以我什至无法尝试纠正所有这些问题。
When I tried the command: 当我尝试命令时:
z1 = interp1d(z,q,0)
the result was: 结果是:
scipy.interpolate.interpolate.interp1d object at 0x051899F0
scipy.interpolate.interpolate.interp1d对象位于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). 我这里只有Python 3 ,因此如果您在理解某些内容时遇到任何问题,只问一下即可(更改了
print
和input
功能部分)。
You did not define the H02
so I just stated it was equal to H0
. 您没有定义
H02
所以我只是说它等于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. 您需要记住的是,
interp1D
将只能在您提供的数据区域内插。 So if q
is within 10
and 100
you cannot interpolate q=0
. 因此,如果
q
在10
到100
,则无法插值q=0
。
I used a redshift of 10 (did not know what to use although the subject seemed interesting). 我使用了10的redshift(尽管这个主题看起来很有趣,但不知道该用什么)。 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.
确保一切正常,并使用可以用来解决问题的部件。
The corrected program(Previously corrected by @armatita
) is: (where I put zf=2
in the output) 校正后的程序(先前由
@armatita
校正)为:(在输出中输入zf=2
)
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 #I donot use a term H02
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:'))
print('Red shift Scale factor Hubble parameter q value')
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)))*H0**2)**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(0))
plot(z, q)
plot([z1(0), z1(0)], [0, 0], c='r')
plot([0, z1(0)], [0, 0], 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()
Previously corrected by @armatita 先前由@armatita纠正
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.