Esempio n. 1
0
    def generate_tooltip(self, format: str = "") -> alt.Tooltip:
        """Wrapper arount alt.Tooltip creation.

        Args:
            format (str): Desired format within tooltip

        Returns:
            An instance of alt.Tooltip containing relevant information from the PlotComponent class.
        """
        return alt.Tooltip(
            field=self.name,
            type=self.alt_type,
            title=self.title,
            format=format,
        )
def display_main_chart(metrics_df,
                       metric_id,
                       metric_name,
                       asset_names,
                       container,
                       scale="linear"):
    # the selection brush oriented on the x-axis
    # important not here had to comment out the interactive function below
    # to convert the graph to static
    brush = alt.selection(type='interval', encodings=['x'])
    base = alt.Chart(metrics_df).properties(width=800)
    chart = base.mark_line().encode(x=alt.X("time",
                                            type="temporal",
                                            title="Time"),
                                    y=alt.Y(metric_id,
                                            type="quantitative",
                                            title=metric_name,
                                            scale=alt.Scale(type=scale),
                                            stack=True),
                                    opacity=alt.condition(
                                        brush, alt.OpacityValue(1),
                                        alt.OpacityValue(0.7)),
                                    tooltip=[
                                        alt.Tooltip("time",
                                                    type="temporal",
                                                    title="Time"),
                                        alt.Tooltip(metric_id,
                                                    type="quantitative",
                                                    title=metric_name)
                                    ],
                                    color="Name").add_selection(brush)
    line = base.mark_rule().encode(y=alt.Y(f"average({metric_id}):Q"),
                                   size=alt.SizeValue(3),
                                   color="Name").transform_filter(brush)

    container.write(chart + line)
def question_senti_barplot(senti_df):
    """Barplot for individual question's sentiment."""
    senti_plot = (alt.Chart(senti_df).mark_bar().encode(
        alt.Y("questions", title="Questions", sort="-x"),
        alt.X(cts.SENTI, title="Sentiment"),
        tooltip=[alt.Tooltip(cts.SENTI, title="Sentiment")],
        opacity=alt.value(0.7),
        color=alt.condition(
            alt.datum[cts.SENTI] > 0,
            alt.value("steelblue"),
            alt.value("red"),
        ),
    ).properties(width=700, height=450)).interactive()

    return senti_plot
Esempio n. 4
0
def pair_plot(df, x_str, y_str, scale_str, tooltip_cols=[]):
    scale_params = {
        'zero': False
    } if scale_str == 'linear' else {
        'type': 'log',
        'domain': [1, 10000000]
    }

    return alt.Chart(df).mark_point().encode(
        x=alt.X(x_str, scale=alt.Scale(type='log')),
        y=alt.Y(y_str, scale=alt.Scale(**scale_params)),
        tooltip=[
            alt.Tooltip(col, title=col.split(':')[0]) for col in tooltip_cols
        ],
        color=alt.Color('statename:N')).properties(width=600, height=400)
Esempio n. 5
0
def bar_chart(data, column):
    ''' create bar charts for displaying categorical data
        :param data:    data from which to display
        :param column:  column on which to create histogram
        :return:        altair bar plot '''

    return alt.Chart(data).mark_area().encode(
        x=alt.X(column, sort='-y', axis=alt.Axis(labels=False, grid=False)),
        y=alt.Y('count():Q', axis=alt.Axis(grid=False),
                title='Clusters (ordered by size)'),
        tooltip=[alt.Tooltip('count()', title='Number of ads in cluster')]
    ).properties(
        width=600,
        height=400
    )
Esempio n. 6
0
def html_demande_type_presta():
    df = create_df_service_amount()
    df.loc[df.ttc_amount.isnull(), 'payment_status'] = 'Refused'
    df.loc[~df.ttc_amount.isnull(), 'payment_status'] = 'Accepted'
    df.label_services = df.label_services.str.title()

    show_comp = False
    plot = alt.Chart(
        df, title='Nombre de vente/refus par prestation').mark_bar().encode(
            y=alt.Y('label_services:N',
                    title='Types prestations',
                    sort=alt.EncodingSortField(field="ht_amount",
                                               op="count",
                                               order='descending')),
            x=alt.X('count()', title='Nombre de ventes'),
            color=alt.Color('compagnie' if show_comp else 'payment_status',
                            title='Status paiement'),
            tooltip=[
                alt.Tooltip('distinct(compagnie)', title='Nbr entreprise'),
                alt.Tooltip('count()', title='Nbr devis')
            ],
        )
    plot.save('app/templates/plot/analyse_vente_type_presta.html')
    return plot
