def update_graph(DayofWeek, TimeofDay, branch_index, func, pltTitle):
    '''
    plot barplots of different functions for specific DayofWeek and TimeofDay 
    
    Parameters
    ---
    DayofWeek: Str
    TimeofDay: Str
    func: Str
    pltTitle: Str
    branch_index: Str
    '''
    chart = alt.Chart(new_df).mark_bar(color="cornflowerblue").encode(
        alt.X('Product line:N'),
        alt.Y(func, type='quantitative'),
        tooltip=[
            alt.Tooltip('Product line', title='Product line'),
            alt.Tooltip('Day_of_week', title="Day of Week"),
            alt.Tooltip('Time_of_day', title="Time of Day"),
            alt.Tooltip(func, title=pltTitle)
        ]).transform_filter(
            alt.FieldEqualPredicate(
                field='Branch', equal=branch_index)).transform_filter(
                    alt.FieldEqualPredicate(
                        field='Day_of_week',
                        equal=DayofWeek)).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Time_of_day',
                                equal=TimeofDay)).properties(width=280,
                                                             height=200,
                                                             title=pltTitle)
    return chart
Exemple #2
0
def make_bar_plot(day_of_week, time_of_day, branch_index, func, plot_title, y_title):
    '''Make a bar plot filtered by branch, day of week, and time of day 
    
    Parameters
    ---
    day_of_week: Str
    time_of_day: Str
    func: Str
    plot_title: Str
    branch_index: Str
    '''
    bar_plot = (alt
                .Chart(df)
                .mark_bar(color = 'cornflowerblue')
                .encode(alt.X('Product line:N', title=None),
                        alt.Y(func, type='quantitative', title=y_title),
                        tooltip=[alt.Tooltip('Product line', title='Product line'),
                                alt.Tooltip('Day_of_week', title='Day of Week'),
                                alt.Tooltip('Time_of_day', title='Time of Day'),
                                alt.Tooltip(func, title=plot_title)])
                .transform_filter(alt.FieldEqualPredicate(field='Branch', equal=branch_index))
                .transform_filter(alt.FieldEqualPredicate(field='Day_of_week', equal=day_of_week))
                .transform_filter(alt.FieldEqualPredicate(field='Time_of_day', equal=time_of_day))
                .properties(width=250, height=175, title= plot_title)
    )
    return bar_plot
Exemple #3
0
def timeseries_chart(data, category):
    chartData = (
        data[data['chapter'] == category]
        .groupby(["cause_of_death_map", "sex", "year"])
        .agg({"count": "sum"})
        .reset_index()
        .sort_values("count", ascending=False)
    )

    colorScale = alt.Scale(
        domain=['Males', 'Females'],
        range=['#4A90E2', '#50E3C2']
    )

    choices = chartData[chartData['cause_of_death_map'] != 'Other']['cause_of_death_map'].unique()

    colorScale = alt.Scale(
        domain=['Males', 'Females'],
        range=['#4A90E2', '#50E3C2']
    )

    base = alt.Chart(chartData).mark_line(
        point=True,
        interpolate="cardinal"
    ).encode(
        x=alt.X("year", type="nominal", axis=alt.Axis(labelAngle=330), title="Year"),
        y=alt.Y("count", title="Number of Deaths"),
        tooltip=chartData.columns.tolist(),
        color=alt.Color("sex", scale=colorScale, legend=alt.Legend(title="Gender"))
    ).properties(
        width=350,
        height=200
    )

    one = base.transform_filter(
        alt.FieldEqualPredicate(field='cause_of_death_map', equal=choices[0])
    ).properties(
        title=choices[0]
    )

    two = base.transform_filter(
        alt.FieldEqualPredicate(field='cause_of_death_map', equal=choices[1])
    ).properties(
        title=choices[1]
    )

    three = base.transform_filter(
        alt.FieldEqualPredicate(field='cause_of_death_map', equal=choices[2])
    ).properties(
        title=choices[2]
    )

    chart = alt.hconcat(one, two, three).configure(
        axis=alt.Axis(grid=False)
    )
    return return_vega_spec_json(chart)
Exemple #4
0
def make_heat_map(branch_index, func, plot_title):
    
    '''Make a heat map by day of week and time of day
    
    Parameters
    ---
    branch_index: Str
    func: Str
    plot_title: Str
    '''
    
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    times = ['Morning', 'Afternoon', 'Evening']
    
    heat_map = (alt
                .Chart(df)
                .mark_rect()
                .encode(alt.X('Day_of_week:N', title=None, sort=days),
                        alt.Y('Time_of_day:N', title=None, sort=times),
                        alt.Color(func, type = 'quantitative' ,title=None, scale=alt.Scale(scheme='greens')),
                        tooltip=[alt.Tooltip('Day_of_week', title='Day of the week'), 
                                 alt.Tooltip('Time_of_day', title='Time of the day'),
                                 alt.Tooltip(func, type='quantitative', title=plot_title, format=',.0f')])
                .configure_axisX(labelAngle=45)
                .transform_filter(alt.FieldEqualPredicate(field='Branch', equal= branch_index))
                .configure_axis(labelFontSize=13, titleFontSize=13)
                .configure_title(fontSize=14)
                .properties(width=180, height=130, title=plot_title)
    )   
    return heat_map
def make_total_sales(store_id='A'):

    # Create total sales heat map (unfiltered by store)

    total_sales = (
        alt.Chart(df)  # make this react to the radio
        .mark_rect().encode(
            alt.X('Day_of_week:N', title=None, sort=days),
            alt.Y('Time_of_day:N', title=None, sort=times),
            alt.Color('sum(Total):Q',
                      title=None,
                      scale=alt.Scale(scheme='greens')),
            tooltip=[
                alt.Tooltip('Day_of_week', title='Day of the week'),
                alt.Tooltip('Time_of_day', title="Time of the day"),
                alt.Tooltip('sum(Total):Q', title='Sales', format='$,.0f')
            ]).transform_filter(
                alt.FieldEqualPredicate(
                    field='Branch', equal=store_id)).configure_axis(
                        labelFontSize=13, titleFontSize=13).configure_title(
                            fontSize=14).properties(width=180,
                                                    height=130,
                                                    title='Total sales'))

    return total_sales
Exemple #6
0
def update_world_chart(year, stat_type, include_usa, gdp_pct):
    map_stat = 'percent_GDP' if gdp_pct else 'USD_Value'
    map_legend = '% GDP' if gdp_pct else 'USD Value'
    arms_df_tmp = arms_gdp.copy()
    if not include_usa:
        arms_df_tmp.loc[arms_df_tmp['Country'] == 'USA', map_stat] = 0

    print(year, stat_type, include_usa, gdp_pct)
    chart = alt.Chart(world_map_skl).mark_geoshape(stroke='white').encode(
        color=alt.condition(alt.FieldEqualPredicate(field=map_stat, equal=0),
                            alt.value('lightgray'), map_stat + ':Q',
                            scale=alt.Scale(scheme='goldorange'),
                            legend=alt.Legend(title=map_legend)),
        tooltip=['Country:N', map_stat + ':Q']
    ).transform_lookup(
        lookup='id',
        from_=alt.LookupData(arms_df_tmp.query("Year == " + str(year)).query("Direction == '%s'" % (stat_type)), 'id',
                             [map_stat, 'Country'])
    ).project('equirectangular').properties(
        width=720,
        height=300,
        background='white'
    ).configure_axis(
        grid=False
    )

    return html.Iframe(
        sandbox='allow-scripts',
        id='plot',
        width='900',
        height='350',
        style={'border-width': '0'},
        srcDoc=chart.to_html()
    )
def concat_earnings_rank(year, geo):
    ear_rank = alt.Chart(
        eco, title="Average weekly earnings by province/territory"
    ).mark_bar().encode(
        x=alt.X('average(All industries):Q',
                axis=alt.Axis(tickCount=3,
                              titleFontSize=20,
                              grid=False,
                              ticks=False,
                              format=('$,f')),
                title=None),
        y=alt.Y('Geography:O',
                sort='-x',
                axis=alt.Axis(labelFontSize=18),
                title=None),
        color=alt.condition(
            (alt.datum.Geography == geo) | (alt.datum.Geography == 'Canada'),
            alt.value('darkblue'), alt.value('lightblue')),
        tooltip=[
            alt.Tooltip('Geography', title='Province/territory'),
            alt.Tooltip('Year'),
            alt.Tooltip('average(All industries):Q',
                        format='$,',
                        title='Earnings')
        ]).transform_filter(alt.FieldEqualPredicate(
            field='Year',
            equal=year)).configure_view(strokeOpacity=0).configure_axis(
                domain=False).configure_title(fontSize=30)

    w = 300
    h = 200

    ear_rank = ear_rank.properties(width=w, height=h)

    return ear_rank.to_html()
