Morning, I am trying to add all my values of Distance and Speed to an array, so I can plot them with PLT. However, python is only appending 1 value, why? What am I doing wrong? Code and output below: ”’
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()
output:
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]
”’ AS you can see the array is ONLY showing Distance and same distance again, HOW do I get it to add all the Distance and Speed values?
Advertisement
Answer
In every loop iteration, you very specifically erase your previous list, and replace it with two copies of the current data. I’ll focus on only one variable:
curr_dis=Distance # the two variables are now the same value amount_list=[Distance] # over-write amount_list with only the current value amount_list.append(curr_dis) # append another copy of the current value
You have two things to upgrade in your programming skills:
(1) Learn basic debugging. If nothing else, insert strategic print
statements to display variables of interest, and to trace your program flow.
(2) Learn basic techniques with data structures. In this case, you need to repeat your tutorial materials on handling a sequence (string, list, tuple, etc.). To accumulate a list the basic pattern is
a = [] while ... : create a new_value for the list a.append(new_value) print(a)
Can you finish from here?