def build_graph(df, y_title, x_title, survivor_vals): if y_title not in df.columns or x_title not in df.columns: return None cols = [x_title, y_title] + ["Survivors", "Country"] pre_cols = cols + ["Study Pop Size (N)"] post_cols = cols + ["Population"] df = df[pre_cols] df = df.dropna() max_pop = max(df["Study Pop Size (N)"].values) #round up the maximum number to the nearest hundred max_pop = int(max_pop) + 100 - int(max_pop) % 100 buckets = [100, 500, 1000, 2000, max_pop] df["Population"] = df["Study Pop Size (N)"].apply( lambda x: graph_bucket(x, buckets)) df = df[post_cols] df = df[df['Survivors'].isin(survivor_vals)] colors = get_colors() fig = go.Figure() color_ind = {'Non-survivors only': 1, 'Survivors only': 4, 'Both': 2} sizes = [5, 10, 20, 40, 60] for i in df.Survivors.unique(): s = 0 lb = 0 for ind, j in enumerate(buckets): pop_size = df[(df['Survivors'] == i) & (df['Population'] == j)].shape[0] if pop_size > 0: fig.add_trace( go.Scatter( x=df[(df['Survivors'] == i) & (df['Population'] == j)][x_title], y=df[(df['Survivors'] == i) & (df['Population'] == j)][y_title], legendgroup=i, name='{} <br> {} < Pop. Size ≤ {}'.format( i, lb, str(int(j))), mode="markers", marker=dict(color=colors[color_ind[i]], size=sizes[ind]), text=df['Country'], )) lb = j s += 1 fig.update_layout(height=550, title={ 'text': '<br>'.join( wrap('<b> {} vs. {} </b>'.format( x_title, y_title), width=26)), 'y': 0.97, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, title_font_size=20, xaxis={ 'title': x_title, 'linecolor': 'lightgrey' }, yaxis={ 'title': "Percentage with " + y_title, 'linecolor': 'lightgrey' }, legend_title='<b> Survivors <br> Population Size </b>', margin={ 'l': 40, 'b': 40, 't': 40, 'r': 10 }, hovermode='closest', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', legend={ "orientation": "h", "xanchor": "center", "y": -0.2, "x": 0.5, "itemsizing": "trace" }, modebar={ 'orientation': 'v', 'bgcolor': 'rgba(0,0,0,0)', 'color': 'lightgray', 'activecolor': 'gray' }) graph = dcc.Graph(id='interactive-graph', figure=fig) return graph
def build_state_projection(df_projections, state, country, continent, vals): location = find_smallest_scope(state, country, continent) df_projections_sub = df_projections.loc[ (df_projections.Province == state) & (df_projections.Country == country)] if continent not in ['US', 'World']: df_projections_sub = df_projections_sub.loc[(df_projections_sub.Continent == continent)] if continent == 'US': df_projections_sub = df_projections.loc[(df_projections.Country == 'US') & (df_projections.Province == state)] if continent == 'World': if country =='None': df_projections_sub = df_projections.loc[(df_projections.Continent == 'None')] #include only global world data fig = go.Figure() cols = get_cols() if (vals is not None) and (set(vals).issubset(set(cols))): colors = get_colors() for val in vals: i = cols[val] fig.add_trace(go.Scatter( name=val, showlegend=True, x=df_projections_sub['Day'], y=df_projections_sub[val].values, mode="lines+markers", marker=dict(color=colors[i]), line=dict(color=colors[i]) )) title = '<br>'.join(wrap('<b> Projections for {} </b>'.format(location), width=26)) fig.update_layout( height=550, title={ 'text': title, 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}, title_font_size=25, xaxis={'title': "Date",'linecolor': 'lightgrey'}, yaxis={'title': "Count",'linecolor': 'lightgrey'}, legend_title='<b> Values Predicted </b>', margin={'l': 40, 'b': 40, 't': 40, 'r': 10}, hovermode='closest', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', legend={ "orientation": "h", "xanchor": "center", "y": -0.2, "x": 0.5 }, modebar={ 'orientation': 'v', 'bgcolor': 'rgba(0,0,0,0)', 'color': 'lightgray', 'activecolor': 'gray' } ) graph = dcc.Graph( id='projection-graph', figure=fig ) return graph
def us_timeline(df, title, with_opt): fig = go.Figure() colors = get_colors() if with_opt: # we want to keep the baseline shortage the same color as the prev. graph # and add a new color for the new shortage col_ind = [0, 3] c = 0 i = 0 for val in df.columns: if val != "Date": fig.add_trace( go.Scatter(x=df['Date'], y=df[val].values, legendgroup=i, name=val.replace(' ', '<br>'), mode="lines+markers", marker=dict(color=colors[col_ind[c]]), line=dict(color=colors[col_ind[c]]))) c += 1 i += 1 else: state_cols = ["Shortage", "Supply", "Demand"] no_model_visual = get_no_model_visual() for i, val in enumerate(state_cols): fig.add_trace( go.Scatter(x=df['Date'], y=df[val].values, legendgroup=i, name=no_model_visual[val].replace(' ', '<br>'), mode="lines+markers", marker=dict(color=colors[i]), line=dict(color=colors[i]))) i += 1 fig.update_layout(height=550, title={ 'text': '<br>'.join( wrap(''.join(['<b> ', title, ' </b>']), width=30)), 'y': 0.96, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, title_font_size=20, xaxis={ 'title': "Date", 'linecolor': 'lightgrey' }, yaxis={ 'title': "Count", 'linecolor': 'lightgrey' }, legend_title='<b> Values Predicted </b>', margin={ 'l': 40, 'b': 40, 't': 40, 'r': 10 }, hovermode='closest', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', legend={ "orientation": "h", "xanchor": "center", "y": -0.2, "x": 0.5 }, modebar={ 'orientation': 'v', 'bgcolor': 'rgba(0,0,0,0)', 'color': 'lightgray', 'activecolor': 'gray' }) graph = dcc.Graph(id='projection-graph-vent', figure=fig) return graph