def make_transaction_size(store_id='A'):

    # Create average transaction size heat map (unfiltered by store)

    transaction_size = (alt.Chart(df).mark_rect().transform_aggregate(
        groupby=['Branch', 'Day_of_week', 'Time_of_day'],
        total_sales='sum(Total):Q',
        total_trxns='count(Invoice ID):Q').transform_calculate(
            Avg_trans_size='datum.total_sales / datum.total_trxns').encode(
                alt.X('Day_of_week:N', title=None, sort=days),
                alt.Y('Time_of_day:N', title=None, sort=times),
                alt.Color('Avg_trans_size:Q',
                          title=None,
                          scale=alt.Scale(scheme='greens')),
                tooltip=[
                    alt.Tooltip('Day_of_week', title='Day of the week'),
                    alt.Tooltip('Time_of_day', title="Time of the day"),
                    alt.Tooltip('Avg_trans_size:Q',
                                title='Average transaction size',
                                format='$,.0f')
                ]).transform_filter(
                    alt.FieldEqualPredicate(
                        field='Branch', equal=store_id)).configure_axis(
                            labelFontSize=13,
                            titleFontSize=13).configure_title(
                                fontSize=14).properties(
                                    width=180,
                                    height=130,
                                    title='Average transaction size'))

    return transaction_size
Exemple #9
0
def make_bar_plot(day_of_week, time_of_day, branch_index, func, plot_title,
                  y_title):
    '''
    Make a bar plot filtered by branch, day of week, and time of day 
    
    Parameters
    ----------
    day_of_week: str
        the day of week ranging from Monday to Sunday 
    time_of_day: str
        the time of day (Morning, Afternoon or Evening) 
    func: str
        the variable to be used as y axis  
    plot_title: str
        the name to be used as title 
    branch_index: str
        the character used to represent the supermarket branch 

    Returns
    -------
    Altair chart object
        a bar plot 

    '''
    bar_plot = (alt.Chart(df).mark_bar(color='cornflowerblue').encode(
        alt.X('Product line:N', title=None),
        alt.Y(func, type='quantitative', title=y_title),
        tooltip=[
            alt.Tooltip('Product line', title='Product line'),
            alt.Tooltip(func, title=plot_title)
        ]).transform_filter(
            alt.FieldEqualPredicate(
                field='Branch', equal=branch_index)).transform_filter(
                    alt.FieldEqualPredicate(
                        field='Day_of_week',
                        equal=day_of_week)).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Time_of_day',
                                equal=time_of_day)).properties(
                                    width=250, height=175, title=plot_title))
    return bar_plot
def concat_gdp_indus_vis(year, geo):
    real_gdp_ind_rank = alt.Chart(industry_gdp, title="GDP contribution by industry").mark_bar().encode(
        y=alt.Y('sum(Industry GDP):Q', title='CA$ (MM)',
                axis=alt.Axis(tickCount=3, titleFontSize=20, grid=False, format=('$,f'))),
        x=alt.X('Industry:O', sort='-y', axis=alt.Axis(labelAngle=-90), title=None),
        color=alt.Color('sum(Industry GDP)', title='GDP', scale=alt.Scale(scheme='blues'),
                        legend=alt.Legend(format=('$,f'))),
        tooltip=[alt.Tooltip('Geography', title='Province/territory'), alt.Tooltip('Year'), alt.Tooltip('Industry'),
                 alt.Tooltip('sum(Industry GDP):Q', format=('$,.0f'), title='GDP')]).transform_filter(
        alt.FieldEqualPredicate(field='Year', equal=year)).transform_filter(
        alt.FieldEqualPredicate(field='Geography', equal=geo))

    real_gdp_ind_gr_rank = alt.Chart(industry_gdp_gr, title="GDP growth rate by industry").mark_bar().encode(
        y=alt.Y('sum(Industry GDP):Q', title=None, axis=alt.Axis(tickCount=3, grid=False, format=('%'))),
        x=alt.X('Industry:O', sort='-y', title=None, axis=alt.Axis(labelLimit=90, labelAngle=-90)),
        color=alt.Color('sum(Industry GDP)', title='GDP growth rate', scale=alt.Scale(scheme='redblue'),
                        legend=alt.Legend(format=('%'))),
        tooltip=[alt.Tooltip('Geography', title='Province/territory'), alt.Tooltip('Year'), alt.Tooltip('Industry'),
                 alt.Tooltip('sum(Industry GDP):Q', format=('.2%'), title='Growth rate')]).transform_filter(
        alt.FieldEqualPredicate(field='Year', equal=year)).transform_filter(
        alt.FieldEqualPredicate(field='Geography', equal=geo))

    w = 400
    h = 300

    real_gdp_ind_rank = real_gdp_ind_rank.properties(
        width=w,
        height=h
    )

    real_gdp_ind_gr_rank = real_gdp_ind_gr_rank.properties(
        width=w,
        height=h
    )
    vis = (real_gdp_ind_rank | real_gdp_ind_gr_rank).configure_view(strokeOpacity=0).configure_axis(domain=False,
                                                                                                    ticks=False).configure_title(fontSize=30)
    return vis.to_html()
Exemple #11
0
def make_heat_map(branch_index, func, plot_title):
    """
    Make a heat map by day of week and time of day
    
    Parameters
    ----------
    branch_index: str
        the character used to represent the supermarket branch 
    func: str
        the variable to be associated with alt.Color() 
    plot_title: str
        the name to be used as title 

    Returns
    -------
    Altair chart object 
        a heat map 
    """

    days = [
        'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
        'Sunday'
    ]
    times = ['Morning', 'Afternoon', 'Evening']

    heat_map = (alt.Chart(df).mark_rect().encode(
        alt.X('Day_of_week:N', title=None, sort=days),
        alt.Y('Time_of_day:N', title=None, sort=times),
        alt.Color(func,
                  type='quantitative',
                  title=None,
                  scale=alt.Scale(scheme='greens')),
        tooltip=[
            alt.Tooltip(func,
                        type='quantitative',
                        title=plot_title,
                        format=',.0f')
        ]).configure_axisX(labelAngle=45).transform_filter(
            alt.FieldEqualPredicate(
                field='Branch', equal=branch_index)).configure_axis(
                    labelFontSize=13,
                    titleFontSize=13).configure_title(fontSize=14).properties(
                        width=180, height=130, title=plot_title))
    return heat_map
    def __create_world_timeseries_chart(self, case_type, ntype="New"):
        """create trend chart for global numbers

        Args:
            case_type (string): "confirmed", "recovered", "death"
            ntype (string): "Total" or "New"

        Returns:
            [type]: [description]
        """

        if case_type == "confirmed":
            chart_title = "Global Confirmed Cases"
            case_type = 1
        elif case_type == "death":
            chart_title = "Global Deaths"
            case_type = 2
        elif case_type == "recovered":
            chart_title = "Global Recovered Cases"
            case_type = 3
        if ntype == "Total":
            chart_title = chart_title + " Over Time"
        else:
            chart_title = "New " + chart_title + " Per Day"
        data = self.data_reader.get_timeserie_data_by_country("all", case_type)

        chart = (
            alt.Chart(
                data,
                title=alt.TitleParams(text=chart_title),
                height=200,
            )
            .mark_line()
            .transform_filter(alt.FieldEqualPredicate(field="type", equal=ntype))
            .encode(
                x=alt.X("date:T", title="", axis=alt.Axis(format=("%b %Y"))),
                y=alt.Y("count:Q", title=""),
            )
            .configure_axis(grid=False)
            .configure_title(anchor="start")
            .properties(width=735)
        )
        return chart.to_html()
Exemple #13
0
    def __create_timeserie_chart(self, country, case_type=1, ntype="Total"):
        data = self.data_reader.get_timeserie_data_by_country(
            country, case_type)
        if case_type == 1:
            chart_title = "Cases over time"
        elif case_type == 2:
            chart_title = "Deaths over time"

        chart = (
            alt.Chart(
                data,
                title=alt.TitleParams(text=chart_title, subtitle=country),
            ).mark_line().transform_filter(
                alt.FieldEqualPredicate(field="type", equal=ntype)).encode(
                    x=alt.X("date:T", title=""),
                    y=alt.Y("count:Q", title="")).configure_axis(
                        grid=False).configure_title(anchor="start").properties(
                            width=350, height=200)
            # .properties(width="container", height="container")
        )
        return chart.to_html()
