Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created June 11, 2020 06:25
Show Gist options
  • Save shinokada/d6f026f7271b5a079120b031cb40d21b to your computer and use it in GitHub Desktop.
Save shinokada/d6f026f7271b5a079120b031cb40d21b to your computer and use it in GitHub Desktop.
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")
@poltys
Copy link

poltys commented Aug 27, 2020

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:

   
  def create_annotations(column):
      return dict(x=pd.to_datetime(df[column].idxmax()),
                          y=df[column].max(),
                          xref="x", yref="y",
                          text="Max:<br> %.3f" % df[column].max(),
                          ax=0, ay=-40)

    button_all = dict(label = 'All',
                      method = 'update',
                      args = [{'visible': df.columns.isin(df.columns),
                               'title': 'All',
                               'showlegend':True,
                               'annotations':df.columns.isin(df.columns),
                               }])
                               
    def create_layout_button(column):
        return dict(label = column,
                    method = 'update',
                    args = [{'visible': df.columns.isin([column]),
                             'title': column,
                             'showlegend': True,
                             'annotations': df.columns.isin([column]),
                             }])
fig.update_layout(
    updatemenus=[go.layout.Updatemenu(
        active = 0,
        buttons = ([button_all] * addAll) + list(df.columns.map(lambda column: create_layout_button(column)))
        )
    ],  
)
fig.update_layout(
    title_text=title,
    height=800,
    # annotations= annotations,
    annotations = list(df.columns.map(lambda column: create_annotations(column))),
    updatemenus=[
                 dict(
                     direction="down",
                      pad={"r": 10, "t": 10},
                      showactive=True,
                      x=0,
                      xanchor="left",
                      y=1.15,
                      yanchor="top",)],
    )`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment