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.
Prepare two csv files using numbers. The contents of the first file (hoge)
The second file (hogehoge) is
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….
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
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
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
The first argument "CSVfile / hoge.csv" is the csv file to read
The second argument usescols = [1,2] specifies the column to be used this time (in this case, the 2nd and 3rd lines of the csv file)
The third argument skiprows = 2 specifies the line to skip (in this case, skip 2 lines and read the value from the 3rd line)
The fourth argument names = ["x", "y"] names the two specified columns
Fifth argument encoding = "utf8" specifies character code
It has become.
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.
plt.figure () creates a window without a graph. Its argument, figsize = (15, 6), specifies the size of the graph.
plt.xticks () is a function related to memory setting, and you can specify the memory width of the axis by putting np.arange () in the first argument. In the case of plt.xticks (np.arange (0, 180 + 1, 5)), memory with 5 intervals is specified in the range of 0 ≤ value ≤ 180 on the x-axis. Also, the reason why the second argument of np.arange () is 180 + 1 is that the definition of np.arange () is 1st argument ≤ value <2nd argument, and if the 2nd argument is 180 Since 180 is not displayed in the memory, it is +1.
plt.xlabel () labels the axis, the label name is specified in the first argument and the font size is specified in the second argument.
plt.xlim () sets the range of axis values, this time specifying the axis range so that the x-axis is 0 ≤ x ≤ 180 and the y-axis is -1 ≤ y ≤ 1. ..
plt.grid () can specify the presence or absence of grid lines (thin grid lines) by putting true or false in the argument. This time, I put true and displayed it.
plt.plot () is an instruction to draw a graph. This time, x and y of the csv file read by specifying data1 ["x"] and data1 ["y"] as arguments are set on the horizontal axis of the graph. I set it on the vertical axis. If you want to overlay graphs, just write the graphs you want to overlay with plt.plot ().
When you actually run this code on jupyter notebook
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