Esempio n. 7
0
 def _transform_encoding(self, encoding, value):
     import altair as alt
     if isinstance(value, dict):
         value = dict(value)
         for kw, val in value.items():
             if kw == 'scale':
                 if isinstance(val, list):
                     val = alt.Scale(range=val)
                 else:
                     val = alt.Scale(**val)
             if kw == 'tooltip':
                 val = [alt.Tooltip(**v) for v in val]
             value[kw] = val
         value = getattr(alt, encoding.capitalize())(**value)
     return value
def make_customer_satisfaction(store_id='A'):

    # Create customer satisfaction heat map (unfiltered by store)

    customer_satisfaction = (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('mean(Rating):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('mean(Rating):Q',
                        title='Average satisfaction',
                        format='.2f')
        ]).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 satisfaction'))

    return customer_satisfaction
Esempio n. 9
0
def create_top_n_barplot(world_source: pd.DataFrame) -> alt.Chart:
    """Return alt.Chart stacked barplot of top `n` most affected countries.

    Parameters
    ----------
    world_source : pd.DataFrame

    Returns
    -------
    top_n_bar : alt.Chart
    """
    most_affected = get_most_affected(world_source, n=10)
    top_n_bar = (alt.Chart(most_affected).mark_bar().encode(
        y=alt.Y("country_region:N", title="", sort="-x"),
        x=alt.X("sum(count):Q", title="Count"),
        color=alt.Color("status:N", scale=alt.Scale(scheme="tableau20")),
        tooltip=[
            alt.Tooltip("country_region:N", title="Country"),
            alt.Tooltip("sum(count):Q", title="Count"),
            alt.Tooltip("status:N", title="Status"),
        ],
    ).properties(title="10 most affected nations", width=600,
                 height=300).configure_legend(orient="top"))
    return top_n_bar
Esempio n. 10
0
def make_module_coverage_heatmap(module_coverage, mag_order=None):
    num_mags_in_frame = len(set(module_coverage['genome']))
    c = alt.Chart(module_coverage, title='Module').encode(
        x=alt.X('module_name',
                title=None,
                sort=None,
                axis=alt.Axis(labelLimit=0, labelAngle=90)),
        y=alt.Y('genome',
                title=None,
                sort=mag_order,
                axis=alt.Axis(labelLimit=0)),
        tooltip=[
            alt.Tooltip('genome', title='Genome'),
            alt.Tooltip('module_name', title='Module Name'),
            alt.Tooltip('steps', title='Module steps'),
            alt.Tooltip('steps_present', title='Steps present')
        ]).mark_rect().encode(
            color=alt.Color('step_coverage',
                            legend=alt.Legend(title='% Complete'),
                            scale=alt.Scale(domain=(0, 1)))).properties(
                                width=HEATMAP_CELL_WIDTH *
                                len(HEATMAP_MODULES),
                                height=HEATMAP_CELL_HEIGHT * num_mags_in_frame)
    return c
Esempio n. 11
0
def plot_pathway_by_virus():

    pathways = wikidata_from_file("queries/covidpathways.rq")

    pathways_transf = (pathways.groupby(
        ["virusLabel", "pathwayLabel",
         "wikiPathID"]).count().reset_index().drop(
             ["componentLabel", "virus", "pathway"], axis=1).replace(
                 {
                     "severe acute respiratory syndrome-related coronavirus":
                     "SARS-related CoV"
                 },
                 regex=True,
             ))

    pathways_transf["url"] = (
        "https://www.wikipathways.org/index.php/Pathway:" +
        pathways_transf["wikiPathID"])

    plot = (
        alt.Chart(pathways_transf).mark_circle().encode(
            # x=alt.X("virusLabel:N", title="# of components"),
            y=alt.Y("wikiPathID:N", title=None),
            x=alt.X("virusLabel:N", title=None),
            color=alt.Color("virusLabel:N", legend=None),
            size=alt.Size("component:Q",
                          legend=None,
                          scale=alt.Scale(range=[100, 300])),
            href="url:N",
            tooltip=[
                alt.Tooltip("component:Q", title="# of components"),
                alt.Tooltip("pathwayLabel:N", title="Pathway"),
            ],
        ))

    return plot
Esempio n. 12
0
def plot_immunity(data):
    df = data.data[[
        'Date', 'Immunity (Lower Bound)', 'Immunity (Upper Bound)'
    ]].copy()
    df = df.set_index('Date').rolling(window='14d').mean().reset_index()

    chart = alt.Chart(df).mark_area(opacity=0.5).encode(
        x='Date:T',
        y=alt.Y('Immunity (Lower Bound):Q',
                title='Immunity %',
                axis=alt.Axis(format='%')),
        y2=alt.Y2('Immunity (Upper Bound):Q', title=''),
        tooltip=alt.Tooltip('Date'))

    return chart
Esempio n. 13
0
    def plot_bar_behavior(selection, y_axis=y_axis):
        """
        Plot user selected behavior by park area.

        Parameters
        ----------
        selection : alt.selection_multi() object
            selection paramter for setting up interactive multi-selection feature of the plots  

        y_axis: character
            Drop-down menu selection value of behavior for making the plot accordingly
        Returns
        -------
        b_chart
            The plot of the selected behavior.
        """

        b_chart = (alt.Chart(squirrel_b_json).mark_bar(
            color='gray').add_selection(selection).encode(
                alt.X('properties.' + y_axis + ':Q',
                      title="Squirrel Count",
                      axis=alt.Axis(labelFontSize=18, titleFontSize=20)),
                alt.Y('properties.sitename_short:N',
                      title="Park Region",
                      axis=alt.Axis(labelFontSize=14, titleFontSize=20),
                      sort=sort_order),
                opacity=alt.condition(brush, alt.value(1.0), alt.value(0.2)),
                tooltip=[
                    alt.Tooltip('properties.sitename:N', title="Park Region"),
                    alt.Tooltip('properties.' + y_axis + ':Q',
                                title="Count " + y_axis.replace('_', ' '))
                ]).properties(title="Squirrel Behavior by Park Region: " +
                              y_axis.replace('_', ' '),
                              width=pw,
                              height=ph))
        return b_chart
Esempio n. 14
0
def plot(selection, transformed_df):
    """"""
    color = alt.condition(selection, alt.Color("country:N", legend=None),
                          alt.value("lightgray"))

    base = alt.Chart(transformed_df, title="Flu cases per week").encode(
        x=alt.X("week:Q", title="Week"),
        y=alt.Y("flu_cases:Q", title="Flu Cases"),
        color=color,
        tooltip=([alt.Tooltip("flu_cases:Q", title="Total Cases")]),
    )

    line = base.mark_line().add_selection(selection).interactive(bind_y=False)

    return line, color
def show_world_scatter(df, label):
    '''
    Creates scatter plot with LOESS lines
    '''
    world_data = get_world_data(df, label)
    cases_scale = max(world_data[label])
    date_scale = max(world_data['date'])
    oldest = min(world_data['date'])
    scale_y = alt.Scale(domain=(0, cases_scale + 10000))
    scale_x = alt.Scale(domain=(oldest, (date_scale + pd.DateOffset(days=3))))

    base = alt.Chart(world_data).mark_circle(
        size=150,
        opacity=0.5).transform_fold(fold=[label], as_=['cases', 'y']).encode(
            x=alt.X('date:T', scale=scale_x, title='Date'),
            y=alt.Y('y:Q',
                    scale=scale_y,
                    title=label.replace('_', ' ').title()),
            fill=alt.Color(label + ':Q',
                           legend=alt.Legend(title=label.replace('_', ' '))),
            tooltip=[
                alt.Tooltip('date'),
                alt.Tooltip(label,
                            format=",.0f",
                            title=label.replace('_', ' '))
            ])

    loess = base + base.transform_loess('date', 'y').mark_line(size=4)
    chart = (base + loess).configure_axis(
        labelFontSize=11, titleFontSize=13,
        titleColor='grey').configure_legend(
            titleFontSize=13,
            labelFontSize=12,
        ).configure_axisX(labelAngle=-30).properties(height=350).interactive()

    st.altair_chart(chart, use_container_width=True)
Esempio n. 16
0
def generate_chart(chart_type, data, width=0, height=0):
    if data is None:
        # Use an empty-ish dict because if we use None the x axis labels rotate
        # 90 degrees. No idea why. Need to debug.
        data = {"": []}

    if not isinstance(data, pd.DataFrame):
        data = type_util.convert_anything_to_df(data)

    index_name = data.index.name
    if index_name is None:
        index_name = "index"

    data = pd.melt(data.reset_index(), id_vars=[index_name])

    if chart_type == "area":
        opacity = {"value": 0.7}
    else:
        opacity = {"value": 1.0}

    # Set the X and Y axes' scale to "utc" if they contain date values.
    # This causes time data to be displayed in UTC, rather the user's local
    # time zone. (By default, vega-lite displays time data in the browser's
    # local time zone, regardless of which time zone the data specifies:
    # https://vega.github.io/vega-lite/docs/timeunit.html#output).
    x_scale = (alt.Scale(
        type="utc") if _is_date_column(data, index_name) else alt.Undefined)
    y_scale = alt.Scale(
        type="utc") if _is_date_column(data, "value") else alt.Undefined

    x_type = alt.Undefined
    # Bar charts should have a discrete (ordinal) x-axis, UNLESS type is date/time
    # https://github.com/streamlit/streamlit/pull/2097#issuecomment-714802475
    if chart_type == "bar" and not _is_date_column(data, index_name):
        x_type = "ordinal"

    chart = (getattr(alt.Chart(data, width=width, height=height),
                     "mark_" + chart_type)().encode(
                         alt.X(index_name,
                               title="",
                               scale=x_scale,
                               type=x_type),
                         alt.Y("value", title="", scale=y_scale),
                         alt.Color("variable", title="", type="nominal"),
                         alt.Tooltip([index_name, "value", "variable"]),
                         opacity=opacity,
                     ).interactive())
    return chart
Esempio n. 17
0
def map_state(curr_day_num, state_txt, state_counties, confirmed,
              confirmed_min, confirmed_max, deaths, deaths_min, deaths_max):
    # Washington State
    base_state = alt.Chart(state_counties).mark_geoshape(
        fill='white',
        stroke='lightgray',
    ).properties(
        width=1000,
        height=800,
    ).project(type='mercator')

    # counties
    base_state_counties = alt.Chart(
        us_counties).mark_geoshape().transform_lookup(
            lookup='id',
            from_=alt.LookupData(
                confirmed[(confirmed['confirmed'] > 0)
                          & (confirmed['day_num'] == curr_day_num)], 'fips',
                ['confirmed', 'county'])).encode(
                    color=alt.Color('confirmed:Q',
                                    scale=alt.Scale(
                                        type='log',
                                        domain=[confirmed_min, confirmed_max]),
                                    title='Confirmed'),
                    tooltip=[
                        alt.Tooltip('confirmed:Q'),
                        alt.Tooltip('county:O'),
                    ],
                )

    # deaths by long, latitude
    points = alt.Chart(
        deaths[(deaths['deaths'] > 0) & (deaths['day_num'] == curr_day_num)]
    ).mark_point(opacity=0.75, filled=True).encode(
        longitude='long_:Q',
        latitude='lat:Q',
        size=alt.Size('sum(deaths):Q',
                      scale=alt.Scale(type='symlog',
                                      domain=[deaths_min, deaths_max]),
                      title='deaths'),
        color=alt.value('#BD595D'),
        stroke=alt.value('brown'),
        tooltip=[
            alt.Tooltip('lat'),
            alt.Tooltip('long_'),
            alt.Tooltip('deaths'),
            alt.Tooltip('county:O'),
        ],
    ).properties(
        # update figure title
        title=
        f'COVID-19 {state_txt} Confirmed Cases and Deaths per 100K by County [{curr_day_num}]'
    )

    return (base_state + base_state_counties + points)
Esempio n. 18
0
def monthly_returns_heatmap(report):
    monthly_returns = report.resample(
        "M")["Total Portfolio"].last().pct_change().reset_index()
    monthly_returns.columns = ["Date", "Monthly Returns"]

    chart = alt.Chart(monthly_returns).mark_rect().encode(
        alt.X("year(Date):O", title="Year"),
        alt.Y("month(Date):O", title="Month"),
        alt.Color(
            "mean(Monthly Returns)",
            title="Return",
            scale=alt.Scale(scheme="redyellowgreen")),
        alt.Tooltip("mean(Monthly Returns)",
                    format=".2f")).properties(title="Average Monthly Returns")

    return chart
Esempio n. 19
0
def chart_regression_gd(df, ws):
    df = df.copy(deep=True)
    df["Yhat"] = ws[0] + ws[1]*df["Werbeausgaben"]
    min_x, max_x = df["Werbeausgaben"].min(), df["Werbeausgaben"].max()
    min_y, max_y = -19,20#df.min().min(), df.max().max()
    points = alt.Chart(df).mark_circle(color="#434A56",size=100).encode(
            alt.X("Werbeausgaben",scale=alt.Scale(domain=(0.9*min_x,1.1*max_x))),
            alt.Y("Absatz",scale=alt.Scale(domain=(0.9*min_y,1.1*max_y))
                , title="Absatz"),
            tooltip= [alt.Tooltip(field="Yhat", title="geschätzter Absatz", type="quantitative", format=".2f")]
    )
    line = alt.Chart(df).mark_line(color="#F63366", size=2).encode(
            alt.X("Werbeausgaben"),#,scale=alt.Scale(domain=(0.9*min_x,1.1*max_x))),
            alt.Y("Yhat")#alt.Y("Absatz_Prognose")
    )
    return (points + line).properties(width=700)
Esempio n. 20
0
def make_functional_heatmap(functional_df, mag_order=None):
    # build heatmaps
    charts = list()
    for i, (group,
            frame) in enumerate(functional_df.groupby('category', sort=False)):
        # set variables for chart
        function_order = get_ordered_uniques(list(frame.function_name))
        num_mags_in_frame = len(set(frame['genome']))
        chart_width = HEATMAP_CELL_WIDTH * len(function_order)
        chart_height = HEATMAP_CELL_HEIGHT * num_mags_in_frame
        # if this is the first chart then make y-ticks otherwise none
        if i == 0:
            y = alt.Y('genome',
                      title=None,
                      sort=mag_order,
                      axis=alt.Axis(
                          labelLimit=0,
                          labelExpr="replace(datum.label, /_\d*$/gi, '')"))
        else:
            y = alt.Y('genome',
                      axis=alt.Axis(title=None, labels=False, ticks=False),
                      sort=mag_order)
        # set up colors for chart
        rect_colors = alt.Color('present',
                                legend=alt.Legend(title="Function is Present",
                                                  symbolType='square',
                                                  values=[True, False]),
                                sort=[True, False],
                                scale=alt.Scale(range=['#2ca25f', '#e5f5f9']))
        # define chart
        # TODO: Figure out how to angle title to take up less space
        c = alt.Chart(frame, title=alt.TitleParams(group)).encode(
            x=alt.X('function_name',
                    title=None,
                    axis=alt.Axis(labelLimit=0, labelAngle=90),
                    sort=function_order),
            tooltip=[
                alt.Tooltip('genome', title='Genome'),
                alt.Tooltip('category', title='Category'),
                alt.Tooltip('subcategory', title='Subcategory'),
                alt.Tooltip('function_ids', title='Function IDs'),
                alt.Tooltip('function_name', title='Function'),
                alt.Tooltip('long_function_name', title='Description'),
                alt.Tooltip('gene_symbol', title='Gene Symbol')
            ]).mark_rect().encode(y=y, color=rect_colors).properties(
                width=chart_width, height=chart_height)
        charts.append(c)
    # merge and return
    function_heatmap = alt.hconcat(*charts, spacing=5)
    return function_heatmap
Esempio n. 21
0
 def set_options(self, **kwargs):
     self.options.update(kwargs)
     if isinstance(self.options["x"], str):
         self.options["x"] = alt.X(self.options["x"])
     if isinstance(self.options["y"], str):
         self.options["y"] = alt.X(self.options["y"])
     if "tooltip" in self.options and isinstance(self.options["tooltip"], list):
         nl = []
         for t in self.options["tooltip"]:
             if isinstance(t, str):
                 nl.append(alt.Tooltip(t.replace(".", ""), title=t))
             else:
                 t.title = t.shorthand
                 t.shorthand = t.shorthand.replace(".", "")
                 nl.append(t)
         self.options["tooltip"] = nl
