Read Python csv data with Pandas ⇒ Graph with Matplotlib

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

Read Python csv data with Pandas ⇒ Graph with Matplotlib
Graph Excel data with matplotlib (1)
Graph Excel data with matplotlib (2)
Read json data with python
Process csv data with python (count processing using pandas)
Versatile data plotting with pandas + matplotlib
[Python] Read the csv file and display the figure with matplotlib
Graph time series data in Python using pandas and matplotlib
Read pandas data
[python] Read data
[Python] Set the graph range with matplotlib
Read CSV and analyze with Pandas and Seaborn
[Python] Read a csv file with a large data size using a generator
Data analysis with python 2
How to read a CSV file with Python 2/3
Read and format a csv file mixed with comma tabs with Python pandas
Read data with python / netCDF> nc.variables [] / Check data size
[Python] limit axis of 3D graph with Matplotlib
Read JSON with Python and output as CSV
[Python] How to read excel file with pandas
Read table data in PDF file with Python
Read CSV file: pandas
Implement "Data Visualization Design # 3" with pandas and matplotlib
Heatmap with Python + matplotlib
Band graph with matplotlib
Data visualization with pandas
Data manipulation with Pandas!
Draw a graph with matplotlib from a csv file
Plot CSV of time series data with unixtime value in Python (matplotlib)
Shuffle data with pandas
Write CSV data to AWS-S3 with AWS-Lambda + Python
Csv tinkering with python
Read Python csv file
Data analysis with Python
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
[Python] How to draw a line graph with Matplotlib
Data analysis environment construction with Python (IPython notebook + Pandas)
How to convert JSON file to CSV file with Python Pandas
[Python] How to deal with pandas read_html read error
Make a CSV formatting tool with Python Pandas PyInstaller
[Python] A memo to write CSV vertically with Pandas
Sample data created with python
Extract Twitter data with CSV
Get Youtube data with python
Graph drawing method with matplotlib
[Python] Change dtype with pandas
Python application: data visualization # 2: matplotlib
Write to csv with Python
Data analysis using python pandas
Download csv file with python
Data processing tips with Pandas
[Python] Read Japanese csv with pandas without garbled characters (and extract columns written in Japanese)
I created a stacked bar graph with matplotlib in Python and added a data label
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Notes on importing data from MySQL or CSV with Python
Notes on handling large amounts of data with python + pandas
Stylish technique for pasting CSV data into Excel with Python
How to read an Excel file (.xlsx) with Pandas [Python]
[Python] Loading csv files using pandas
Create plot animation with Python + Matplotlib
A python graphing manual with Matplotlib.