Проблеми з запуском коду після імпортування пакетів, таких як pandas, numpy та matplotlib, можуть виникнути з різних причин, і вирішення їх може вимагати деяких досліджень та експериментів. У цій статті ми розглянемо одну з таких ситуацій і запропонуємо кілька можливих рішень.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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 apostrophes 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() |
Спроби перевстановлення імпортів, проте жоден з них не встановлюється до останньої стабільної версії.