# Author:- Anurag Gupta # email:- 999.anuraggupta@gmail.com %matplotlib inline import matplotlib.pyplot as plt import pandas as pd
#### ----- Step 1 (Download data)---- URL_DATASET = r'https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv' df1 = pd.read_csv(URL_DATASET) # print(df1.head(3)) # Uncomment to see the dataframe
#### ----- Step 2 (Select data for India)---- df_india = df1[df1['Country'] == 'India'] print(df_india.head(3))
#### ----- Step 3 (Plot data)---- # Increase size of plot plt.rcParams["figure.figsize"]=20,20 # Remove if not on Jupyter # Plot column 'Confirmed' df_india.plot(kind = 'bar', x = 'Date', y = 'Confirmed', color = 'blue')
ax1 = plt.gca() df_india.plot(kind = 'bar', x = 'Date', y = 'Deaths', color = 'red', ax = ax1) plt.show()
%matplotlib notebook # Author:- Anurag Gupta # email:- 999.anuraggupta@gmail.com import pandas as pd import matplotlib.pyplot as plt import matplotlib.animation as animation from time import sleep
#### ---- Step 1:- Download data URL_DATASET = r'https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv' df = pd.read_csv(URL_DATASET, usecols = ['Date', 'Country', 'Confirmed']) # print(df.head(3)) # uncomment this to see output
#### ---- Step 2:- Create list of all dates list_dates = df['Date'].unique() # print(list_dates) # Uncomment to see the dates
#### --- Step 3:- Pick 5 countries. Also create ax object fig, ax = plt.subplots(figsize=(15, 8)) # We will animate for these 5 countries only list_countries = ['India', 'China', 'US', 'Italy', 'Spain'] # colors for the 5 horizontal bars list_colors = ['black', 'red', 'green', 'blue', 'yellow']
### --- Step 4:- Write the call back function # plot_bar() is the call back function used in FuncAnimation class object defplot_bar(some_date): df2 = df[df['Date'].eq(some_date)] ax.clear() # Only take Confirmed column in descending order df3 = df2.sort_values(by = 'Confirmed', ascending = False) # Select the top 5 Confirmed countries df4 = df3[df3['Country'].isin(list_countries)] # print(df4) # Uncomment to see that dat is only for 5 countries sleep(0.2) # To slow down the animation # ax.barh() makes a horizontal bar plot. return ax.barh(df4['Country'], df4['Confirmed'], color= list_colors)
### --- Step 6:- Save the animation to an mp4 # Place where to save the mp4. Give your file path instead path_mp4 = r'C:\Python-articles\population_covid2.mp4' # my_anim.save(path_mp4, fps=30, extra_args=['-vcodec', 'libx264']) my_anim.save(filename = path_mp4, writer = 'ffmpeg', fps=30, extra_args= ['-vcodec', 'libx264', '-pix_fmt', 'yuv420p']) plt.show()