Skip to main content
 首页 » 编程设计

用于查找点和曲线之间的最小距离的 Python 代码

2024年10月01日10lidabo

我有一些数据,并且绘制了幅度与波长的关系图(蓝点)。然后我有一些代码从文件中读取模型恒星种群,并将其绘制在同一张图上(粉红色的线)。在此代码中,有一个可以调整的刻度,可以将这条线在图表上向上或向下移动。到目前为止,我一直在改变比例尺,使这条线尽可能靠近我的点,但我想写一些代码来计算我的点和点之间的总距离的比例尺值该行是最小的。到目前为止,这是我的代码:

#Import modules 
 
from math import * 
import numpy as np 
import matplotlib.pyplot as plt 
 
# Specify data 
 
wavelength =  
np.array([357.389,445.832,472.355,547.783,620.246,752.243,891.252,2164.089]) 
magnitude =  
np.array([24.0394,23.1925,23.1642,22.4794,21.7496,20.9047,20.4671,19.427]) 
 
# Create Graph 
 
#plt.scatter(wavelength, magnitude) 
#plt.ylim([25,18]) 
#plt.xlim([300,2200]) 
#plt.xlabel('wavelength (nm)') 
#plt.ylabel('magnitude') 
#plt.title('object 1') 
#plt.show() 
#plt.close() 
 
#now - here is some code that reads a model stellar population model from a  
file 
 
lines = open('fig7b.dat').readlines() 
 
wavelengths, luminosities = [],[] 
 
for l in lines: 
    s = l.split() 
    wl = s[0] 
    old = s[-1] 
    if '#' not in wl: 
        wavelengths.append(float(wl)) #wavelength in angstroms  
        luminosities.append(float(old)) #luminosities are in log units! 
 
 
scale = 3.5 
c=3.e8  
wavelengths = np.array(wavelengths) 
nus = c/(wavelengths*1.e-10)  
luminosities = np.array(luminosities) + scale  
 
luminosity_density = np.log10(((10**luminosities)*wavelengths)/nus) 
 
#plt.plot(wavelengths,luminosity_density) 
#z = 1.0 
#plt.plot(wavelengths*(1+z),luminosity_density,color='r') 
 
#plt.axis([900, 10000, 25,31]) 
#plt.savefig('sed.png') 
#plt.show() 
#plt.close() 
 
Mpc_to_cm = 3.086e24 #convert Mpc to cm  
z  = 0.3448 #our chosen redshift 
D_L = 1841.7 * Mpc_to_cm  
 
#remember luminosity_density is logged at the moment 
flux_density = (10**luminosity_density) * (1+z) / (4*pi*D_L**2) #units will  
be erg/s/cm^2/Hz 
 
#now turn that into an AB magnitude - goes back to log 
AB_mag = -2.5*np.log10(flux_density) - 48.6 
 
#try plotting your photometry on here and play with z and D_L  
plt.plot(wavelengths*(1+z),AB_mag,color='pink') 
plt.scatter(wavelength*10., magnitude,color='cornflowerblue') 
plt.axis([900, 25000, 30,18]) 
plt.xlabel('wavelength') 
plt.ylabel('magnitude') 
plt.title('object 1') 
plt.savefig('sed_ab.png') 
plt.show() 

它给出了一个看起来像这样的图表:

打印最佳比例值也很有帮助。 一般来说,我对 python 和编程还很陌生,粉红色的线不是一个简单的方程式(在我得到的文件中,它由很多数据点组成)所以我有点卡住了。抱歉,如果我没有使用正确的语言来描述我的问题,并且对于长代码 - 很多评论都是我的主管在我有单独的情节之前保留的以前的情节。 (我使用的是 python 2.7)

fig7b.dat 的链接:https://drive.google.com/open?id=0B_tOncLLEAYsbG8wcHJMYVowOXc

请您参考如下方法:

首先,根据曲线数据创建一个点列表,使每个点对应于第一个点列表(每个对应的点对将具有相同的 X 坐标,即相同的波长)。

那么这两组点之间的最小距离就是:(sum(points2)-sum(points1))/len(points1)。

看下面的例子

points1 = [1.1, 1.4, 1.8, 1.9, 2.3, 1.7, 1.9, 2.7] 
points2 = [8.4, 3.5, 2.9, 7.6, 0.1, 2.2, 3.3, 4.8] 
 
def min_distance(first,second): 
  assert len(first) == len(second)  # must have same size 
  result = (sum(second) - sum(first)) / len(first) 
  return result 
 
print("Adding this value to the first series of points") 
print("will provice minimum distance between curves") 
print(min_distance(points1,points2)) 

运行此命令将打印值 2.25。如果将 2.25 添加到 points1 的所有值,您将获得两组点之间的最小可能距离(在此为 62.36特殊情况)。

在您的问题中,points1 将是 magnitude 数组。 points2 将是 fig7b.dat 中与波长对应的点。

假设您希望最小化点和曲线之间的平方和。它还假设距离是垂直测量的(这就是为什么您需要提取具有相应波长的点)。