def make_customer_traffic(store_id='A'):

    # Create customer traffic heat map (unfiltered by store)

    customer_traffic = (alt.Chart(df).mark_rect().encode(
        alt.X('Day_of_week:N', title=None, sort=days),
        alt.Y('Time_of_day:N', title=None, sort=times),
        alt.Color('count(Invoice ID):Q',
                  title=None,
                  scale=alt.Scale(scheme='greens')),
        tooltip=[
            alt.Tooltip('Day_of_week', title='Day of the week'),
            alt.Tooltip('Time_of_day', title="Time of the day"),
            alt.Tooltip('count(Invoice ID):Q', title='No. of transactions')
        ]).transform_filter(
            alt.FieldEqualPredicate(
                field='Branch', equal=store_id)).configure_axis(
                    labelFontSize=13,
                    titleFontSize=13).configure_title(fontSize=14).properties(
                        width=180, height=130, title='Customer traffic'))

    return customer_traffic
Exemple #15
0
def get_bar_chart(ranks_st: pd.DataFrame, sorted_omic: list,
                  conditionals_1: str, conditionals_2: str, omic_column: str,
                  omic: str, omic1: str, omic2: str, x_size: float,
                  y_size: float, mlt, selector1, selector2):

    if omic == omic1:
        x = alt.X('%s:N' % omic1, sort=sorted_omic, axis=None)
        y = alt.Y('mean(rank):Q', axis=alt.Axis(titleFontSize=8))
        width = x_size
        height = 50
    else:
        x = alt.X('mean(rank):Q', axis=alt.Axis(titleFontSize=8, orient='top'))
        y = alt.Y('%s:N' % omic2, sort=sorted_omic, axis=None)
        width = 50
        height = y_size

    if omic_column:
        color = alt.condition(mlt, omic_column, alt.ColorValue("grey"))
    else:
        color = alt.condition(mlt, alt.ColorValue("steelblue"),
                              alt.ColorValue("grey"))

    tooltips = [
        omic, 'mean(rank)', 'stdev(rank)',
        'min(%s)' % conditionals_1,
        'min(%s)' % conditionals_2
    ]
    if omic_column:
        tooltips.append(omic_column)

    bar_omic = alt.Chart(ranks_st).mark_bar(
    ).encode(x=x, y=y, color=color, tooltip=tooltips).add_selection(
        mlt, selector1, selector2).transform_filter(
            alt.FieldEqualPredicate(field='conditional', equal='conditionals')
        ).transform_filter(
            alt.datum[conditionals_1] <= selector1.cutoff1).transform_filter(
                alt.datum[conditionals_2] <= selector2.cutoff2).properties(
                    width=width, height=height)
    return bar_omic
Exemple #16
0
def concat_cpi_vis(year, geo):
    all_cpi_evo = alt.Chart(eco, title="CPI all-items evolution").mark_area(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
            x=alt.X('Year',
                    title='2002=100',
                    axis=alt.Axis(tickCount=5,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format='Y')),
            y=alt.Y('average(All-items):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=(',.0f')),
                    title=None),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(All-items):Q',
                            format=('.0f'),
                            title='CPI')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Geography',
                                        equal=geo)).transform_filter(
                                            alt.FieldRangePredicate(
                                                'Year', [2000, year]))

    all_cpi_gr = alt.Chart(eco_gr, title="Growth rates").mark_bar(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue'),
        size=16).encode(x=alt.X('Year',
                                title='Year',
                                axis=alt.Axis(tickCount=5,
                                              titleFontSize=20,
                                              grid=False,
                                              ticks=False,
                                              format='Y')),
                        y=alt.Y('sum(All-items):Q',
                                axis=alt.Axis(tickCount=3,
                                              grid=False,
                                              ticks=False,
                                              format=('%')),
                                title=None),
                        color=alt.condition((alt.datum.Year == year),
                                            alt.value('darkblue'),
                                            alt.value('lightblue')),
                        tooltip=[
                            alt.Tooltip('Geography',
                                        title='Province/territory'),
                            alt.Tooltip('Year'),
                            alt.Tooltip('sum(All-items):Q',
                                        format=('.2%'),
                                        title='Growth rate')
                        ]).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Geography',
                                equal=geo)).transform_filter(
                                    alt.FieldRangePredicate(
                                        'Year', [2000, year]))

    gasoline_cpi_evo = alt.Chart(
        eco, title="CPI gasoline evolution").mark_area(
            point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
                x=alt.X('Year',
                        title='2002=100',
                        axis=alt.Axis(tickCount=5,
                                      titleFontSize=20,
                                      grid=False,
                                      ticks=False,
                                      format='Y')),
                y=alt.Y('average(Gasoline):Q',
                        axis=alt.Axis(tickCount=3,
                                      titleFontSize=20,
                                      grid=False,
                                      ticks=False,
                                      format=(',.0f')),
                        title=None),
                tooltip=[
                    alt.Tooltip('Geography', title='Province/territory'),
                    alt.Tooltip('Year'),
                    alt.Tooltip('average(Gasoline):Q', title='CPI')
                ]).transform_filter(
                    alt.FieldEqualPredicate(field='Geography',
                                            equal=geo)).transform_filter(
                                                alt.FieldRangePredicate(
                                                    'Year', [2000, year]))

    gasoline_cpi_gr = alt.Chart(eco_gr, title="Growth rates").mark_bar(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue'),
        size=16).encode(x=alt.X('Year',
                                title='Year',
                                axis=alt.Axis(tickCount=5,
                                              titleFontSize=20,
                                              grid=False,
                                              ticks=False,
                                              format='Y')),
                        y=alt.Y('sum(Gasoline):Q',
                                axis=alt.Axis(tickCount=3,
                                              grid=False,
                                              ticks=False,
                                              format=('%')),
                                title=None),
                        color=alt.condition((alt.datum.Year == year),
                                            alt.value('darkblue'),
                                            alt.value('lightblue')),
                        tooltip=[
                            alt.Tooltip('Geography',
                                        title='Province/territory'),
                            alt.Tooltip('Year'),
                            alt.Tooltip('sum(Gasoline):Q',
                                        format=('.2%'),
                                        title='Growth rate')
                        ]).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Geography',
                                equal=geo)).transform_filter(
                                    alt.FieldRangePredicate(
                                        'Year', [2000, year]))

    cpi_rank = alt.Chart(
        eco, title="CPI all-items by province/territory").mark_bar().encode(
            x=alt.X('average(All-items):Q',
                    title='2002=100',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  format=(',.0f'))),
            y=alt.Y('Geography:O',
                    sort='-x',
                    axis=alt.Axis(labelFontSize=18),
                    title=None),
            color=alt.condition((alt.datum.Geography == geo) |
                                (alt.datum.Geography == 'Canada'),
                                alt.value('darkblue'), alt.value('lightblue')),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(All-items):Q', title='CPI')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Year', equal=year))

    cpi_gr_rank = alt.Chart(
        eco_gr, title="Growth rate by province/territory").mark_bar().encode(
            x=alt.X('average(All-items):Q',
                    title=None,
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  format=('%'))),
            y=alt.Y('Geography:O',
                    sort='-x',
                    axis=alt.Axis(labelFontSize=18),
                    title=None),
            color=alt.condition((alt.datum.Geography == geo) |
                                (alt.datum.Geography == 'Canada'),
                                alt.value('darkblue'), alt.value('lightblue')),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(All-items):Q',
                            format=('.2%'),
                            title='CPI')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Year', equal=year))

    w = 300
    h = 200

    all_cpi_evo = all_cpi_evo.properties(width=w, height=h)

    all_cpi_gr = all_cpi_gr.properties(width=w, height=h)
    gasoline_cpi_evo = gasoline_cpi_evo.properties(width=w, height=h)

    gasoline_cpi_gr = gasoline_cpi_gr.properties(width=w, height=h)

    cpi_rank = cpi_rank.properties(width=w, height=h)

    cpi_gr_rank = cpi_gr_rank.properties(width=w, height=h)

    all_cpi = (all_cpi_evo | all_cpi_gr)
    gasoline_cpi = (gasoline_cpi_evo | gasoline_cpi_gr)
    rank_cpi = (cpi_rank | cpi_gr_rank)
    cpi_plot = ((all_cpi & gasoline_cpi)
                & rank_cpi).configure_view(strokeOpacity=0).configure_axis(
                    domain=False).configure_title(fontSize=30)

    return cpi_plot.to_html()
