Read the csv file with jupyter notebook and write the graph on top of it

Preface

I recently had the opportunity to use a jupyter notebook, so I will summarize what I learned there. What I did was read only the necessary lines in the csv file and have the graph drawn.

Csv file to prepare

Prepare two csv files using numbers. The contents of the first file (hoge) 2020-10-28 19.15.24.png

The second file (hogehoge) is 2020-10-30 23.51.21.png

In hoge, the values are arranged in 10 increments from 0 to 180 in the second column, and the cos and sin values corresponding to the values in the second column are entered in the third and fourth columns. hogehoge has the value of tan in the third column. If you save it as it is, it will be saved as a numbers file, so rewrite these files to a csv file and save it. With numbers, you can create a csv file from File → Export → CSV…. 2020-10-28 19.27.27.png

Directory structure

The directory structure of the prepared csv file and the created ipynb file (file name is sample.ipynb) is as follows.

sample
├── CSVfile
│   ├── hoge.csv
│   └── hogehoge.csv
└── sample.ipynb

code

import First, import the library and module used this time. The ones to be used are as follows.


import matplotlib.pyplot as plt
import matplotlib.ticker as ptick
from matplotlib.pyplot import figure
import japanize_matplotlib
import numpy as np
import pandas as pd

Read csv file

Next, read the csv file. Reads the values of row1 and row2 of hoge and the values of row1 and row2 of hogehoge.

#Read csv file
#usecols: Specify columns to use skiprows: Skip rows names: Name columns to retrieve
data1 = pd.read_csv("CSVfile/hoge.csv", usecols=[1,2], skiprows=2, names=["x", "y"], encoding="utf8")
data2 = pd.read_csv("CSVfile/hogehoge.csv", usecols=[1,2], skiprows=1, names=["x", "y"], encoding="utf8")

pd.read_csv () is a function that reads csv. Looking at data1

It has become.

Drawing a graph

Next, the graphs for the columns extracted from the two loaded csv files are overlaid and drawn.

plt.figure(figsize=(15, 6))#Graph size
#Axis memory width
plt.xticks(np.arange(0, 180+1, 5))
plt.yticks(np.arange(-1, 1+0.1, 0.1))
#Axis label
plt.xlabel('x', fontsize=15)
plt.ylabel('y', fontsize=15)
#Axis range
plt.xlim([0,180])
plt.ylim([-1,1])
plt.grid(True)#grid settings
#Drawing a graph
plt.plot(data1["x"], data1["y"])
plt.plot(data2["x"], data2["y"])#Overlay graphs

Let's see what we are doing from above.

When you actually run this code on jupyter notebook

2020-11-03 14.29.39.png

As a result, you can see that the cos (x) value of hoge.csv and the tan (x) value of hogehoge.csv are drawn in the graph as set.

Recommended Posts

Read the csv file with jupyter notebook and write the graph on top of it
Read and write csv file
Read the csv file and display it in the browser
[Python] Read the csv file and display the figure with matplotlib
Read the VTK file and display the color map with jupyter.
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
Read and write csv files with numpy
Display the graph of tensorBoard on jupyter
Scrap the published csv with Github Action and publish it on Github Pages
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Read the file with python and delete the line breaks [Notes on reading the file]
Read CSV file with Python and convert it to DataFrame as it is
Install matplotlib and display graph on Jupyter Notebook
Fourier transform the wav file read by Python, reverse transform it, and write it again.
Monitor the training model with TensorBord on Jupyter Notebook
Read json file with Python, format it, and output json
The story of launching python2.x jupyter notebook using docker (crushed on Saturday and Sunday)
Read and write a file
Write and read a file
[pyqtgraph] Add region to the graph and link it with the graph region
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
How to see the contents of the Jupyter notebook ipynb file
Write charts in real time with Matplotlib on Jupyter notebook
Formatting with autopep8 on Jupyter notebook
[Python] Write to csv file with Python
Simply view the Jupyter notebook file
Format the CSV file of "National Holiday" of the Cabinet Office with pandas
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Read and format a csv file mixed with comma tabs with Python pandas
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)
Predict the amount of electricity used in 2 days and publish it in CSV
The one that divides the csv file, reads it, and processes it in parallel
Edit the file of the SSH connection destination server on the server with VS Code
I vectorized the chord of the song with word2vec and visualized it with t-SNE
Convert the spreadsheet to CSV and upload it to Cloud Storage with Cloud Functions
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
Read CSV file with python (Download & parse CSV file)
Check the existence of the file with python
Use Jupyter Lab and Jupyter Notebook with EC2
Try SVM with scikit-learn on Jupyter Notebook
Let's read the RINEX file with Python ①
Clone the github repository on jupyter notebook
GPU check of PC on jupyter notebook
Linking python and JavaScript with jupyter notebook
Read CSV and analyze with Pandas and Seaborn
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
Various ways to read the last line of a csv file in Python
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
To output a value even in the middle of a cell with Jupyter Notebook
I want to write an element to a file with numpy and check it.
Scraping the rainfall data of the Japan Meteorological Agency and displaying it on M5Stack
How to read a serial number file in a loop, process it, and graph it
How to install Fast.ai on Alibaba Cloud GPU and run it on Jupyter notebook
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Get the stock price of a Japanese company with Python and make a graph
[Python] Automatically read prefectural information on the new coronavirus from the PDF of the Ministry of Health, Labor and Welfare and write it in a spreadsheet or Excel.
How to read a CSV file with Python 2/3
Scraping the holojour and displaying it with CLI
Read Python csv data with Pandas ⇒ Graph with Matplotlib