Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Matplotlib to draw epidemic Diagram

Shulou Source: shulou.com Published: 2022-06-01 09:59:04 09月26日 Update

This article mainly introduces how to use Matplotlib to draw a dynamic map of the epidemic, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.

Use matplotlib to map the daily death toll of COVID-19 in four countries in the past six months. The API interface for obtaining data is:

Https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv

The logic of data processing is as follows. Refer to the processing logic pushed a few days ago:

Df = pd.read_csv ('a.csvents, delimiter=',', header='infer')

Df_interest = df.loc [df ['Country/Region'] .isin ([' United Kingdom', 'US',' Italy', 'Germany']) & df [' Province/State'] .isna ()]

Df_interest.rename (index=lambda x: df_interest.at [x, 'Country/Region'], inplace=True)

Df1 = df_interest.transpose ()

Df1 = df1.drop (['Province/State',' Country/Region', 'Lat',' Long'])

Df1 = df1.loc [(df1! = 0) .any (1)]

Df1.index = pd.to_datetime (df1.index)

To make it easier for you to understand, show some of the data from df_interest:

Some of the data of the collated df1:

It can be seen that as of yesterday, the number of COVID-19 deaths in the United States had reached 219286.

The logic of drawing a line chart animation is as follows:

Color = ['red',' green', 'blue',' orange']

Fig = plt.figure ()

Plt.xticks (rotation=45, ha= "right", rotation_mode= "anchor")

Plt.subplots_adjust (bottom = 0.2, top = 0.9)

Plt.ylabel ('No of Deaths')

Plt.xlabel ('Dates')

# this function is a callback function for drawing animation

# there is one and only one parameter I, which indicates the number of frames and the line of the df1

Def showLine (I):

Plt.legend (df1.columns)

P = plt.plot (df1 [: I] .index, df1 [: I] .values)

For i in range (0pr 4):

P [I] .set _ color (Color [I])

This is the only line for drawing the animation. Call FuncAnimation, whose second argument is the function showLine defined above:

Animator = ani.FuncAnimation (fig, showLine, interval = 10)

Plt.show ()

The drawn line chart animation is as follows:

Thank you for reading this article carefully. I hope the article "how to draw an epidemic Map using Matplotlib" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Animation data article function logic epidemic situation number of people parameters lines parts reference processing line value interest half a year only at the same time country interface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology macOS NVidia MariaDB