Created
May 18, 2020 18:27
-
-
Save gilfernandes/cc4ce72a33e0480b8cea4c93e06a2377 to your computer and use it in GitHub Desktop.
Covid 19 barchart animation code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.style as style | |
plt.rcParams.update({'font.size': 16}) | |
plt.rc('axes', titlesize=16) # fontsize of the axes title | |
plt.rc('axes', labelsize=16) # fontsize of the x and y labels | |
plt.rc('xtick', labelsize=16) | |
plt.rc('ytick', labelsize=16) | |
unique_dates = df_grouped[date].unique() | |
days_in_dataset = len(unique_dates) | |
color_map = generate_color_map() | |
def update_bar_plot(num): | |
style.use('fivethirtyeight') | |
ax.clear() | |
df_grouped_single_date = calc_bar_plot_data(unique_dates[num]) | |
num_countries = 15 | |
countries = df_grouped_single_date[country_region][:num_countries] | |
confirmed_cases = df_grouped_single_date[confirmed][:num_countries] | |
death_cases = df_grouped_single_date[deaths][:num_countries] | |
colors = [color_map[country] for country in countries] | |
title = convert_to_readable_date(unique_dates[num]) | |
ax.set_title(title) | |
for i, v in enumerate(confirmed_cases): # setting the number after the bar | |
ax.text(v + 3, i - .15, str(v), color='black') | |
h = ax.barh(countries, confirmed_cases, color=colors) | |
ax.barh(countries, death_cases, color='r') | |
ax.legend(h, countries) | |
ani = FuncAnimation(fig, update_bar_plot, frames=range(days_in_dataset), repeat=False, interval=750) | |
from IPython.display import HTML | |
HTML(ani.to_jshtml()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment