Python code for reading csv data with Pandas and graphing it with Matplotlib. Since the versatility of reading csv data has been increased, it is possible to select a path and read it.
Python 3.7.3. The code is as follows.
#Module import
import os, tkinter, tkinter.filedialog, tkinter.messagebox
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#Display file selection dialog
root = tkinter.Tk()
root.withdraw()
#File extension fTyp= [("","*")] or fTyp = [("","*.csv")]
fTyp = [("csv-file","*.csv"), ("All-file","*")]
#Get folder path
iDir = os.path.abspath(os.path.dirname(__file__))
tkinter.messagebox.showinfo('Analysis program','Please select a file!')
file = tkinter.filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir)
print(file)
#File path recognition
file1 = file.replace('/', os.sep)
file_name = os.path.basename(file)
data_path = os.path.dirname(file1)
os.chdir(data_path)
print(file_name)
#read csv file
data = pd.read_csv(file_name,encoding = "utf-8",skiprows = 100)# skiprows = 5 encoding="shift_jis" or "utf-8"
print(data)
#Y axis(amplitude)Definition of
y = np.array(data)
y = y.reshape(-1,)
#X axis(Time axis)Definition of
samp = 100 #Sampling frequency(Hz)
samp_t = 1/samp
x = np.array(np.arange(0,(samp_t*len(y)),(1/samp)))
#Graphing
plt.title("Graph")
plt.xlabel("time(s)")#&Horizontal axis label
plt.ylabel("Voltage(v)")#Vertical label
plt.grid(True)#Scale display
plt.tight_layout()#All plots in a box
plt.plot(x,y)
plt.show()
If you want to know more details, please see the following article (´ ・ ω ・ `) https://kgrneer.com/python-csv/
Recommended Posts