Created
June 11, 2020 06:25
-
-
Save shinokada/d6f026f7271b5a079120b031cb40d21b to your computer and use it in GitHub Desktop.
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 pandas as pd | |
import numpy as np | |
import plotly.graph_objects as go | |
# confirmed_global | |
confirmed_global='https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv' | |
covid19_confirmed=pd.read_csv(confirmed_global,index_col='Country/Region') | |
confirmed_latest = covid19_confirmed.T.index.values[-1] | |
df_grouped_conf=covid19_confirmed.groupby('Country/Region').sum() | |
df_confirmed=df_grouped_conf.sort_values(by=confirmed_latest,ascending=False).head(10).drop(['Lat', 'Long'],axis=1).T | |
def multi_plot(df, title, addAll = True): | |
fig = go.Figure() | |
for column in df.columns.to_list(): | |
fig.add_trace( | |
go.Scatter( | |
x = df.index, | |
y = df[column], | |
name = column | |
) | |
) | |
button_all = dict(label = 'All', | |
method = 'update', | |
args = [{'visible': df.columns.isin(df.columns), | |
'title': 'All', | |
'showlegend':True}]) | |
def create_layout_button(column): | |
return dict(label = column, | |
method = 'update', | |
args = [{'visible': df.columns.isin([column]), | |
'title': column, | |
'showlegend': True}]) | |
fig.update_layout( | |
updatemenus=[go.layout.Updatemenu( | |
active = 0, | |
buttons = ([button_all] * addAll) + list(df.columns.map(lambda column: create_layout_button(column))) | |
) | |
], | |
yaxis_type="log" | |
) | |
# Update remaining layout properties | |
fig.update_layout( | |
title_text=title, | |
height=800 | |
) | |
fig.show() | |
multi_plot(df_confirmed, title="Logarithmic COVID-19 time series total confirmed by country") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @shinokada,
Thanks for the code!
I was trying to add annotations to this function however I'm somehow stuck. I was wondering if you have few tips on how to do it: