Skip to content

Instantly share code, notes, and snippets.

@gilfernandes
Created May 18, 2020 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilfernandes/cc4ce72a33e0480b8cea4c93e06a2377 to your computer and use it in GitHub Desktop.
Save gilfernandes/cc4ce72a33e0480b8cea4c93e06a2377 to your computer and use it in GitHub Desktop.
Covid 19 barchart animation code
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