Exemple #17
0
def concat_ngdp_vis(year, geo):
    nominal_gdp = alt.Chart(
        eco, title="Total GDP").mark_bar().transform_aggregate(
            groupby=['Geography', 'Year'], GDP='sum(Nominal GDP)').encode(
                x=alt.X('sum(GDP):Q',
                        title='CA$ (MM)',
                        axis=alt.Axis(titleFontSize=20,
                                      grid=False,
                                      ticks=False,
                                      labels=False)),
                y=alt.Y('Geography:O',
                        sort='-x',
                        title=None,
                        axis=alt.Axis(labelFontSize=20,
                                      ticks=False,
                                      grid=False)),
                tooltip=[
                    alt.Tooltip('Geography', title='Province/territory'),
                    alt.Tooltip('Year')
                ]).transform_filter(
                    alt.FieldEqualPredicate(
                        field='Geography', equal=geo)).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Year', equal=year)).properties(
                                    height=200, width=400).mark_text(
                                        dx=-175, color='darkblue',
                                        size=40).encode(text=alt.Text(
                                            'sum(GDP):Q', format=('$,')))

    nominal_gdp_gr = alt.Chart(
        eco_gr, title="Growth rate").mark_bar().transform_aggregate(
            groupby=['Geography', 'Year'], GDP='sum(Nominal GDP)').encode(
                x=alt.X('sum(GDP):Q',
                        title='Yearly %',
                        axis=alt.Axis(titleFontSize=20,
                                      grid=False,
                                      ticks=False,
                                      labels=False)),
                y=alt.Y('Geography:O',
                        sort='-x',
                        title=None,
                        axis=alt.Axis(grid=False, ticks=False, labels=False)),
                tooltip=[
                    alt.Tooltip('Geography', title='Province/territory'),
                    alt.Tooltip('Year')
                ]).transform_filter(
                    alt.FieldEqualPredicate(
                        field='Geography', equal=geo)).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Year', equal=year)).properties(
                                    height=200, width=400).mark_text(
                                        dx=-175, color='darkblue',
                                        size=40).encode(text=alt.Text(
                                            'sum(GDP):Q', format=('.2%')))

    nominal_gdp_evo = alt.Chart(eco, title="GDP evolution").mark_area(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
            x=alt.X('Year',
                    title='Year',
                    axis=alt.Axis(tickCount=5,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format='Y')),
            y=alt.Y('sum(Nominal GDP):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=('$,f')),
                    title=None),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('sum(Nominal GDP):Q', format=('$,'), title='GDP')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Geography',
                                        equal=geo)).transform_filter(
                                            alt.FieldRangePredicate(
                                                'Year', [2000, year]))

    nominal_gdp_rank = alt.Chart(
        eco, title="Contribution by province/territory").mark_bar().encode(
            x=alt.X('sum(Nominal GDP):Q',
                    title='CA$ (MM)',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  format=('$,f'))),
            y=alt.Y('Geography:O',
                    sort='-x',
                    axis=alt.Axis(labelFontSize=18),
                    title=None),
            color=alt.condition((alt.datum.Geography == geo) |
                                (alt.datum.Geography == 'Canada'),
                                alt.value('darkblue'), alt.value('lightblue')),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('sum(Nominal GDP):Q', format=('$,'), title='GDP')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Year', equal=year))

    nominal_gdp_gr_evo = alt.Chart(eco_gr, title="GDP growth rates").mark_bar(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue'),
        size=16).encode(x=alt.X('Year',
                                title='Year',
                                axis=alt.Axis(tickCount=5,
                                              titleFontSize=20,
                                              grid=False,
                                              ticks=False,
                                              format='Y')),
                        y=alt.Y('sum(Nominal GDP):Q',
                                axis=alt.Axis(tickCount=3,
                                              grid=False,
                                              ticks=False,
                                              format=('%')),
                                title=None),
                        color=alt.condition((alt.datum.Year == year),
                                            alt.value('darkblue'),
                                            alt.value('lightblue')),
                        tooltip=[
                            alt.Tooltip('Geography',
                                        title='Province/territory'),
                            alt.Tooltip('Year'),
                            alt.Tooltip('sum(Nominal GDP):Q',
                                        format=('.2%'),
                                        title='Growth rate')
                        ]).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Geography',
                                equal=geo)).transform_filter(
                                    alt.FieldRangePredicate(
                                        'Year', [2000, year]))

    nominal_gdp_gr_rank = alt.Chart(
        eco_gr, title="By province/territory").mark_bar().encode(
            x=alt.X('sum(Nominal GDP):Q',
                    title=None,
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  format=('%'))),
            y=alt.Y('Geography:O',
                    sort='-x',
                    axis=alt.Axis(labelFontSize=18),
                    title=None),
            color=alt.condition((alt.datum.Geography == geo) |
                                (alt.datum.Geography == 'Canada'),
                                alt.value('darkblue'), alt.value('lightblue')),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('sum(Nominal GDP):Q',
                            format=('.2%'),
                            title='Growth rate')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Year', equal=year))

    w = 300
    h = 200

    nominal_gdp = nominal_gdp.properties(width=w, height=h)

    nominal_gdp_gr = nominal_gdp_gr.properties(width=w, height=h)

    nominal_gdp_evo = nominal_gdp_evo.properties(width=w, height=h)

    nominal_gdp_rank = nominal_gdp_rank.properties(width=w, height=h)

    nominal_gdp_gr_evo = nominal_gdp_gr_evo.properties(width=w, height=h)

    nominal_gdp_gr_rank = nominal_gdp_gr_rank.properties(width=w, height=h)

    ngdp_summary = (nominal_gdp | nominal_gdp_gr)
    ngdp_total = (nominal_gdp_evo | nominal_gdp_rank)
    ngdp_gr = (nominal_gdp_gr_evo | nominal_gdp_gr_rank)
    ngdp_vis = ((ngdp_summary & ngdp_total)
                & ngdp_gr).configure_view(strokeOpacity=0).configure_axis(
                    domain=False).configure_title(fontSize=30)

    return ngdp_vis.to_html()
    dailyCount = dailyCount.reset_index()
    currentMonth = currentMonth.reset_index()

    #Create monthly cumulative line chart
    monthlyLine = altair.Chart(currentMonth).mark_line(
        stroke='#f304d3').encode(
            altair.X('date(Date):T',
                     axis=altair.Axis(title='Day of the Month')),
            altair.Y('MonthlyCumSum',
                     axis=altair.Axis(title='Total Bikes'),
                     scale=altair.Scale(domain=(0, 100000))),
            altair.Color('Date:O',
                         timeUnit='year',
                         legend=altair.Legend(title='Year'))).transform_filter(
                             altair.FieldEqualPredicate(
                                 timeUnit='year',
                                 field='Date',
                                 equal=datetime.date.today().year)).properties(
                                     width=200, height=200)

    monthlyLineAvg = altair.Chart(currentMonth).mark_line(
        color='grey', strokeDash=[8, 8]).encode(
            altair.X('date(Date):T', axis=altair.Axis(title='')),
            altair.Y('mean(MonthlyCumSum)', axis=altair.Axis(title='')),
        ).properties(width=200, height=200)

    monthlyLineBand = altair.Chart(currentMonth).mark_errorband(
        extent='stdev', color='grey').encode(
            altair.X('date(Date):T', axis=altair.Axis(title='')),
            altair.Y('MonthlyCumSum', axis=altair.Axis(title='')),
        ).properties(width=200, height=200)
Exemple #19
0
def plot_lang_chart(job_title_var):
    lang_chart = alt.Chart(data=kaggle_langs).mark_bar().encode(
        x='prog_lang', y='count()').transform_filter(
            alt.FieldEqualPredicate(field='Q5', equal=job_title_var))
    return lang_chart.to_html()
