Why wont my code run after i've dowloaded and imported pandas, numpy and matplotlib?

60 Views Asked by At

Relatively new to coding and having an issue with a project im working on. The project streams data from an embedded system to thonny via usb and stores the data in a csv. Im trying to graph the data etc using matplotlib, pandas, numpy etc but found out i didn't have them installed. I installed them using the built in package manager on thonny. But when i go to run the program the error "Process ended with exit code 3." keep coming up. If i take the graphing aspect and all related imports out the code runs fine adding to the csv just no graphing. The code isn't perfect as it is anyway, but i can't really continue without fixing this issue. Any help would be appreciated, thanks

Code is below:

import serial
import csv
from time import time as current_time
from time import sleep
import pandas as pd
import numpy as np
import matplotlib as plt

ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM5"
db_list = []
print("Type CTRL + C to exit. (may take 5 seconds)")


ser.open()

start_time = current_time()

for i in range(10):
          
    microbitdata = str(ser.readline())
    
    decibel_level = microbitdata[2:]
    
    decibel_level = decibel_level.replace(" ","")#removes any spaces from the data
    
    decibel_level = decibel_level.replace("'","")#removes an opostrophies 
    
  
    decibel_level = decibel_level.replace("\\r\\n","")# replace \\r\\n with a blank space 
    
    decibel_level = decibel_level.replace("\\r","") #replace \\r with nothing
    
    decibel_level = decibel_level.replace("\\n","") #replace \\n with nothing 
    

    sleep(0.1)#break between data being added
    
    elapsed_time = round(current_time() - start_time)
    print("Elapsed time:", elapsed_time,"/db level:", decibel_level)
    sleep(1)  # Pauses the loop for 1 second
    decibel_level = int(decibel_level)
    db_list.append(decibel_level)
    
    safe = decibel_level <= 70
    potential_damage = 71<= decibel_level <= 90
    risk_of_hearing_loss = 91<= decibel_level <=119
    immediate_harm = decibel_level >= 120
        
    
    
    path = "Sound_level_data.csv"  #naming the file name 
    f = open(path, "a", newline='')#appends at the end of each line 

    writer = csv.writer(f)

    writer.writerow([elapsed_time,decibel_level])


    f.close()

#calc the trendline and plots the graph
csv = pd.read_csv('Sound_level_data.csv')
data = csv[['Time', 'Sound Level']]
x = data['elapsed_time']
y = data['decibel_level']



plt.scatter(x, y)

z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.xlabel('db Level')
plt.ylabel('Time')

print("The slope is", z[0])
print("The intercept is", z[1])
print("The equation of the line is y="+str(round(z[0],2))+"x+"+str(round(z[1],2)))

plt.savefig("graph.png")
plt.show()


Tried reinstalling the imports but none will install to the latest stable version

0

There are 0 best solutions below