Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python visual artifact Plotly to dynamically demonstrate the changing trend of global epidemic situation

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use Python visualization artifact Plotly dynamic demonstration global epidemic trend, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for you, there are people who need this aspect can learn, I hope you can gain something.

Visualizing artifacts with Python Plotly

Dynamic demonstration of global epidemic trends

Good morning, my name is Lemonbit.

Recently, there are many contents to visualize epidemic data. Today, I will use Python visualization to apply Plotly to visualize the development of epidemic situation abroad. In the form of actual project combat, while analyzing and understanding the trend of foreign epidemic situation, we will deepen our learning and application of Plotly.

Before we get started, let's take a look at some of the final renderings, and if you're interested, keep looking.

data sources

At present, the epidemic situation in China has been gradually controlled, and various indicators have begun to improve obviously. However, the situation abroad does not seem too optimistic, and the existing homework has not been copied well. Therefore, this time we mainly analyze the development of foreign epidemic situation visually.

The epidemic data comes from the open source project Akshare. Because the data obtained by this project is sometimes unstable, connection failure may be encountered, so here I provide the saved data for everyone to use.

preparations

As usual, let me introduce the environment in which I operate first.

Mac system

Anaconda(Python 3.7)

Jupyter Notebook

Python libraries used this time include akshare, pandas, plotly, etc., imported as follows:

import akshare as ak import pandas as pd import plotly from plotly.offline import iplot, init_notebook_mode import plotly.express as px from datetime import datetime init_notebook_mode()

Next, we read the data we have obtained, and the data we have saved is as of March 7.

#Get data from akshare # df_all_history = ak.epidemic_history() #Get data from csv file df_all_history = pd.read_csv('epidemic_all_20200307.csv',index_col=0) df_all_history

Some of the formatting needs to be adjusted for the data obtained from above. For dates, we organize two columns of data here, one column for dates in time format ('date ') and one column for dates in string format ('dates'). The reason for this is that we need to use these two formats separately later.

df_all = df_all_history #Save date in string format as a separate column df_all['dates'] = df_all_history['date'] #Convert string format dates to date format df_all['date'] = pd.to_datetime(df_all['date'])

Access to epidemic data abroad

The above data are global data. We can eliminate those belonging to China and get foreign data.

#Foreign countries, by country statistics df_oversea = df_all.query("country!= 'China'") df_oversea.fillna(value="", inplace=True) df_oversea

First, use plotly express to see the overall trend of foreign epidemic sub-countries.

#Foreign countries, by country statistics df_oversea = df_all.query("country!= 'China'") df_oversea.fillna(value="", inplace=True) df_oversea

As can be seen from the above figure, the development trend of epidemic situation abroad is obvious in most countries from February 10. Therefore, we focus on analyzing the situation after this period later.

#Existing data demos start February 10, 2020 df_overseadf_oversea_recent = df_oversea.set_index('date') df_oversea_recentdf_oversea_recent = df_oversea_recent['2020-02-10':] df_oversea_recent

Since the data for some countries are not recorded since February 10,2020, supplementary data will be required. We can manually create a new excel data sheet and fill in the value of the supplementary date with 0.

I mainly supplement Iran's data here, because Iran is developing too fast and must be included in the scope of analysis. Other countries, if there is a need to supplement, can continue to improve the follow-up.

#Due to some countries, the data does not start from February 10, 2020, so supplementary data is needed, and the value is 0. #The data is supplemented in excel form, read here df_oversea_buchong = pd.read_excel('epidemic_buchong.xlsx') df_oversea_buchong['dates'] = df_oversea_buchong['date'].apply(lambda x:x.strftime('%Y-%m-%d')) df_oversea_buchong.set_index('date', inplace=True) df_oversea_buchong.fillna(value="", inplace=True) print(df_oversea_buchong.info()) df_oversea_buchong

Once we have the data we need to supplement, we can combine these two parts of the data and analyze them together.

#Consolidation of supplementary data df_oversea_recentdf_oversea_recent_new = df_oversea_recent.append(df_oversea_buchong) df_oversea_recent_new.sort_index(inplace=True) df_oversea_recent_new

Once we have the merged data, we first visualize the changes using bubble plots, in this case plotly express scatter plots.

Plotly express has now been merged into plotly, and I personally feel that it is better to use cufflinks than plotly native content.

#Consolidation of supplementary data df_oversea_recentdf_oversea_recent_new = df_oversea_recent.append(df_oversea_buchong) df_oversea_recent_new.sort_index(inplace=True) df_oversea_recent_new

It can be clearly seen from the dynamic chart above that at present, among overseas countries, South Korea, Iran and Italy are the most serious. Among the three countries, Iran and Italy are more obvious than South Korea in terms of growth trend. At present, South Korea's growth has slowed down, while Iran and Italy are still in the process of rapid growth. The follow-up situation is not optimistic.

In addition, in this chart, there are several other countries worth paying attention to. Japan, except for the diamond, from the data point of view, the growth of its own territory is still in a slightly better range. On the contrary, Germany, France, Spain, personally feel that has become a gradual growth trend, have to guard against.

Moreover, since people flow freely between EU countries, it now seems that the whole EU is likely to become the hardest hit area of the epidemic, with huge impact.

The trend of these countries in the lower left corner of the above chart can be seen closely. The trend is as follows. In this way, it will be more obvious for Germany, France and Spain.

Here, there is another country, that is, the United States. Although it seems that the United States is OK from the data and growth situation, I am afraid the actual situation is much worse.

The above is a bubble chart to demonstrate the process of change, we can also demonstrate in the form of a histogram, the effect is as follows:

Regarding the above renderings, you can study them yourself. Welcome to communicate.

It should be noted that the code runs in Jupyter Notebook, and if it runs in an IDE such as PyCharm, you need to modify the code slightly.

Finally, thanks again to the open source project Akshare for providing the data interface.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report