def concat_employment_vis(year, geo):
    er_evo = alt.Chart(eco, title="Unemployment rate evolution").mark_area(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
            x=alt.X('Year',
                    title='Year',
                    axis=alt.Axis(tickCount=5,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format='Y')),
            y=alt.Y('average(Unemployment rate):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=('%')),
                    title=None),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(Unemployment rate):Q',
                            format=('.2%'),
                            title='Unemployment rate')
            ]).transform_filter(
                alt.FieldEqualPredicate(
                    field='Geography', equal=geo)).transform_filter(
                        alt.FieldRangePredicate(
                            'Year', [2000, year])).properties(height=430)

    er_rank = alt.Chart(
        eco,
        title="Unemployment rate by province/territory").mark_bar().encode(
            x=alt.X('average(Unemployment rate):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=('%')),
                    title=None),
            y=alt.Y('Geography:O',
                    sort='-x',
                    axis=alt.Axis(labelFontSize=18),
                    title=None),
            color=alt.condition((alt.datum.Geography == geo) |
                                (alt.datum.Geography == 'Canada'),
                                alt.value('darkblue'), alt.value('lightblue')),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(Unemployment rate):Q',
                            format=('.2%'),
                            title='Unemployment rate')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Year', equal=year))

    er_ind_rank = alt.Chart(
        labour, title="Unemployment rate by industry").mark_bar().encode(
            x=alt.X('Unemployment rate:Q',
                    axis=alt.Axis(tickCount=3,
                                  grid=False,
                                  ticks=False,
                                  format=('%')),
                    title=None),
            y=alt.Y('Industry:O',
                    sort='-x',
                    axis=alt.Axis(labelFontSize=18),
                    title=None),
            color=alt.value('lightblue'),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('Industry'),
                alt.Tooltip('Unemployment rate:Q',
                            format=('.2%'),
                            title='Unemployment rate')
            ]).transform_filter(
                alt.FieldEqualPredicate(
                    field='Year', equal=year)).transform_filter(
                        alt.FieldEqualPredicate(field='Geography', equal=geo))

    w = 300
    h = 200

    # nominal_gdp = nominal_gdp.properties(
    #     width=w,
    #     height=h
    # )
    #
    # nominal_gdp_gr = nominal_gdp_gr.properties(
    #     width=w,
    #     height=h
    # )
    #
    # nominal_gdp_evo = nominal_gdp_evo.properties(
    #     width=w,
    #     height=h
    # )

    er_ind_rank = er_ind_rank.properties(width=w, height=h)

    er_rank = er_rank.properties(width=w, height=h)

    er_evo = er_evo.properties(width=w, height=h)

    employment_plot = ((er_evo | er_rank) & er_ind_rank).configure_view(
        strokeOpacity=0).configure_axis(domain=False).configure_title(
            fontSize=30)

    return employment_plot.to_html()
