[Python] Introduction to graph creation using coronavirus data [For beginners]

Introduction

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.

Premise

environment

goal

Our goal is to:

Preparation

Data reading

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.

Supplement

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.

Data display

Import matplotlib

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 writingmdates instead of matplotlib.dates` each time.

Get color pattern

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

Definition of scale

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.

Reflect the defined scale information on the graph

python


ax = plt.gca()

With plt.gca (), the current Axes object is acquired and set as the object of ʻax`.

Supplement What is an Axes object?

It seems that there are many people who have doubts about Axes objects and Figure objects.

The image is as follows. fig_map.png

Source: Official document

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)

Drawing a graph

#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. COVID-19.png

Regarding the displayed graph, the horizontal axis is the date and the vertical axis is the number of people.

Finally

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.

Reference information

COVID-19Basic knowledge of matplotlib that I wanted to know early, or the story of an artist who can adjust the appearance

Recommended Posts

[Python] Introduction to graph creation using coronavirus data [For beginners]
Introduction to Programming (Python) TA Tendency for beginners
Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)
~ Tips for beginners to Python ③ ~
Introduction to Python For, While
Let's analyze Covid-19 (Corona) data using Python [For beginners]
[For beginners] How to study Python3 data analysis exam
[Introduction to Python] How to write repetitive statements using for statements
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
Excel graph creation using python xlwings
An introduction to Python for non-engineers
[Pandas] I tried to analyze sales data with Python [For beginners]
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
Beginners read "Introduction to TensorFlow 2.0 for Experts"
How to use "deque" for Python data
An introduction to Python for machine learning
Introduction to Discrete Event Simulation Using Python # 2
WebApi creation with Python (CRUD creation) For beginners
[Introduction to Data Scientists] Basics of Python ♬
An introduction to Python for C programmers
Python beginners publish web applications using machine learning [Part 2] Introduction to explosive Python !!
Memo # 4 for Python beginners to read "Detailed Python Grammar"
The fastest way for beginners to master Python
An introduction to object-oriented programming for beginners by beginners
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Memo # 1 for Python beginners to read "Detailed Python Grammar"
[Introduction to Python] How to handle JSON format data
Try to calculate RPN in Python (for beginners)
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 6 for Python beginners to read "Detailed Python Grammar"
How to make Python faster for beginners [numpy]
Memo # 5 for Python beginners to read "Detailed Python Grammar"
[Introduction for beginners] Working with MySQL in Python
[For beginners] Introduction to vectorization in machine learning
Understand Python for Pepper development. -Introduction to Python Box-
Let's analyze Covid-19 (Corona) data using Python [For beginners]
[Introduction to Python] How to get the index of data with a for statement
Data analysis using Python 0
Introduction to Python language
Introduction to OpenCV (python)-(2)
Python Beginner's Guide (Introduction)
OpenCV for Python beginners
[BigQuery] How to use BigQuery API for Python -Table creation-
[For beginners] How to use say command in python!
How to convert Python # type for Python super beginners: str
Reading Note: An Introduction to Data Analysis with Python
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
[Python] How to use the graph creation library Altair
Write data to KINTONE using the Python requests module
[Introduction for beginners] Reading and writing Python CSV files
Python Exercise for Beginners # 1 [Basic Data Types / If Statements]
[Introduction to Python] How to stop the loop using break?
Python # How to check type and type for super beginners
[Introduction to Python] How to write conditional branches using if statements
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
Introduction to Python Django (2) Win
Python for Data Analysis Chapter 4