【发布时间】:2025-12-08 19:30:05
【问题描述】:
早上,我正在尝试将我的所有距离和速度值添加到一个数组中,以便我可以使用 PLT 绘制它们。但是,python 只附加 1 个值,为什么?我究竟做错了什么?下面的代码和输出: '''
import cv2
import matplotlib.pyplot as plt
import numpy as np
list=["spect2.png","spect3.png","spect4.png","spect5.png","spect6.png",
"spect7.png","spect8.png","spect9.png","spect11.png"]
for i in range(len(list)):
curr_list=list[i]
print("This is the image name reading: " + curr_list)
im = cv2.imread(curr_list)
red = [0,0,255]
X,Y = np.where(np.all(im==red,axis=2))
try:
Average_X=sum(Y)/len(Y)
except ZeroDivisionError:
print("Zero div, cant")
print(Average_X)
yellow=[0,242,255]
X,Y=np.where(np.all(im==yellow,axis=2))
try:
Average_Y=sum(Y)/len(Y)
except ZeroDivisionError:
print("Zero div, cant")
print(Average_Y)
amount_shifted=(Average_Y-Average_X)/Average_X
speed=amount_shifted*3000000
Distance=speed/22
print("Speed away from us: "+str(speed))
print("Distance away from us in mpc: "+str(Distance))
curr_dis=Distance
curr_amo=speed
amount_list=[Distance]
amount_list.append(curr_dis)
amount_list_shifted=[speed]
amount_list_shifted.append(curr_amo)
fig, ax = plt.subplots(figsize=(10, 6))
print(amount_list)
print(amount_list_shifted)
ax.scatter(amount_list, amount_list_shifted)
plt.show()
输出:
This is the image name reading: spect2.png
323.0
329.2345679012346
Speed away from us: 57906.20341703948
Distance away from us in mpc: 2632.1001553199767
This is the image name reading: spect3.png
361.0
361.0
Speed away from us: 0.0
Distance away from us in mpc: 0.0
This is the image name reading: spect4.png
304.0
Zero div, cant
361.0
Speed away from us: 562500.0
Distance away from us in mpc: 25568.18181818182
This is the image name reading: spect5.png
341.0
350.6666666666667
Speed away from us: 85043.98826979489
Distance away from us in mpc: 3865.6358304452224
This is the image name reading: spect6.png
407.0
411.0
Speed away from us: 29484.029484029485
Distance away from us in mpc: 1340.1831583649766
This is the image name reading: spect7.png
413.0
418.6441717791411
Speed away from us: 40998.82648286526
Distance away from us in mpc: 1863.583021948421
This is the image name reading: spect8.png
342.0
354.95454545454544
Speed away from us: 113636.36363636349
Distance away from us in mpc: 5165.28925619834
This is the image name reading: spect9.png
343.0
348.9032258064516
Speed away from us: 51631.71259287102
Distance away from us in mpc: 2346.896026948683
This is the image name reading: spect11.png
342.0
343.5409836065574
Speed away from us: 13517.400057521088
Distance away from us in mpc: 614.4272753418677
[614.4272753418677, 614.4272753418677]
[13517.400057521088, 13517.400057521088]
''' 如您所见,该数组仅再次显示距离和相同的距离,我如何让它添加所有距离和速度值?
【问题讨论】:
标签: python arrays numpy matplotlib astronomy