def create_genetic_chart(data):
    scales = alt.selection_interval(bind='scales')
    chart = alt.Chart(data, height=500, width=850).mark_line(point=True).encode(
        x=alt.X('IterationNumber:N', title='Кількість ітерацій'),
        y=alt.Y('Result',
            scale=alt.Scale(domain=[1,15]),
            title='Середня кількість кандидатів в комітеті', 
            sort=sorted(set(data['Result']), reverse=True)
            ),
        color=alt.Color('Algorithm:N',title='Алгоритм', scale=alt.Scale(scheme='category10')),
        tooltip=alt.Tooltip('Result', title='Значення ЦФ')
    ).add_selection(
        scales
    ).configure_point(
        size=80
    )
    return chart
def create_greedy_chart(data):
    scales = alt.selection_interval(bind='scales')
    chart = alt.Chart(data, height=500, width=850).mark_line(point=True).encode(
        x=alt.X('CandidatesNum:N', title='Кількість кандидатів'),
        y=alt.Y('Result',
            axis=alt.Axis(tickMinStep=0.2),
            title='Середня кількість кандидатів в комітеті', 
            sort=sorted(set(data['Result']), reverse=True)
           ),
        color=alt.Color('Algorithm:N',title='Алгоритм', scale=alt.Scale(scheme='category10')),
        tooltip=alt.Tooltip('Result', title='Значення ЦФ')
    ).add_selection(
        scales
    ).configure_point(
        size=80
    )
    return chart
Esempio n. 24
0
def close_for_how_long(yelp_join):
    st.markdown(
        "From now on, we retrive the original information in **Temporary Closed Until**, **Covid Banner**, and **highlights**."
    )

    st.write(
        "First, let's see when did these temporarly closed businesses plan to reopen in June 10. There are {} businesses uploading closure notification."
        .format(sum(yelp_covid_df['Temporary Closed Until'] != 'FALSE')))

    st.write(
        "You may select certain category you are interested in from the bottom box, and brush certain time sub-interval from the upper figure."
    )

    close_time = yelp_join[yelp_join['Temporary Closed Until'] !=
                           'FALSE']['Temporary Closed Until']
    close_time = list(close_time)
    close_time = [ele[:-5] for ele in close_time]

    category = yelp_join[yelp_join['Temporary Closed Until'] !=
                         'FALSE']['categories'].fillna('').apply(find_category)
    category = list(category)

    df = pd.DataFrame()
    df['Close Until'] = close_time
    df['Category'] = category

    brush = alt.selection_interval()
    input_dropdown = alt.binding_select(options=cate_list, name="Category of ")
    picked = alt.selection_single(encodings=["color"], bind=input_dropdown)

    base = alt.Chart(df[df['Close Until'] < '2021-01-01']).mark_area().encode(
        alt.X("Close Until:T"),
        alt.Y("count()")).properties(height=50, width=500).add_selection(brush)

    chart = base & alt.Chart(df[
        df['Close Until'] < '2021-01-01T00:00:00']).mark_bar(size=20).encode(
            alt.X("Close Until:T", scale=alt.Scale(domain=brush)),
            alt.Y("count()", title='Business number'),
            alt.Tooltip(["Close Until:T", "Category:N", "count()"]),
            color=alt.condition(picked, "Category:N", alt.value("lightgray")),
        ).add_selection(picked).properties(height=300, width=500)

    st.write(chart)
    st.write(
        "It is interesting that the planned reopen time is quite concentrated, most during June and July, and on the start or end of a certain month."
    )
Esempio n. 25
0
def chart2(x_val = 'gini_index'):
    """
    This function creates a chart for avg hate crime rate across a specified factor given as input.   
    -------------------

    Arguments:
    x_val - The factor for which we want to compare the avg hate crime rate
    -------------------

    Returns:
    A altair chart with the scatter plot for avg. hate crime rate and specified factor.

    """
    df = pd.read_csv('data/hate_crimes.csv').loc[:,[x_val,'avg_hatecrimes_per_100k_fbi','state']]
    df = df.dropna()
    
    df = pd.DataFrame({'x': df.iloc[:,0], 'y': df.iloc[:,1], 'state':df.iloc[:,2] })

    # Define the degree of the polynomial fit
    degree = 1

    # Build a dataframe with the fitted data
    poly_data = pd.DataFrame({'xfit': np.linspace(df['x'].min(), df['x'].max(), 500)})

    poly_data[str(degree)] = np.poly1d(np.polyfit(df['x'], df['y'], degree))(poly_data['xfit'])

    type_dict = {'gini_index': 'Income Disparity',
                'share_unemployed_seasonal': 'Unemployment rate seasonal',
                'share_white_poverty': 'White people poverty rate',
                'share_non_citizen': 'Percentage of Non-citizens',
                'share_population_in_metro_areas': 'Percentage of people in metro cities'}

    # Plot the data points on an interactive axis
    points = alt.Chart(df).mark_circle(color='black', size = 40, opacity = 0.6).encode(
        x=alt.X('x:Q', title=type_dict[x_val], scale = alt.Scale(domain = [min(df['x']),max(df['x'])])),
        y=alt.Y('y:Q', title='Average hate crime per 100K people'),
        tooltip = [alt.Tooltip('state:O', title = "State")]
    )

    # Plot the best fit polynomials
    polynomial_fit = alt.Chart(poly_data).mark_line().encode(
        x='xfit:Q',
        y='1:Q'
    )
    
    return (points + polynomial_fit).properties(title = 'Hate crime rate across socio-economic factors', width = 400, height = 300)
Esempio n. 26
0
 def plot_aq(date=0):
     """
     Returns map of interpolated AQ values for single date
     """
     plot_df = interp_buffer.query('date_int ==' +
                                   str(date)).reset_index(drop=True)
     interpolated_plot = (alt.Chart(plot_df).mark_geoshape().encode(
         fill=alt.Color(param,
                        scale=alt.Scale(domain=[
                            min(interp_buffer[param].dropna()),
                            max(interp_buffer[param].dropna())
                        ],
                                        type='linear',
                                        scheme='yelloworangebrown'),
                        title=units_dict[param]),
         tooltip=[
             alt.Tooltip(param,
                         title=param_title[param] + " \
                                 " + units_dict[param],
                         format='0.5')
         ]))
     if date < mar1_offset + 16:
         return ((interpolated_plot + base_map).properties(
             title="Bay Area Avg.{} ({}): {}".format(
                 param_title[param], units_dict[param],
                 plot_df.loc[0, 'date']),
             height=450,
             width=500).configure_title(fontSize=20).configure_legend(
                 titleFontSize=16, labelFontSize=14))
     else:
         annotation = alt.Chart(plot_df).mark_text(
             align='left',
             baseline='middle',
             fontSize=16,
             dx=65,
             dy=-210,
             text='Shelter-in-place enforced',
             color='red').encode()
         return ((interpolated_plot + base_map + annotation).properties(
             title="Bay Area Avg.{} ({}): {}".format(
                 param_title[param], units_dict[param],
                 plot_df.loc[0, 'date']),
             height=450,
             width=500).configure_title(fontSize=20).configure_legend(
                 titleFontSize=16, labelFontSize=14))
Esempio n. 27
0
def newcomer():
    #TOP 10 der Entitäten, die in den letzten 365 Tagen erstellt wurden
    if satzart == 'alle':
        st.subheader(f'TOP 10 GND-Newcomer')
        st.write(
            'TOP 10 der GND-Entitäten, die in den letzten 365 Tagen angelegt wurden.'
        )
        newcomer_daten = pd.read_csv(
            f'{path}/../stats/title_gnd_newcomer_top10.csv', index_col=None)

        newcomer = alt.Chart(newcomer_daten).mark_bar().encode(
            alt.X('gnd_id', title='Entitäten', sort='-y'),
            alt.Y('count', title='Anzahl'),
            alt.Color('name', sort='-y', title='Entität'),
            tooltip=[
                alt.Tooltip('name:N', title='Entität'),
                alt.Tooltip('bbg:N', title='Satzart'),
                alt.Tooltip('gnd_id:N', title='IDN'),
                alt.Tooltip('count:Q', title='Anzahl')
            ])

    else:
        st.subheader(f'TOP 10 {satzart} GND-Newcomer')
        st.write(
            f'TOP 10 der {satzart} Sätze, die in den letzten 365 Tagen angelegt wurden.'
        )
        newcomer_daten = load_gnd_top_daten('newcomer_top10')

        newcomer = alt.Chart(
            newcomer_daten.loc[newcomer_daten['bbg'].str.startswith(
                satzart[:2],
                na=False)]).mark_bar().encode(alt.X('gnd_id:O',
                                                    title='Entitäten',
                                                    sort='-y'),
                                              alt.Y('count', title='Anzahl'),
                                              alt.Color('name',
                                                        sort='-y',
                                                        title='Entität'),
                                              tooltip=[
                                                  alt.Tooltip('name:N',
                                                              title='Entität'),
                                                  alt.Tooltip('gnd_id:N',
                                                              title='IDN'),
                                                  alt.Tooltip('count:Q',
                                                              title='Anzahl')
                                              ])
    st.altair_chart(newcomer, use_container_width=True)
def plot_immunity(data):
    df = data.data[[
        "Date", "Immunity (Lower Bound)", "Immunity (Upper Bound)"
    ]].copy()
    df = df.set_index("Date").rolling(window="14d").mean().reset_index()

    chart = (alt.Chart(df).mark_area(opacity=0.5).encode(
        x="Date:T",
        y=alt.Y(
            "Immunity (Lower Bound):Q",
            title="Immunity %",
            axis=alt.Axis(format="%"),
        ),
        y2=alt.Y2("Immunity (Upper Bound):Q", title=""),
        tooltip=alt.Tooltip("Date"),
    ))

    return chart
Esempio n. 29
0
def title_plot(region_filter):
    sorted_genre = list(sales_data[sales_data.Region == region_filter].groupby(
        "Genre").sum().sort_values("Sales", ascending=False).index)
    chart = alt.Chart(
        sales_data[sales_data.Region == region_filter]).mark_circle(
            size=50).encode(
                alt.X("Genre", sort=sorted_genre, title=None),
                alt.Y("Sales:Q", stack=None, title="Sales (in millions)"),
                alt.Color("Genre", scale=alt.Scale(scheme='category20')),
                alt.Tooltip("Name"))
    chart = chart + alt.Chart(
        sales_data[sales_data.Region == region_filter].sort_values(
            "Sales", ascending=False).iloc[:5, ]).mark_text(
                align="left", dx=10).encode(
                    alt.X("Genre", sort=sorted_genre),
                    alt.Y("Sales:Q"),
                    text="Name").properties(title=region_filter)
    return chart.to_html()
def show_publisher_region_bar(df):
    st.write('## Popular Publishers')

    st.write(
        "**It might also be interesting to compare different publishers.🤔**")

    st.write("💡 *You can select specific year range with slider*")
    st.write(
        "💡 *Hover over publishers to see the number of games they released during the time*"
    )
    min_year, max_year = get_year_range(df)
    selected_year = st.slider("View Popular Publisher in Year Range:",
                              min_year, max_year, (min_year, max_year), 1)

    df_selected = df[(df["Year"] >= selected_year[0])
                     & (df["Year"] <= selected_year[1])]

    regions = [
        'Global_Sales', 'NA_Sales', 'EU_Sales', 'JP_Sales', 'Other_Sales'
    ]
    new_df = get_publisher_agg_df(df_selected, regions, 10)

    brush = alt.selection_single(encodings=['color'])

    titles = {
        'Global_Sales': 'Global',
        'NA_Sales': 'North America',
        'EU_Sales': 'Europe',
        'JP_Sales': 'Japan',
        'Other_Sales': 'Other Regions'
    }
    for region in regions:
        st.write("**%s**" % (titles[region]))
        st.write(
            alt.Chart(new_df[region]).mark_bar().encode(
                x=alt.X("Publisher:N", sort='-y'),
                y=alt.Y(region, title='Sale (in millions)'),
                color=alt.Color("Publisher:N",
                                scale=alt.Scale(scheme='tableau20')),
                tooltip=[
                    alt.Tooltip(field="Name",
                                title="Total Released Games",
                                type="quantitative")
                ]).properties(width=600, height=350))