Example #1
0
def choropleth_drug(data: CardLiveData, world: geopandas.GeoDataFrame):
    df_geo = data.sample_counts(['geo_area_code',
                                 'geo_area_name_standard']).reset_index()

    # Remove N/A from counts so it doesn't mess with colors of map
    df_geo = df_geo[~df_geo['geo_area_name_standard'].str.contains('N/A')]

    if df_geo.empty or df_geo['count'].sum() == 0:
        fig = EMPTY_MAP
    else:
        fig = px.choropleth(
            df_geo,
            geojson=world,
            locations='geo_area_code',
            featureidkey='properties.un_m49_numeric',
            color='count',
            color_continuous_scale='YlGnBu',
            hover_data=['geo_area_name_standard'],

            # Off-center to avoid a color fill issue with Antarctica
            # where the oceans get filled instead of the continent
            center={
                'lat': 0,
                'lon': 0.01
            },
            title='Samples by geographic region',
        )

        fig.update_traces(hovertemplate=(
            '<b style="font-size: 125%;">%{customdata[0]}</b><br>'
            '<b>Count:</b>  %{z}<br>'))

    fig.update_layout(
        margin={
            "r": 0,
            "t": 35,
            "l": 0,
            "b": 0
        },
        coloraxis_colorbar=dict(
            title='Count',
            yanchor='middle',
            y=0.5,
            len=1,
            lenmode='fraction',
            outlinecolor='black',
            outlinewidth=1,
            bgcolor='white',
            thickness=25,
            thicknessmode='pixels',
        ),
    )

    return fig
Example #2
0
def totals_figure(data: CardLiveData, type_value: str,
                  color_by_value: str) -> go.Figure:
    type_col = TOTALS_COLUMN_SELECT_NAMES[type_value]
    color_col = TOTALS_COLUMN_SELECT_NAMES[color_by_value]
    if type_col == color_col or color_by_value == 'default':
        count_by_columns = [type_col]
    else:
        count_by_columns = [type_col, color_col]

    if data.empty:
        fig = EMPTY_FIGURE
    else:
        totals_df = data.sample_counts(count_by_columns).reset_index()

        type_col_name = TOTALS_COLUMN_DATAFRAME_NAMES[type_value]
        color_col_name = TOTALS_COLUMN_DATAFRAME_NAMES[color_by_value]

        category_orders = order_categories(totals_df,
                                           type_col_name,
                                           by_sum=True,
                                           sum_col='count')
        if color_by_value != 'default':
            category_orders.update(
                order_categories(totals_df,
                                 color_col_name,
                                 by_sum=True,
                                 sum_col='count'))

        fig = px.bar(
            totals_df,
            y=type_col_name,
            x='count',
            color=color_col_name,
            height=get_figure_height(len(totals_df[type_col_name].unique())),
            category_orders=category_orders,
            labels={
                'count': 'Samples count',
                'geo_area_name_standard': 'Geographic region',
                'lmat_taxonomy': 'Organism',
                'rgi_kmer_taxonomy': 'Organism'
            },
            title=TOTALS_FIGURE_TITLES[type_value],
        )
        fig.update_layout(font={'size': 14},
                          yaxis={
                              'title': '',
                              'ticksuffix': TICKSPACE
                          })

    return fig