[Python] Read the csv file and display the figure with matplotlib

Data used this time

Somehow I used the average temperature data of Saitama and Iwate prefectures. Download here

Read csv file with NumPy

Load a csv file using NumPy The function to use when loading is loadtxt Since a numpy array is returned, prepare a variable to hold it (data_set this time)


import numpy as np

data_set = np.loadtxt(
    fname="sampleData.csv", #Path and name of the file to read
    dtype="float", #Read with float
    delimiter=",", #Since it is csv, separate it with a comma
)

View diagram with matplotlib

This time with matplotlib, the data read earlier is displayed as a scatter plot.

import matplotlib.pyplot as plt

#Draw a scatter plot
#Please note that the figure is not displayed just by drawing.
for data in data_set:
    plt.scatter(data[0], data[1])

plt.title("correlation") #title
plt.xlabel("Average Temperature of SAITAMA") #x-axis label
plt.ylabel("Average Temperature of IWATE") #y-axis label
plt.grid() #Draw a grid line(You don't have to draw)

plt.show() #The figure is displayed by using the show function. It is not displayed unless it is written.

Here is the completed code and diagram

import numpy as np
import matplotlib.pyplot as plt

data_set = np.loadtxt(
    fname="sampleData.csv",
    dtype="float",
    delimiter=",",
)

#Draw a scatter plot → use scatter
#Take out line by line and draw
#plt.scatter(x coordinate value,y coordinate value)
for data in data_set:
    plt.scatter(data[0], data[1])

plt.title("correlation")
plt.xlabel("Average Temperature of SAITAMA")
plt.ylabel("Average Temperature of IWATE")
plt.grid()

plt.show()

scatter.png

next time#

Next, I will draw a regression line on this scatter plot

reference#

・ Reading csv https://www.sejuku.net/blog/73071 ・ Drawing a scatter plot https://pythondatascience.plavox.info/matplotlib/%E6%95%A3%E5%B8%83%E5%9B%B3

Recommended Posts

[Python] Read the csv file and display the figure with matplotlib
Read the csv file and display it in the browser
Read the VTK file and display the color map with jupyter.
Read CSV file with python (Download & parse CSV file)
Let's read the RINEX file with Python ①
Read Python csv file
How to read a CSV file with Python 2/3
Read Python csv data with Pandas ⇒ Graph with Matplotlib
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
Read and write csv file
Read csv with python pandas
Download csv file with python
I tried to touch the CSV file with Python
Read json file with Python, format it, and output json
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Extract the xz file with python
[Python] font family and font with matplotlib
[Python] Write to csv file with Python
Output to csv file with Python
Python CSV file reading and writing
Reading and writing CSV with Python
Read the csv file with jupyter notebook and write the graph on top of it
Read a file in Python with a relative path from the program
[Python] Read a csv file with a large data size using a generator
Calculate and display standard weight with python
[Python] Set the graph range with matplotlib
Check the existence of the file with python
Display Python 3 in the browser with MAMP
Read and write csv files with numpy
Read the file line by line in Python
Read the file line by line in Python
Read Python csv and export to txt
Display markers above the border with matplotlib
Match the colorbar to the figure with matplotlib
[pandas] .csv file reading and display method
[python] Read html file and practice scraping
[Python] Read the specified line in the file
Read CSV and analyze with Pandas and Seaborn
[Automation] Read mail (msg file) with Python
[Python] How to specify the window display position and size of matplotlib
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
[Implementation example] Read the file line by line with Cython (Python) from the last line
[Python] How to read excel file with pandas
Interactively display algebraic curves with Python and Jupyter
Read table data in PDF file with Python
[Python3] Read and write with datetime isoformat with json
Solving the Lorenz 96 model with Julia and Python
Draw a graph with matplotlib from a csv file
Archive and compress the entire directory with python
Convert the character code of the file with Python3
Read line by line from a file with Python
Example of reading and writing CSV with Python
Format and display time series data with different scales and units with Python or Matplotlib
Execute raw SQL using python data source with redash and display the result
Python --Read data from a numeric data file and find the multiple regression line.
Let's play with Python Receive and save / display the text of the input form
A Python script that reads a SQL file, executes BigQuery and saves the csv
Fourier transform the wav file read by Python, reverse transform it, and write it again.
Read CSV file: pandas
Heatmap with Python + matplotlib