Exemple #21
0
def app():

    ####### Datasets
    
    control_dataset = 'https://raw.githubusercontent.com/JulioCandela1993/VisualAnalytics/master/data/control_policy.csv'
    deaths_dataset = 'https://raw.githubusercontent.com/JulioCandela1993/VisualAnalytics/master/data/deaths.csv'
    
    
    ####### Dataframes
    
    control_df = pd.read_csv(control_dataset)
    
    
    ####### Dashboard
    
    st.title("Tobacco Control")
    
    
    
    
    st.markdown('''
    The following analysis is based on the evaluation made by World Health Organization (WHO) 
    to country policies against Tobacco. A score from 1 to 5 is assigned depending on the intensity 
    of a country to deal with Tobacco issues being 1 the worst and 5 the best
    ''')
    
    ####### Control Measures given by WHO
    
    control_metrics = ["Monitor",	
               "Protect from tobacco smoke",	
               "Offer help to quit tobacco use", 
               "Warn about the dangers of tobacco",
               "Enforce bans on tobacco advertising",
               "Raise taxes on tobacco",
               #"Anti-tobacco mass media campaigns"
    ]
    
    
    # Main Selector of Control Measures
    cols = st.selectbox('Select control measure: ', control_metrics)
    
    if cols in control_metrics:   
        metric_to_show_in_covid_Layer = cols +":Q"
        metric_name = cols
       
    years = ['2008', '2010', '2012', '2014', '2016', '2018']
    columns_year = [metric_name+" "+str(year) for year in years]
    columns = ["d" +str(year) for year in years]
        
        
    container_map = st.beta_container()
    
    
    ####### Map Visualization
    
    with container_map:
        
        st.header("How are countries controlling Tobacco consumption?")
        #st.header('"'A global view of the implementation of the policy """ around the world'"')
    
        st.markdown('''
        In the folling map, we can identify the intensity of a specific control policy for each country. 
        We can also see the evolution of these policies from 2008 to 2018
        ''')
        
        # Year Selector
        select_year_list = st.selectbox('Select year: ', years)#st.slider('Select year: ', 2008, 2018, 2008, step = 2)
        select_year = int(select_year_list)

        # Map Topology
        url_topojson = 'https://raw.githubusercontent.com/JulioCandela1993/VisualAnalytics/master/world-countries.json'
        data_topojson_remote = alt.topo_feature(url=url_topojson, feature='countries1')
        
        
        ### Map Chart
        
        map_geojson = alt.Chart(data_topojson_remote).mark_geoshape(
            stroke="black",
            strokeWidth=1,
            #fill='lightgray'
        ).encode(
            color=alt.Color(metric_to_show_in_covid_Layer),
        ).transform_lookup(
                lookup="properties.name",
                from_=alt.LookupData(control_dataset, "Country", [metric_name,"Year"])
        ).properties(
            width=700,
            height=500
        )
              
        choro = alt.Chart(data_topojson_remote, title = 'Implementation of the policy "' +metric_name+'" around the world').mark_geoshape(
            stroke='black'
        ).encode(
            color=alt.Color(metric_to_show_in_covid_Layer, 
                            scale=alt.Scale(range=['#ffe8dd','#ef4f4f']),
                            legend=None),
            tooltip=[
                alt.Tooltip("properties.name:O", title="Country"),
                alt.Tooltip(metric_to_show_in_covid_Layer, title=metric_name),
                alt.Tooltip("year:Q", title="Year"),
            ],
        ).transform_calculate(
            d2008 = "1",
            d2010 = "1",
            d2012 = "1",
            d2014 = "1",
            d2016 = "1",
            d2018 = "1"
        ).transform_fold(
            columns, as_=['year', 'metric']
        ).transform_calculate(
            yearQ = 'replace(datum.year,"d","")'
        ).transform_calculate(
            key_val = 'datum.properties.name + datum.yearQ'
        ).transform_lookup(
                lookup="key_val",
                from_=alt.LookupData(control_dataset, "ID", [metric_name,"Year"])
        ).transform_calculate(
            year='parseInt(datum.Year)',
        ).transform_filter(
            alt.FieldEqualPredicate(field='year', equal=select_year)
        )
    
    
        st.altair_chart(map_geojson + choro)
            
        ## Qualification array
        
        qualifications = pd.DataFrame.from_dict({
            "keys": [1,2,3,4,5],
            "category":["1.Very Bad", "2.Bad", "3.Medium", "4.Good", "5.Perfect"]
            })
        
        ## Legend Chart
        
        ##### Data Transformations

        legend_info = alt.Chart(control_dataset).transform_joinaggregate(
            num_countries='count(*)',
        ).transform_filter(
            alt.FieldEqualPredicate(field='Year', equal=select_year)
        ).transform_lookup(
                lookup=metric_name,
                from_=alt.LookupData(qualifications, "keys", ["category"])
        ).transform_aggregate(
            count='count()',
            groupby=[metric_name,"category"]
        ).transform_joinaggregate(
            total='sum(count)'  
        ).transform_calculate(
            pct='datum.count / datum.total'  
        )
        
        legend_bar = legend_info.mark_bar().encode(
                x=alt.X('pct:Q', stack="normalize", sort=alt.SortField(metric_to_show_in_covid_Layer), title = None, axis = None),                
                color=alt.Color(metric_to_show_in_covid_Layer,
                            scale=alt.Scale(range=['#ffe8dd','#ef4f4f'])
                            ,legend = None),
                tooltip=[
                    alt.Tooltip(metric_to_show_in_covid_Layer, title=metric_name)
                ],
        )
        
        legend_value = legend_info.mark_text(dx = -11, align='center', baseline='middle', color='black', fontWeight = "bold").encode(
            x=alt.X('pct:Q', sort=alt.SortField(metric_to_show_in_covid_Layer), stack='normalize', axis = None),
            #detail = metric_to_show_in_covid_Layer,
            color=alt.Color(metric_name +":O",
                            scale=alt.Scale(range=['#000000','#000000'])
                            ,legend = None),
            text=alt.Text('pct:Q',format='.0%')
        )
        
        legend_category = legend_info.mark_text(dx = 10, dy = 10, align='left', baseline='middle', color='black', angle = 90, fontWeight = "bold").encode(
            x=alt.X('pct:Q', sort=alt.SortField(metric_to_show_in_covid_Layer), stack='normalize', axis = None),
            #detail = metric_to_show_in_covid_Layer,
            color=alt.Color(metric_name +":O",
                            scale=alt.Scale(range=['#000000','#000000'])
                            ,legend = None),
            #text=alt.Text(metric_to_show_in_covid_Layer)
            text=alt.Text("category:N")
        )
            
        legend_chart = (legend_bar + legend_value + legend_category).properties(
            width=700,
            height=100,
            title = metric_name
        ).configure_title(align = "left"
        ).configure_view(
            strokeWidth=0
        )
        
        
        st.altair_chart(legend_chart)
        
    ##### Evolution of policy per selected countries    
        
    container_policycountry = st.beta_container()
    with container_policycountry:   
        
        st.header("Evolution of the policy per country")
        
        
        st.markdown('''
        
        In addition, we can evaluate and compare the evolution of the selected policy among different countries of our interest:
        
        ''')
    
    
        ## Selector of countries
        
        countries = st.multiselect('Select countries to plot',
                                    control_df.groupby('Country').count().reset_index()['Country'].tolist(),
                                    default=['China', 'India', 'France'])
        
        st.markdown('''
        
    
        ''') 
        
        xscale_barchart = alt.Scale(domain=(0, 5))
        
        ## Comparisson Chart of Policies per country
        
        barchart_country = alt.Chart(control_dataset,width=90,height=20,
                                     title = 'Evolution of Policy "' + metric_name + '" per selected countries'
        ).mark_bar( size = 20
        ).encode(
            alt.X('value:Q', scale = xscale_barchart, title = "", axis = alt.Axis(grid = False)),
            alt.Row('Country:N', title = "", spacing = 5, header = alt.Header(labelAngle = 0, labelAlign = "left",labelLimit = 100)),
            alt.Column("Year:O", title = "", spacing = 10),
            color=alt.Color("value:Q", 
                            scale=alt.Scale(domain=[1,4], range=['#ffe8dd','#ef4f4f']),
                            legend=None),
            tooltip=[
                alt.Tooltip("Country:N", title="Country"),
                alt.Tooltip(metric_to_show_in_covid_Layer, title=metric_name),
                alt.Tooltip("Year:O", title="Year"),
            ]
        ).transform_fold(
            [metric_name],
            as_ = ['Measurement_type', 'value']
        ).transform_filter(
            alt.FieldOneOfPredicate(field="Country", oneOf=countries)
        ).transform_filter(
            {'field': 'Year', 'range': [2008,2018]}
        ).configure_title(align = "center", anchor = "middle", dy = -10)
            
            
        st.altair_chart(barchart_country)
        
        st.altair_chart(legend_chart)
        
    
    ####### Scatterplot control policy vs deaths
    
    def render_latex(formula, fontsize=10, dpi=100):
        """Renders LaTeX formula into Streamlit."""
        fig = plt.figure()
        text = fig.text(0, 0, '$%s$' % formula, fontsize=fontsize)
    
        fig.savefig(BytesIO(), dpi=dpi)  # triggers rendering
    
        bbox = text.get_window_extent()
        width, height = bbox.size / float(dpi) + 0.05
        fig.set_size_inches((width, height))
    
        dy = (bbox.ymin / float(dpi)) / height
        text.set_position((0, -dy))
    
        buffer = BytesIO()
        fig.savefig(buffer, dpi=dpi, format='jpg')
        plt.close(fig)
    
        st.image(buffer)
    
    
    
    container_correlation = st.beta_container()
    with container_correlation: 
    
        st.header("Are the policies having an impact in the deaths by Smoking?")
        
        st.markdown('''
        Countries have implemented different control policies against Tobacco which have been measured by WHO from 2008 until 2018. 
        During this period, some countries have strengthen their policies; however, we don't know the real impact of them.
        
        As a consequence, the following visualization measures the correlation of the change in control policies with 
        respect to the change in deaths by Smoking. The definitions of % of change are the following:
            
        ''')    
        
        render_latex(r'\%\ change\ in\ '+metric_name+r'\ =\ \frac{'+metric_name+r'\ in\ 2016}{'+metric_name+r'\ in\ 2008}')
        
        render_latex(r'\%\ change\ in\ Deaths\ by\ Smoking\ =\ \frac{Deaths\ by\ Smoking\ in\ 2016}{Deaths\ by\ Smoking\ in\ 2008}')
        
        st.markdown('''
        The user can also select brush the histograms in order to filter the points and 
        evaluate the slope of the regression in more detail (with groups that increased more or less in control policies, for example)
        
        
        ''')
    
        brush = alt.selection_interval()
        
        ## Data Transformations
        
        base_scatter = alt.Chart(control_dataset).transform_lookup(
                lookup="ID",
                from_=alt.LookupData(deaths_dataset, "ID", ["deaths","Year"])
        ).transform_calculate(
            deaths='parseFloat(datum.deaths)',
            year='parseInt(datum.Year)',
            metric = alt.datum[metric_name]
        ).transform_calculate(
            deaths_2016='datum.year==2016?datum.deaths:0',
            deaths_2008='datum.year==2008?datum.deaths:0',
            metric_2016='datum.year==2016?datum.metric:0',
            metric_2008='datum.year==2008?datum.metric:0',
            year='parseInt(datum.Year)',
            sizepoint = '2'
        ).transform_aggregate(
            deaths_2016='sum(deaths_2016)',
            metric_2016='sum(metric_2016)',
            deaths_2008='sum(deaths_2008)',
            metric_2008='sum(metric_2008)',
            groupby=["Country"]
        ).transform_calculate(
            incr_ratio_deaths='((datum.deaths_2016/datum.deaths_2008)-1)*100',
            incr_ratio_metric='((datum.metric_2016/datum.metric_2008)-1)*100',
        )
        
        xscale = alt.Scale(domain=(-100, 300))
        yscale = alt.Scale(domain=(-100, 200))
        
        
        ## Scatterplot of changes in Policy and changes in deaths
        
        points_scatter = base_scatter.mark_point(size=50, stroke="#ef4f4f").encode(
            alt.X('incr_ratio_metric:Q', scale = xscale, title = '% change of efforts in ' + metric_name + ' from 2008 to 2016'),
            alt.Y('incr_ratio_deaths:Q', scale=yscale, title = '% change in deaths from 2008 to 2016'),
            #color=alt.condition(brush, alt.value('blue'), alt.value('lightgray')),  
            #opacity=alt.condition(brush, alt.value(0.75), alt.value(0.05)),
            tooltip=[
                        alt.Tooltip("deaths_2016:Q", title="Deaths in 2016"),
                        alt.Tooltip("deaths_2008:Q", title="Deaths in 2008"),
                        alt.Tooltip("Country:N", title="Country"),
                    ],
        ).properties(
            width=450,
            height=450
        ).transform_filter(brush)   
            
        regression_scatter = points_scatter.transform_regression(
                on='incr_ratio_metric', regression='incr_ratio_deaths', method = 'linear'
        ).mark_line(color='#19456b')
        
        scatter_final = (points_scatter + regression_scatter)
           
        # Histogram of changes in policy
        
        top_hist = base_scatter.mark_area(line=True, opacity=0.3).encode(
            alt.X("incr_ratio_metric:Q",
                  bin=alt.Bin(maxbins=30, extent=xscale.domain),
                  title=''
                  ),
            alt.Y('count()', title=''),
            color=alt.value("#ef4f4f")
        ).add_selection(
            brush
        ).properties(width=450 , height=100, title = "Distribution of % change in policy")
        
        # Histogram of changes in deaths    
            
        right_hist = base_scatter.mark_area(line=True, opacity=0.3).encode(
            alt.Y('incr_ratio_deaths:Q',
                  bin=alt.Bin(maxbins=20, extent=yscale.domain),
                  title='',
                  ),
            alt.X('count()', title=''),
            color=alt.value("#ef4f4f")
        ).add_selection(
            brush
        ).properties(width=110, height=450, 
                     title=alt.TitleParams(text="Distribution of % change in deaths", align="center", angle = 90, orient = 'right')
        )
        
        st.altair_chart((top_hist & (scatter_final |right_hist )
            ).properties(title = "Correlation between % change in policy and % change in deaths"
            ).configure_title(align = "center", anchor = "middle", dy = -10))
