I will explain the series of steps from reading a CSV file based on WHO public data to graphing it.
The site I referred to is here.
The above site is explained on the assumption that you have a basic knowledge of Python and matplotlib.
In this article, I will summarize the points that I did not understand. Supplementary explanations are written so that even beginners can understand.
Our goal is to:
First, load the CSV.
import pandas as pd
#Data capture
df = pd.read_csv("COVID-19.csv", index_col='Date', parse_dates=['Date'])
I'm importing a library called pandas and reading a CSV file. pandas is a library that provides easy-to-use data structures and data analysis tools such as CSV loading.
For the basic contents of Japanese, you can understand what you can do by looking at here.
The arguments for pandas.read_csv () are:
There are many other parameters available, so it's confusing at first, but I think it's okay if you understand them one by one.
python
import matplotlib.dates as mdates
It refers to a library called matplotlib. matplotlib is a library that allows you to create static, animated, and interactive visualizations in just a few lines.
Among them, this time, we will use matplotlib.dates to handle time series data with the horizontal axis as the date.
You can add an alias by adding ʻasto the import statement. You can call it by writing
mdates instead of
matplotlib.dates` each time.
python
#Get color pattern
cmap = plt.get_cmap("tab10")
This time, each line needs to be color-coded in order to display multiple data on one graph. It is difficult to think about colors one by one, so it is a library that has an appropriate color pattern.
It is an image that switches the cmap to be read according to the number of lines to be displayed.
Use a lot of colors with matplotlib
python
#Define the scale to set
locator = mdates.AutoDateLocator()
formatter = mdates.ConciseDateFormatter(locator)
When creating a graph, there are various decisions such as the unit of scale. The above process can be briefly explained as follows.
python
ax = plt.gca()
With plt.gca ()
, the current Axes object is acquired and set as the object of ʻax`.
It seems that there are many people who have doubts about Axes objects and Figure objects.
The image is as follows.
object | Description | Parent-child relationship |
---|---|---|
Figure object | Outline of the figure | First parent |
Axes object | Image area with data space | Figure child class |
Axis object | Scale (mark on the axis) and scale label (character string to label the scale) | Axes child class |
If you want to understand the above in detail, the following articles may be helpful.
[Basic knowledge of matplotlib that I wanted to know early, or the story of an artist who can adjust the appearance](https://qiita.com/skotaro/items/08dc0b8c5704c94eafb9#figure-axes-axis%E3%81%AF%E9 % 9A% 8E% E5% B1% A4% E6% A7% 8B% E9% 80% A0% E3% 81% AB% E3% 81% AA% E3% 81% A3% E3% 81% A6% E3% 81 % 84% E3% 82% 8B)
#Reflect the defined axis scale on the graph
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
#drawing
ax.plot(df.index, df['Global Confirmed'], "s-", color=cmap(3))
ax.plot(df.index, df['China Confirmed'], "o-", color=cmap(2))
ax.plot(df.index, df['Global Deaths'], "s-", color=cmap(7))
ax.plot(df.index, df['China Deaths'], "o-", color=cmap(6))
#Drawing grid auxiliary lines
ax.grid()
#Log scale display of y-axis
plt.yscale('log')
#Show legend
plt.legend(['Global Confirmed', 'China Confirmed','Global Deaths', 'China Deaths'])
#Save file
plt.savefig('COVID-19.svg', bbox_inches="tight") #If svg is set to png, it can be saved as an image
The display results are as follows.
Regarding the displayed graph, the horizontal axis is the date and the vertical axis is the number of people.
If you are such a person, you can read this to understand the step-up procedure for learning machine learning efficiently.
Also, here is a reference for specific ways to learn Python.
■ COVID-19 ■ Basic knowledge of matplotlib that I wanted to know early, or the story of an artist who can adjust the appearance
Recommended Posts