def make_individual_metric_chart(metric, name):
    """
    Makes a chart for a single metric and a single provider.
    
    Assumes: dataframe 'df' that has all the data from CSVs
    Assumes: dataframes 'names' and 'metrics' for lookups
    """

    providerdf = df[(df["Metric"] == metric) & (df["Type"] == "Individual") &
                    (df["Name"] == name)]

    # Lookup clinic from the provider name in the names dataframe.
    # Make a comparison dataframe.
    clinic_name = names[names.Name == name].iloc[0].Clinic
    clinicdf = df[(df["Metric"] == metric) & (df["Name"] == clinic_name)]

    # Lookup the metric target -- not all metrics have a target.
    metric_target = metrics[metrics.Metric == metric].iloc[0].Target

    # If there's a target value, we'll make a rule on graph.
    if metric_target:
        metricdf = pd.DataFrame([{
            "TargetValue": metric_target,
            "Title": "Target"
        }])

    # Make a Current dataframe to use for Strip Chart.
    current_date = max(providerdf["Date"])
    current_metric = df[(df["Metric"] == metric)
                        & (df["Type"] == "Individual")
                        & (df["Date"] == current_date)]

    # Altair Graphs to combine.
    provider_progress_line = (alt.Chart(providerdf).mark_line(
        strokeWidth=4).encode(
            alt.X("Date:T", title=""),
            alt.Y(
                "Percentage:Q",
                axis=alt.Axis(format="%", title=""),
                scale=alt.Scale(domain=(0, 1)),
            ),
            color=alt.Color("Name:N", legend=None),
        ).properties(width=200, height=200))

    # 1f77b4 -- blue, #ff7f0e -- orange. Currently hardcoded but eventually
    # will look up a clinic color. Also will be used in the HTML generation.

    clinic_progress_line = (alt.Chart(clinicdf).mark_line(
        strokeWidth=2).encode(
            alt.X("Date:T", title=""),
            alt.Y(
                "Percentage:Q",
                axis=alt.Axis(format="%", title=""),
                scale=alt.Scale(domain=(0, 1)),
            ),
            color=alt.ColorValue("#ff7f0e"),
        ))

    if metric_target:
        metric_target_rule = (alt.Chart(metricdf).mark_rule(
            strokeWidth=1, strokeDash=[4, 2]).encode(y="TargetValue:Q",
                                                     color=alt.value("green"),
                                                     opacity=alt.value("0.5")))

    fcn_current_strip_chart = (alt.Chart(current_metric).mark_tick(
        color="#eee").encode(
            alt.Y(
                "Percentage:Q",
                axis=alt.Axis(format="%", title="", labels=False),
                scale=alt.Scale(domain=(0, 1)),
            )).properties(height=200))

    provider_highlight_strip = (alt.Chart(current_metric).mark_tick().encode(
        alt.Y("Percentage:Q"), opacity=alt.value("1.0")).transform_filter(
            alt.FieldEqualPredicate(field="Name", equal=name)))

    provider_percent = (provider_highlight_strip.mark_text(
        align="left", baseline="middle", dx=15, size=20).encode(
            text=alt.Text("Percentage:Q", format=".2%")).transform_filter(
                alt.FieldEqualPredicate(field="Name", equal=name)))

    if metric_target:
        chart = provider_progress_line + clinic_progress_line + metric_target_rule | (
            fcn_current_strip_chart + provider_highlight_strip +
            provider_percent)
        return chart
    else:
        chart = provider_progress_line + clinic_progress_line | (
            fcn_current_strip_chart + provider_highlight_strip +
            provider_percent)
        return chart
def concat_earnings_vis(year, geo):
    all_ear_evo = alt.Chart(eco, title="All industries").mark_area(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
            x=alt.X('Year',
                    title='CA$',
                    axis=alt.Axis(tickCount=5,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format='Y')),
            y=alt.Y('average(All industries):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=('$,f')),
                    title=None),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(All industries):Q',
                            format=('$,'),
                            title='Earnings')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Geography',
                                        equal=geo)).transform_filter(
                                            alt.FieldRangePredicate(
                                                'Year', [2002, year]))

    all_ear_gr_evo = alt.Chart(eco_gr, title="Growth rates").mark_bar(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue'),
        size=16).encode(x=alt.X('Year',
                                title='Year',
                                axis=alt.Axis(tickCount=5,
                                              titleFontSize=20,
                                              grid=False,
                                              ticks=False,
                                              format='Y')),
                        y=alt.Y('average(All industries):Q',
                                axis=alt.Axis(tickCount=3,
                                              grid=False,
                                              ticks=False,
                                              format=('%')),
                                title=None),
                        color=alt.condition((alt.datum.Year == year),
                                            alt.value('darkblue'),
                                            alt.value('lightblue')),
                        tooltip=[
                            alt.Tooltip('Geography',
                                        title='Province/territory'),
                            alt.Tooltip('Year'),
                            alt.Tooltip('average(All industries):Q',
                                        format=('.2%'),
                                        title='Growth rate')
                        ]).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Geography',
                                equal=geo)).transform_filter(
                                    alt.FieldRangePredicate(
                                        'Year', [2002, year]))

    goods_ear_evo = alt.Chart(eco, title="Goods-producing sector").mark_area(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
            x=alt.X('Year',
                    title='CA$',
                    axis=alt.Axis(tickCount=5,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format='Y')),
            y=alt.Y('average(Goods-producing sector):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=('$,f')),
                    title=None),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(Goods-producing sector):Q',
                            format=('$,'),
                            title='Earnings')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Geography',
                                        equal=geo)).transform_filter(
                                            alt.FieldRangePredicate(
                                                'Year', [2002, year]))

    goods_ear_gr_evo = alt.Chart(eco_gr, title="Growth rates").mark_bar(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue'),
        size=16).encode(x=alt.X('Year',
                                title='Year',
                                axis=alt.Axis(tickCount=5,
                                              titleFontSize=20,
                                              grid=False,
                                              ticks=False,
                                              format='Y')),
                        y=alt.Y('average(Goods-producing sector):Q',
                                axis=alt.Axis(tickCount=3,
                                              grid=False,
                                              ticks=False,
                                              format=('%')),
                                title=None),
                        color=alt.condition((alt.datum.Year == year),
                                            alt.value('darkblue'),
                                            alt.value('lightblue')),
                        tooltip=[
                            alt.Tooltip('Geography',
                                        title='Province/territory'),
                            alt.Tooltip('Year'),
                            alt.Tooltip('average(Goods-producing sector):Q',
                                        format=('.2%'),
                                        title='Growth rate')
                        ]).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Geography',
                                equal=geo)).transform_filter(
                                    alt.FieldRangePredicate(
                                        'Year', [2002, year]))

    serv_ear_evo = alt.Chart(eco, title="Service-producing").mark_area(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue')).encode(
            x=alt.X('Year',
                    title='CA$',
                    axis=alt.Axis(tickCount=5,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format='Y')),
            y=alt.Y('average(Service-producing sector):Q',
                    axis=alt.Axis(tickCount=3,
                                  titleFontSize=20,
                                  grid=False,
                                  ticks=False,
                                  format=('$,f')),
                    title=None),
            tooltip=[
                alt.Tooltip('Geography', title='Province/territory'),
                alt.Tooltip('Year'),
                alt.Tooltip('average(Service-producing sector):Q',
                            format=('$,'),
                            title='Earnings')
            ]).transform_filter(
                alt.FieldEqualPredicate(field='Geography',
                                        equal=geo)).transform_filter(
                                            alt.FieldRangePredicate(
                                                'Year', [2002, year]))

    serv_ear_gr_evo = alt.Chart(eco_gr, title="Growth rates").mark_bar(
        point=alt.OverlayMarkDef(filled=False, fill='darkblue'),
        size=16).encode(x=alt.X('Year',
                                title='Year',
                                axis=alt.Axis(tickCount=5,
                                              titleFontSize=20,
                                              grid=False,
                                              ticks=False,
                                              format='Y')),
                        y=alt.Y('average(Service-producing sector):Q',
                                axis=alt.Axis(tickCount=3,
                                              grid=False,
                                              ticks=False,
                                              format=('%')),
                                title=None),
                        color=alt.condition((alt.datum.Year == year),
                                            alt.value('darkblue'),
                                            alt.value('lightblue')),
                        tooltip=[
                            alt.Tooltip('Geography',
                                        title='Province/territory'),
                            alt.Tooltip('Year'),
                            alt.Tooltip('average(Service-producing sector):Q',
                                        format=('.2%'),
                                        title='Growth rate')
                        ]).transform_filter(
                            alt.FieldEqualPredicate(
                                field='Geography',
                                equal=geo)).transform_filter(
                                    alt.FieldRangePredicate(
                                        'Year', [2002, year]))

    w = 300
    h = 200

    serv_ear_gr_evo = serv_ear_gr_evo.properties(width=w, height=h)

    serv_ear_evo = serv_ear_evo.properties(width=w, height=h)
    goods_ear_gr_evo = goods_ear_gr_evo.properties(width=w, height=h)

    goods_ear_evo = goods_ear_evo.properties(width=w, height=h)

    all_ear_gr_evo = all_ear_gr_evo.properties(width=w, height=h)

    all_ear_evo = all_ear_evo.properties(width=w, height=h)

    all_ear = (all_ear_evo | all_ear_gr_evo)
    goods_ear = (goods_ear_evo | goods_ear_gr_evo)
    serv_ear = (serv_ear_evo | serv_ear_gr_evo)
    earnings_plot = ((all_ear & serv_ear) & goods_ear).configure_view(
        strokeOpacity=0).configure_axis(domain=False).configure_title(
            fontSize=30)

    return earnings_plot.to_html()
        alt.Tooltip(metric_to_show_in_covid_Layer, title=metric_name),
        alt.Tooltip("year:Q", title="Year"),
    ],
).transform_calculate(
    d2008="1", d2010="1", d2012="1", d2014="1", d2016="1",
    d2018="1").transform_fold(columns, as_=[
        'year', 'metric'
    ]).transform_calculate(
        yearQ='replace(datum.year,"d","")').transform_calculate(
            key_val='datum.properties.name + datum.yearQ').transform_lookup(
                lookup="key_val",
                from_=alt.LookupData(
                    control_dataset, "ID",
                    [metric_name, "Year"])).transform_calculate(
                        year='parseInt(datum.Year)', ).transform_filter(
                            alt.FieldEqualPredicate(field='year',
                                                    equal=select_year))

with container_map:
    st.altair_chart(map_geojson + choro)

####### Scatterplot control policy vs deaths

st.header("Are control policies effective?")
'''
Countries have implemented different control policies against Tobacco which have been measured by WHO from 2008 until 2018. 
During this period, some countries have strengthen their policies; however, we don't know the real impact of them in the quality 
of citizen's life.

As a consequence, we have developed the next visualization to measure the efficiency of each change in control policies with 
respect to the change in deaths because of Smoking. We consider "change" as the percentage of variation
between 2008 and 2016. The user can also select brush the histograms in order to filter the points and 
Exemple #25
0
def idca_statistics(
    var,
    bw_method="scott",
    choice=None,
    avail=None,
    wgt=None,
    scale_ch=True,
    bins=10,
    resolution=100,
):
    stats, plot_data = idca_statistics_data(
        var,
        bw_method=bw_method,
        choice=choice,
        avail=avail,
        wgt=wgt,
        scale_ch=scale_ch,
        bins=bins,
        resolution=resolution,
    )
    name = var.name or "Variable"
    selection = alt.selection_single(fields=["Alternative"], bind="legend", empty="all")
    selection2 = alt.selection_single(
        fields=["Alternative"], bind="legend", empty="none"
    )
    ax = alt.Chart(plot_data)
    alt_names = list(str(i.data) for i in var["alt_names"])
    # A dropdown filter
    # options = [None] + list(str(i.data) for i in var['alt_names'])
    # options_dropdown = alt.binding_select(options=options)
    # options_select = alt.selection_single(fields=['Alternative'], bind=options_dropdown, name="Alternative", empty="all")

    frequency = (
        ax.mark_line(interpolate="step",)
        .transform_filter(
            {"not": alt.FieldEqualPredicate(field="Alternative", equal=_GENERIC)}
        )
        .encode(
            x=name,
            y="Frequency",
            color=alt.Color(
                "Alternative",
                sort=alt_names,
                legend=alt.Legend(title="Alternative", orient="left"),
            ),
            # strokeDash="Alternative",
            # tooltip="Alternative",
            opacity=alt.condition(selection, alt.value(1), alt.value(0.0)),
        )
        .add_selection(selection)
    )
    density = (
        ax.mark_line()
        .encode(
            x=name,
            y=alt.Y("Density", title="Density"),
            color=alt.Color("Alternative", legend=None, sort=alt_names),
            # strokeDash=alt.Color("Alternative", legend=None),
            # tooltip="Alternative",
            opacity=alt.condition(selection2, alt.value(1), alt.value(0.0)),
        )
        .add_selection(selection2)
        .transform_filter(selection2)
    )
    generic_density = (
        ax.mark_line()
        .encode(
            x=name, y=alt.Y("Density", title="Density"), color=alt.ColorValue("black"),
        )
        .transform_filter(
            {
                "and": [
                    alt.FieldEqualPredicate(field="Alternative", equal=_GENERIC),
                    selection,
                ]
            }
        )
    )
    density = density + generic_density

    annotation = (
        ax.mark_text(
            align="right",
            baseline="top",
            # fontSize=20,
            # dx=7
        )
        .encode(
            text="Alternative",
            x=alt.X(field=name, aggregate="max", type="quantitative", title=name),
            y=alt.Y(field="Density", aggregate="max", type="quantitative"),
        )
        .transform_filter(
            {
                "and": [
                    alt.FieldEqualPredicate(field="Alternative", equal=_GENERIC),
                    selection,
                ]
            }
        )
    )
    density = density + annotation

    if choice is not None:
        f_areas = (
            ax.mark_area(interpolate="step",)
            .encode(
                x=name,
                y=alt.Y("FrequencyChosen", title="Frequency"),
                color=alt.Color("Alternative", legend=None, sort=alt_names),
                # strokeDash="Alternative",
                # tooltip="Alternative",
                opacity=alt.condition(selection2, alt.value(0.5), alt.value(0.0)),
            )
            .transform_filter(selection2)
        )
        frequency = frequency + f_areas
        d_areas = (
            ax.mark_area()
            .encode(
                x=name,
                y=alt.Y("DensityChosen", title="Density"),
                color=alt.Color("Alternative", legend=None, sort=alt_names),
                # strokeDash="Alternative",
                # tooltip="Alternative",
                opacity=alt.condition(
                    selection2, alt.value(1 if scale_ch else 0.5), alt.value(0.0)
                ),
            )
            .transform_filter(selection2)
        )
        density = density + d_areas
        generic_d_area = (
            ax.mark_area()
            .encode(
                x=name,
                y=alt.Y("DensityChosen", title="Density"),
                opacity=alt.value(0.5),
                color=alt.ColorValue("black"),
            )
            .transform_filter(
                {
                    "and": [
                        alt.FieldEqualPredicate(field="Alternative", equal=_GENERIC),
                        selection,
                    ]
                }
            )
        )
        density = density + generic_d_area

    chart = frequency.properties(height=160, width=400) & density.properties(
        height=90, width=400
    )

    table_data = stats.to_dataframe().reset_index().reset_index()
    table_data["mean"] = table_data["mean"].apply(lambda x: f"{x:.4g}")
    table_data["std"] = table_data["std"].apply(lambda x: f"{x:.4g}")
    tab = alt.Chart(table_data)

    def make_col(colname, title):
        return (
            tab.mark_text()
            .encode(y=alt.Y("index:O", axis=None), text=colname, size=alt.value(10),)
            .properties(title=alt.TitleParams(text=title, align="center", fontSize=10),)
        )

    col1 = make_col("alt_names", "Alternative")
    col2 = make_col("mean", "Mean")
    col3 = make_col("std", "Std Dev")

    return (chart & (col1 | col2 | col3)).configure_view(strokeWidth=0), stats
def plot_psychometric(subject_protocols):
    """
    Plot psychometric curves for selected subjects, steps, and variables

    Typically called by :meth:`.Terminal.plot_psychometric`.

    Args:
        subject_protocols (list): A list of tuples, each with

            * subject_id (str)
            * step_name (str)
            * variable (str)

    Returns:
        :class:`altair.Chart`
    """

    for subject, step, var, n_trials in subject_protocols:
        # load subject dataframe and subset
        asub = Subject(subject)
        sub_df = asub.get_trial_data(step)

        if n_trials > 0:
            sub_df = sub_df[-n_trials:]

        # pdb.set_trace()

        sub_df = coerce_discrete(sub_df, 'response')
        logit = calc_psychometric(sub_df, var)

        # generate points to plot regression fit
        logit_x = np.linspace(sub_df[var].min(), sub_df[var].max(),
                              100).reshape(-1, 1)
        logit_y = logit.predict_proba(logit_x)

        # pdb.set_trace()
        this_logit_df = pd.DataFrame({'x': logit_x[:, 0], 'y': logit_y[:, 1]})
        this_logit_df['subject'] = subject
        this_logit_df['type'] = 'log_regression'

        try:
            logit_df = pd.concat([logit_df, this_logit_df])
        except NameError:
            logit_df = this_logit_df

        # and also mean accuracy by var
        sub_df_sum = sub_df.groupby(var).mean().reset_index()[[
            var, 'response'
        ]]
        sub_df_sum['subject'] = subject
        sub_df_sum['type'] = 'responses'
        sub_df_sum.rename(columns={var: 'x', 'response': 'y'}, inplace=True)

        try:
            sum_df = pd.concat([sum_df, sub_df_sum])
        except NameError:
            sum_df = sub_df_sum

    # combine dataframes
    combo_df = pd.concat([sum_df, logit_df])

    acc_points = alt.Chart().encode(
        alt.X('x:Q', scale=alt.Scale(type='sqrt'), title=var),
        y=alt.Y('y:Q', title="mean response")).transform_filter(
            alt.FieldEqualPredicate('responses', 'type')).mark_point()

    log_curves = alt.Chart().encode(
        alt.X('x:Q', scale=alt.Scale(type='sqrt'), title=var),
        y=alt.Y('y:Q', title="mean response")).transform_filter(
            alt.FieldEqualPredicate('log_regression', 'type')).mark_line()

    combo = alt.layer(acc_points + log_curves,
                      data=combo_df).facet(row='subject:N')
    #combo.sav
    return combo