Example #1
0
    def zoomplot(self):
        "interactive altair plot - but needs to be opened in vega editor"
        chrom = Chromosome(self.genome)
        chrom.altair()
        ichrom = chrom.ichrom

        brush = alt.selection_interval(encodings=['x'],
                                       mark=alt.BrushConfig(fill='red',
                                                            fillOpacity=0.700))

        fadedchrom = ichrom.mark_rect(opacity=0.4)

        mut_pos = pd.DataFrame({'x': self.positions})
        mut_positions = alt.Chart(mut_pos).mark_rule(
            color=alt.ColorName("mediumblue")).encode(
                x='x:Q',
                size=alt.value(1),
                tooltip=[
                    alt.Tooltip('x', title='Position'),
                ])

        layered = alt.layer(fadedchrom, mut_positions)

        zoomtest = alt.vconcat(
            layered.encode(
                alt.X('x1:Q', title=None,
                      scale=alt.Scale(domain=brush))).properties(height=80),
            layered.add_selection(brush).properties(height=30))

        print("Note: this plot must be opened in the Vega editor for "
              "interactive features to work")
        IPython.display.display_html(zoomtest)
Example #2
0
def state_pane(apple, google, cases):
    brush = alt.selection_interval(encodings=['x'])

    p1 = alt.Chart(apple).mark_line(interpolate='monotone').encode(
        x = alt.X('date:T'),
        y = alt.Y('7_day:Q', title = '7-day average'),
        color = alt.Color('transportation_type:N', scale=alt.Scale(scheme="tableau20"))
    ).add_selection(brush).properties(
        title = {'text': 'How are people getting around?',
                'subtitle': 'Source: Apple mobility data, request volumes indexed to Jan 13',
                'subtitleFontSize': 10,
                'fontSize': 16}
    )

    highlight = alt.selection(type='single', on='mouseover',
                                  fields=['destination_type'], nearest=True)

    p2 = alt.Chart(google).mark_line(interpolate='monotone').encode(
        x = alt.X('date:T', scale = alt.Scale(domain = brush)),
        y = 'volume:Q',
        color = alt.Color('destination_type:N',
                         legend = alt.Legend(title = "Destination Type",
                                            values = ['parks', 'residential', 'workplaces',
                                                     'grocery_pharmacy', 'retail_recreation',
                                                     'transit_stations']))
    ).properties(
        title = {'text': 'Where are people going?',
                'subtitle': 'Source: Google mobility data, volume relative to median Jan traffic',
                'subtitleFontSize': 10,
                'fontSize': 16}
    )

    points = p2.mark_circle().encode(
            opacity=alt.value(0)
        ).add_selection(
            highlight
        )

    lines = p2.mark_line(interpolate='monotone').encode(
            size=alt.condition(~highlight, alt.value(1), alt.value(3))
        )
    
    c1 = alt.Chart(cases).mark_bar(color = 'lightgrey').encode(
        x = alt.X('date:T', scale = alt.Scale(domain = brush)),
        y = 'cases:Q'
        ).properties(title = 'Cumulative Cases')
    
    m = cases['new_cases'].max()

    c2 = alt.Chart(cases).mark_bar(color = 'lightblue').encode(
            x = alt.X('date:T', scale = alt.Scale(domain = brush)),
            y = alt.Y('new_cases:Q', title = 'cases', scale = alt.Scale(domain = (0, m)))
        ).properties(title = 'Daily New Cases')

    plot = alt.vconcat(
        (p1 | (points + lines)),
        (c1 | c2)
    )
    
    return plot
Example #3
0
def make_interactive_chart():
    '''
    '''
    pts = alt.selection_interval(encodings=['x', 'y'])

    rect = alt.Chart(data.cars()).mark_point().encode(
        x='Miles_per_Gallon:Q',
        y='Horsepower:Q',
        color=alt.condition(pts, 'Origin',
                            alt.value('lightgray'))).properties(selection=pts)

    # scale=alt.Scale(scheme='greenblue'),
    #         legend=alt.Legend(title='Total Sample')
    #     )
    # circ = rect.mark_point().encode(
    #     alt.ColorValue('grey'),
    #     alt.Size('count()',
    #         legend=alt.Legend(title='Sample in Selection')
    #     )
    # ).transform_filter(
    #     pts
    # )

    return alt.vconcat(rect | rect.encode(x='Acceleration')).resolve_legend(
        color="independent", size="independent")
def plt_nominal(df, vizvar):

    tmp = df[vizvar].fillna("Missing").value_counts().to_frame().reset_index()
    c0 = (
        alt.Chart(tmp)
        .mark_bar()
        .encode(
            x=alt.X("index:N", axis=alt.Axis(labelAngle=45)),
            y=alt.Y("%s:Q" % vizvar, axis=alt.Axis(labelAngle=-25)),
        )
        .properties(width=GRAPH_WIDTH, height=GRAPH_HEIGHT)
    )
    c1 = (
        alt.Chart(df.fillna("Missing"))
        .mark_circle()
        .encode(
            x=alt.X("event_time:T", axis=alt.Axis(labelAngle=45)),
            y=alt.Y("%s:N" % vizvar, axis=alt.Axis(labelAngle=-25)),
            tooltip=["event_time:T", "%s:N" % vizvar],
        )
        .add_selection(
            alt.selection_interval(encodings=["x"], bind="scales", resolve="global")
        )
        .properties(width=GRAPH_WIDTH, height=GRAPH_HEIGHT)
    )
    return c0, c1
def plot_ind(df, short_var, long_var):
    '''Returns a candlestick chart designed for crossover strategy use.
    Parameters:
        df: DataFrame
        short_var/long_var: str, indicator column names
    Returns:
        Altair Interactive Chart
    '''
    scales = alt.selection_interval(bind='scales')
    move_color = alt.condition('datum.price_close - datum.price_open > 0',
                               alt.value("#047220"), alt.value("#910513"))
    price = alt.Chart(df).mark_line(size=0.5).encode(
        alt.X('date:T', title='Date'),
        alt.Y('price_mean_a:Q', title='Arithmetic Mean Price')).properties(
            width=600, title=df['base_asset_id'].iloc[0]).add_selection(scales)
    candles1 = alt.Chart(df).mark_rule().encode(alt.X('date:T', title=None),
                                                alt.Y('price_low:Q',
                                                      title=None),
                                                alt.Y2('price_high:Q',
                                                       title=None),
                                                color=move_color)
    candles2 = alt.Chart(df).mark_bar().encode(alt.X('date:T', title=None),
                                               alt.Y('price_open:Q',
                                                     title=None),
                                               alt.Y2('price_close:Q',
                                                      title=None),
                                               color=move_color)
    ind1 = alt.Chart(df).mark_line(color='#1967e5').encode(
        alt.X('date:T', title='Date'),
        alt.Y('{}:Q'.format(short_var), title=None))
    ind2 = alt.Chart(df).mark_line(color='blue').encode(
        alt.X('date:T', title='Date'),
        alt.Y('{}:Q'.format(long_var), title=None))
    return price + candles1 + candles2 + ind1 + ind2
Example #6
0
def make_figure(x_axis, y_axis):

    brush = alt.selection_interval()
    base = alt.Chart(cars)

    # scatter plot of x vs y
    scatter = (
        base.mark_point()
        .encode(x=x_axis, y=y_axis, color="Origin:N")
        .properties(width=250, height=400, selection=brush)
    )

    # histogram of horsepower
    hist = (
        base.mark_bar()
        .encode(x=alt.X("Horsepower:Q", bin=True), y="count()", color="Origin:N")
        .transform_filter(brush.ref())
    ).properties(height=375)

    chart = alt.hconcat(scatter, hist)

    # Save html as a StringIO object in memory
    cars_html = io.StringIO()
    chart.save(cars_html, "html")

    # Return the html from StringIO object
    return cars_html.getvalue()
Example #7
0
def plot_escola():
    options = np.append(['Todos'], [x for x in range(2008, 2019)] + ["Todos"])
    ano = st.selectbox('Selecione um ano:', options)
    if ano == "Todos":
        df_esc = suicide_df[["ESC"]]
        ano = "2008-2018"
    else:
        df_esc = suicide_df[suicide_df["YEAR"] == int(ano)][["ESC"]]
    df_esc = df_esc[df_esc["ESC"].isna() == 0]
    scales = alt.selection_interval(bind='scales')

    graph = alt.Chart(df_esc,
                      title="Quantidade de suicídios por escolaridade (" +
                      ano + ')').mark_bar(color="#00336E").encode(
                          alt.X("ESC", title="Escolaridade"),
                          y=alt.Y('count()', title='Quantidade'),
                          color=alt.Color('ESC:N', legend=None),
                          tooltip=[
                              alt.Tooltip('ESC', title='Escolaridade'),
                              alt.Tooltip('count()', title='Quantidade')
                          ]).add_selection(scales).interactive()

    st.altair_chart(
        (graph).configure_view(strokeOpacity=0).properties(width=700,
                                                           height=410))
Example #8
0
def age_plot(country_dropdown, source, year):
    alt.themes.enable('dark')
    data = source
    data = data[data['country'] == country_dropdown]

    data['year'] = pd.to_datetime(data['year'], format='%Y')

    year = pd.to_datetime(year, format='%Y')

    #data = data_country_filter[data_country_filter['country'] == country_dropdown]
    data = data[data['year'] > year[0]]
    data = data[data['year'] < year[1]]

    brush = alt.selection_interval()
    click = alt.selection_multi(fields=['age'], bind='legend')
    points = (alt.Chart(
        data, title='Mean Suicides per Capita').mark_line(size=7).encode(
            x='year',
            y='Average_suicides_per_capita',
            color=alt.condition(brush,
                                'age',
                                alt.value('lightgray'),
                                scale=alt.Scale(scheme='blueorange')),
            opacity=alt.condition(
                click, alt.value(0.9),
                alt.value(0.2))).add_selection(brush)).configure_axis(
                    grid=False)
    points.configure_legend(strokeColor='gray',
                            fillColor='#EEEEEE',
                            padding=2,
                            cornerRadius=2,
                            orient='bottom')
    plot = points.add_selection(click)
    return plot.to_html()
Example #9
0
    def display_img(self, args):
        self.output.clear_output()
        with self.output:
            for csv in args["new"]:
                df = pd.read_csv(csv, sep=";")
                df.columns = df.columns.str.replace(".", "_")

                dropdown = alt.binding_select(options=list(df.columns[1:]))
                selection = alt.selection_single(
                    fields=["variable"],
                    bind=dropdown,
                    name="Selection of",  # empty=df.columns[1]
                )

                color = alt.condition(
                    selection, alt.Color("variable:N"), alt.value("lightgray")
                )
                scales = alt.selection_interval(encodings=["x"], bind="scales")
                chart = (
                    alt.Chart(df.melt("time")) #TODO: should not be hardcoded
                    .mark_line()
                    .encode(x="time", y="value", color=color)
                    .add_selection(selection)
                    .transform_filter(selection)
                    .properties(width=400, height=300)
                    .add_selection(scales)
                )

                with alt.data_transformers.enable("default", max_rows=None):
                    display(chart)
def plot_chart2(size):
    brush = alt.selection_interval(encodings=['x'])
    chart = alt.Chart(hr2[hr2.company_size == size]).transform_density(
        'training_hours',
        groupby=['company_size'],
        as_=['training_hours', 'density'],
    ).mark_area(opacity=0.5).encode(
        alt.X('training_hours',
              title="Training Hours",
              axis=alt.Axis(format='~s', labelFontSize=16, titleFontSize=20)),
        alt.Y('density:Q',
              title="Hours Density",
              axis=alt.Axis(labelFontSize=16, titleFontSize=20)),
        alt.Color('company_size:N',
                  legend=alt.Legend(title='Company Size',
                                    labelFontSize=16,
                                    titleFontSize=16))).properties(width=450,
                                                                   height=400)
    full = chart.properties(height=80).add_selection(brush)
    detail = chart.encode(
        alt.X('training_hours',
              title="Training Hours",
              axis=alt.Axis(labelFontSize=16, titleFontSize=20),
              scale=alt.Scale(domain=brush)))

    chart = detail & full
    return chart.to_html()
Example #11
0
def plot_altair(hist, dist, dist_name, bin_size):

    brush = alt.selection_interval(encodings=['x'])

    data = pd.DataFrame.from_dict({
        'rf': hist,
        'p': dist
    }, orient='index').transpose().fillna(0).reset_index()

    data['index'] = data['index'] * bin_size

    base = alt.Chart(
        data, title=f'{dist_name} Estimation of EKA Goals').encode(
            alt.X(
                'index:Q',
                title='Goals Scored',
                bin=alt.Bin(step=bin_size),
                axis=alt.Axis(
                    values=[-5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55
                            ])))

    bar = base.mark_bar(opacity=.7).encode(alt.Y('rf:Q'))

    rule = base.mark_rule(size=2).encode(
        alt.X('index:Q'),
        alt.Y('p:Q', title='Relative Frequency', axis=alt.Axis(tickCount=5)))

    return alt.layer(bar,
                     rule).properties(width=600, height=500).configure_axis(
                         titleFontSize=16).configure_title(fontSize=20)
Example #12
0
def pareto_and_runtimes_by_task(df: pd.DataFrame) -> alt.Chart:
  """Creates an interactive Pareto curve and scatter plot of task runtimes.

  Tracing each curve shows to what extent a small proportion of long-running
  regions contribute disproportionately to the overall runtime. That is,
  "The longest-running X% of regions account for Y% of the total runtime."
  There is a curve for each task.

  Args:
    df: A dataframe of all regions.

  Returns:
    An altair chart.
  """
  grouped = df.groupby(df['Task'], sort=False)
  df = grouped.apply(calculate_pareto_metrics)

  # Sample along the Pareto curve, ensuring the longest regions are shown.
  if len(df) > 5000:
    x = 1000
    df = pd.concat([df.nlargest(x, 'total runtime'), df.sample(5000 - x)])

  # Limit columns to greatly reduce the size of the html report.
  columns_used = [
      'task cumsum order', 'task cumsum fraction', 'tooltip', 'Task',
      'task total runtime', 'task num examples', 'Runtime for task'
  ]
  df = df[columns_used]

  # Brushing on the task_scatter plot highlights the same tasks in the Pareto
  # curve.
  brush = alt.selection_interval()

  pareto_by_task = alt.Chart(df).mark_line(size=2).encode(
      x=alt.X(
          'task cumsum order',
          title='The longest-runtime X% of regions',
          axis=alt.Axis(format='%')),
      y=alt.Y(
          'task cumsum fraction',
          title='Account for Y% of the total runtime',
          axis=alt.Axis(format='%')),
      tooltip='tooltip',
      color=alt.condition(brush, 'Task:N', alt.value('lightgray'))).properties(
          title='Pareto curve for each task').interactive()

  # This chart needs to use the same dataframe as the first chart to enable the
  # brushing on one to affect the other. Using max(task) for 'text' is a
  # trick that causes bundling by task to avoid showing multiple overlapping
  # points which otherwise make the text look funky.
  task_scatter = alt.Chart(df).mark_point(size=10).encode(
      x=alt.X('max(task total runtime)', title='Runtime (seconds)'),
      y=alt.Y('task num examples:Q', title='Number of examples'),
      color=alt.condition(brush, 'Task:N', alt.value('lightgray')),
      tooltip=['Task', 'Runtime for task']
    ) \
    .properties(title='Total runtime for each task (drag to highlight)') \
    .add_selection(brush)

  return pareto_by_task | task_scatter
Example #13
0
def render(table: alt.Data,
           *,
           levels=5,
           alpha=0.6,
           height: int,
           width: int,
           height_minimap: int):
    zoom = alt.selection_interval(encodings=["x", "y"])
    selection = alt.selection_multi(fields=['series'], bind='legend')

    minimaps = _render_density(table,
                               x_name='',
                               levels=levels,
                               alpha=alpha,
                               selection=zoom)

    details = _render_density(table,
                              x_name='Step',
                              levels=levels,
                              alpha=alpha,
                              series_selection=selection,
                              x_scale=alt.Scale(domain={
                                  'selection': zoom.name,
                                  "encoding": "x"
                              }),
                              y_scale=alt.Scale(domain={
                                  'selection': zoom.name,
                                  "encoding": "y"
                              }))

    minimaps = minimaps.properties(width=width, height=height_minimap)
    details = details.properties(width=width, height=height)

    return details & minimaps
def movie_embedding_norm(models):
    """Visualizes the norm and number of ratings of the movie embeddings.
    Args:
      model: A MFModel object.
    """
    if not isinstance(models, list):
        models = [models]
    df = pd.DataFrame({
        'title': movies['title'],
        'genre': movies['genre'],
        'num_ratings': movies_ratings['rating count'],
    })
    charts = []
    brush = alt.selection_interval()
    for i, model in enumerate(models):
        norm_key = 'norm' + str(i)
        df[norm_key] = np.linalg.norm(model.embeddings["movie_id"], axis=1)
        nearest = alt.selection(
            type='single', encodings=['x', 'y'], on='mouseover', nearest=True,
            empty='none')
        base = alt.Chart().mark_circle().encode(
            x='num_ratings',
            y=norm_key,
            color=alt.condition(brush, alt.value('#4c78a8'), alt.value('lightgray'))
        ).properties(
            selection=nearest).add_selection(brush)
        text = alt.Chart().mark_text(align='center', dx=5, dy=-5).encode(
            x='num_ratings', y=norm_key,
            text=alt.condition(nearest, 'title', alt.value('')))
        charts.append(alt.layer(base, text))
    return altair_viewer.show(alt.hconcat(*charts, data=df))
Example #15
0
def finance_plots(N, actual_payoffs, derivative_payoffs):

    actual_payoffs = actual_payoffs.copy()
    derivative_payoffs = derivative_payoffs.copy()

    for i in range(len(actual_payoffs)):
        if i == 0:
            actual_payoffs.iloc[i] = actual_payoffs.iloc[i] - 100
            derivative_payoffs.iloc[i] = derivative_payoffs.iloc[i] - 100
        else:
            for row in range(i):
                actual_payoffs.iloc[
                    i] = actual_payoffs.iloc[i] - actual_payoffs.iloc[row]
                derivative_payoffs.iloc[i] = derivative_payoffs.iloc[
                    i] - derivative_payoffs.iloc[row]

    scales = alt.selection_interval(bind='scales')
    plots = []
    f_payoffs = derivative_payoffs - actual_payoffs
    for i in range(N):
        payoffs_df = f_payoffs.iloc[:, i].reset_index()
        payoffs_df['index'] = np.arange(1, len(payoffs_df) + 1)
        title = "Short Forward Payoff to Player " + str(i) + " per Period"
        payoff_fig = alt.Chart(payoffs_df.melt('index')).mark_bar().encode(
            x=alt.X('index:O', axis=alt.Axis(title='Time Period')),
            y=alt.Y('value', axis=alt.Axis(title='Payoff')),
            color=alt.condition(
                alt.datum.value > 0, alt.value("#6AB187"),
                alt.value('#D32D41'))).add_selection(scales).properties(
                    title=title)
        plots.append(payoff_fig)
    return plots
Example #16
0
def plot_reported_cases(county='alle_fylker',
                        start=False,
                        end=False,
                        range='day'):
    """
    Finds the reported cases and plots these for the given county, for a given
    time interval.

    args:
        county (string, optional): the Norwegian county the reported cases will be from
        start (string, optional): date indicating beginning of the plot (YYYY-MM-DD)
        end (string), optional: date indicating end of plot (YYYY-MM-DD)
    """
    data = fetch_data(county, start, end, range)
    if range == 'day':
        dato = 'Prøvetakingsdato:T'
    else:
        dato = 'Dato:T'

    brush = alt.selection_interval(encodings=['x'])
    chart = alt.Chart(data).mark_bar().encode(
        alt.X(dato, axis=alt.Axis(title='dato')),
        alt.Y('Nye tilfeller', axis=alt.Axis(title='smittetilfeller')),
        tooltip=[dato, 'Nye tilfeller:Q'],
    ).properties(title=f'{county}').add_selection(brush)
    return chart
def draw_availability90_quantitative(df, upper_quant, upper_type,
                                     upper_rename):
    brush = alt.selection_interval(encodings=["x", "y"])
    field_availability_chart = alt.Chart(df).transform_filter(
        brush).mark_circle(size=60).encode(
            alt.X(upper_quant, title=upper_rename),
            alt.Y("availability_90", title="Availability in 90 Days"),
            tooltip=[
                alt.Tooltip(upper_quant, title=upper_rename),
                alt.Tooltip("availability_90", title="Availability in 90 Days")
            ]).interactive()

    scatterplot = alt.Chart(df[df['price'] < 500]).mark_circle(
        size=100).encode(alt.X("price"),
                         alt.Y("review_scores_rating",
                               scale=alt.Scale(zero=False),
                               title='review score'),
                         tooltip=[
                             alt.Tooltip('price', format='$.2f'),
                             alt.Tooltip('review_scores_rating',
                                         title='Review Score')
                         ]).add_selection(brush)

    st.write(
        "**Brush through the lower scatterplot to filter the upper chart by review score and price.**"
    )
    st.write(field_availability_chart & scatterplot)
Example #18
0
def show_pop_w_delta(country_df):
    '''take a dataframe and return two charts, vconcat'd. 
    
    TODO: make sure its renderer is right for http with django, add call to .serve() somewhere. 
    '''
    w = 800  # width

    interval = alt.selection_interval(encodings=['x'])
    column_names = ['year', 'pop', 'delta_pop']

    chart_pop = alt.Chart(country_df, width=w).mark_bar(color='orange').encode(
        #x=alt.X('pop',  scale=alt.Scale(domain=(country_df[column_names[1]].min(), country_df[column_names[1]].max()))),
        x=alt.X('pop',
                scale=alt.Scale(domain=(0,
                                        country_df[column_names[1]].max()))),
        y=alt.Y('year', axis=alt.Axis(title='Year')),
        #y='year'
        tooltip='delta_pop').transform_filter(interval)

    chart_delta = alt.Chart(
        country_df, width=w).mark_line(color='purple').encode(
            x=alt.X('year', axis=alt.Axis(title='Year')),
            y=alt.Y('delta_pop',
                    axis=alt.Axis(format='%', title='Change in Population'))
            # y='delta_pop'
            # tooltip='pop'
        ).properties(selection=interval)

    return chart_delta & chart_pop
Example #19
0
def draw_altair_chrom_canvas_interactive(
    chrom: 'ChromosomeBase',
    width: int = 700,
    outfile: Optional[str] = None,
):
    """Return an interactive altair visualization of the chromosome.
    """
    # create a brush to select interval
    mark = alt.BrushConfig(fill='red', fillOpacity=0.700, stroke="black")
    brush = alt.selection_interval(encodings=['x'], mark=mark)

    # get the canvas drawing
    ichrom = draw_altair_chrom_canvas(chrom, width=width)

    # create an interactive composite plot with two views of ichrom
    # the first view is 2X as tall and is scaled by the brush selector
    view1 = ichrom.encode(
        alt.X('x1:Q', title=None,
              scale=alt.Scale(domain=brush))).properties(height=80)
    # the second view has the brush selector active.
    view2 = ichrom.add_selection(brush).properties(height=40)
    zoom = alt.vconcat(view1, view2, data=ichrom.data)

    # optionally write to disk
    if outfile:
        zoom.save(outfile.strip('.html') + '.html')
    return zoom
Example #20
0
    def plot_twitter_activities(data):
        """
        Function to plot the development and ratio of
        twitter activities.

        :param data: Twitter DataFrame
        """
        # Prepare data
        source = data
        # Create date column
        source['date'] = source.index
        # Calculate total twitter activities
        source['total'] = source.likes + source.retweets
        # Unpivot DataFrame
        source = source[['date', 'number_of_tweets', 'likes', 'retweets', 'total']]
        source = source.melt('date', var_name='activities', value_name='count')
        source = source.set_index(['date'])
        # Get week part from date
        source['week'] = source.index.week
        # Delete index
        source = source.reset_index()

        interval = alt.selection_interval(encodings=['x', 'y'])

        # Group data
        base = alt.Chart(source).transform_aggregate(
            total="sum(count)",
            groupby=['week', 'activities']
        )

        # Upper plot
        scatter = base.mark_line().encode(
            alt.X('week:Q', title=''),
            alt.Y('total:Q', title='Total Aktivitäten'),
            color=alt.condition(interval, 'activities:N', alt.value('lightgrey'))
        ).properties(
            selection=interval,
            height=70, width=600,
            title='Entwicklung der Twitter-Aktivitäten'
        ).transform_filter(
            filter=datum.activities == 'total'
        )

        # Lower plot
        bar = base.mark_bar(opacity=0.7).encode(
            alt.X('week:N', title='Woche'),
            alt.Y('total:Q', title='Anzahl Aktivitäten'),
            color=alt.condition(interval, 'activities:N', alt.value('lightgrey')),
        ).properties(height=200, width=600
                     ).transform_filter(
            filter=datum.activities != 'total'
        )

        # Concatenate diagrams
        chart = scatter & bar

        # Workaround for interactive charts in presentation mode
        chart.save("resources/twitter_activities.html")
Example #21
0
def get_graph(events):

    MARK_SIZE = 100
    selection = alt.selection_interval(bind="scales", resolve="global")
    color_provider = alt.Color("provider:N")
    cce = (alt.Chart(events).mark_circle(
        opacity=0.4, stroke="black", size=MARK_SIZE, strokeWidth=1).encode(
            alt.X("event_time:T", axis=alt.Axis(labelAngle=45)),
            alt.Y("sub_category:N", axis=alt.Axis(labelAngle=-25)),
            color=color_provider,
            tooltip=["key_value", "location", "provider"],
        ).properties(width=850,
                     height=300).add_selection(selection).transform_filter(
                         datum.mimic_category == "chart_events"))

    cio = (alt.Chart(events).mark_circle(
        opacity=0.4, stroke="black", strokeWidth=1, size=MARK_SIZE).encode(
            alt.X("event_time:T", axis=alt.Axis(labelAngle=45)),
            alt.Y("sub_category:N", axis=alt.Axis(labelAngle=-25)),
            color=color_provider,
            tooltip=["key_value", "location", "provider"],
        ).properties(width=850,
                     height=300).add_selection(selection).transform_filter(
                         datum.mimic_category == "io_events"))

    cne = (alt.Chart(events).mark_circle(
        opacity=0.4,
        stroke="black",
        strokeWidth=1,
        size=MARK_SIZE,
    ).encode(
        alt.X("event_time:T", axis=alt.Axis(labelAngle=45)),
        alt.Y("sub_category:N", axis=alt.Axis(labelAngle=-25)),
        color=color_provider,
        tooltip=["detail", "location", "provider"],
    ).properties(width=850,
                 height=125).add_selection(selection).transform_filter(
                     datum.mimic_category == "note_events"))

    cot = (alt.Chart(events).mark_circle(
        opacity=0.4,
        stroke="black",
        strokeWidth=1,
        size=MARK_SIZE,
    ).encode(
        alt.X("event_time:T", axis=alt.Axis(labelAngle=45)),
        alt.Y("mimic_category:N", axis=alt.Axis(labelAngle=-25)),
        color=color_provider,
        tooltip=["key_value", "detail", "provider"],
    ).properties(width=850,
                 height=125).add_selection(selection).transform_filter(
                     (datum.mimic_category == "microbiology_events")
                     | (datum.mimic_category == "med_events")
                     | (datum.mimic_category == "procedure_events")))

    plt = alt.vconcat(cce, cio, cne, cot)
    return plt
Example #22
0
    def render_density_minimap_multiple(self,
                                        datas,
                                        *,
                                        names: List[str],
                                        levels=5,
                                        alpha=0.6,
                                        height: int,
                                        width: int,
                                        height_minimap: int):
        tables = [self.__data_to_table(d) for d in datas]

        zoom = alt.selection_interval(encodings=["x", "y"])

        minimaps = None
        for i, t in enumerate(tables):
            z = zoom if i == 0 else None
            minimap = self.__render_density(t,
                                            name=names[0],
                                            line_color=TABLEAU_10[i],
                                            range_color=TABLEAU_10[i],
                                            levels=levels,
                                            alpha=alpha,
                                            selection=z)
            if minimaps is None:
                minimaps = minimap
            else:
                minimaps += minimap

        details = None
        for i, t in enumerate(tables):
            detail = self.__render_density(
                t,
                name=names[i],
                line_color=TABLEAU_10[i],
                range_color=TABLEAU_10[i],
                levels=levels,
                alpha=alpha,
                x_scale=alt.Scale(domain={
                    'selection': zoom.name,
                    "encoding": "x"
                }),
                y_scale=alt.Scale(domain={
                    'selection': zoom.name,
                    "encoding": "y"
                }))
            if details is None:
                details = detail
            else:
                details += detail

        minimaps = minimaps.properties(width=width, height=height_minimap)
        details = details.properties(width=width, height=height)

        return details & minimaps
Example #23
0
 def visualize_selection(self, df, x_axis, y_axis, legend):
     brush = alt.selection_interval()
     graph = alt.Chart(df).mark_point().encode(
         x=x_axis + ':Q',
         y=y_axis + ':Q',
         color=alt.condition(brush, legend + ':N', alt.value('lightgray'))
         #tooltip = tooltips
     ).properties(width=self.width, height=self.height).add_selection(brush)
     st.text("")
     st.text("")
     st.write(graph)
Example #24
0
def app():

    st.title("Let's demo Streamlit basics")

    st.sidebar.markdown("**Some tools we'll use:**")
    st.sidebar.markdown("""
        * Markdown
        * Pandas
        * Altair""")

    st.markdown("""## Markdown and Pandas
I am writing Markdown but I can also show a pandas DataFrame.
  """)

    df_cars = data.cars()
    st.write(df_cars.head())

    st.markdown("""## Altair
And I can easily make interactive altair plots.
  """)

    brush = alt.selection_interval(encodings=['x', 'y'])
    repeat_chart = alt.Chart(df_cars).mark_point().encode(
        alt.X(alt.repeat('column'), type='quantitative'),
        alt.Y('Miles_per_Gallon:Q'),
        color=alt.condition(brush, 'Origin:N', alt.value('lightgray')),
        opacity=alt.condition(
            brush, alt.value(0.7), alt.value(0.1))).properties(
                width=150, height=150).add_selection(brush).repeat(
                    column=['Weight_in_lbs', 'Acceleration', 'Horsepower'])

    st.write(repeat_chart)

    st.markdown("""## User Input
I can create a text input field to get input from the user.
  """)

    n = int(st.text_input("How many points do you want plotted:", '100'))

    x = np.random.random(n) * 10
    y = np.random.random(n) * 10
    s = np.random.random(n)

    df = pd.DataFrame({'x': x, 'y': y, 'size': s})

    chart = alt.Chart(df, width=400,
                      height=400).mark_point().encode(
                          x='x',
                          y='y',
                          size=alt.Size('size', legend=None),
                          tooltip=['size']).interactive()

    st.write(chart)
def nc_plot(numeric, categorical):
    brush = alt.selection_interval()
    click = alt.selection_multi(fields=[categorical], bind='legend')

    chart = alt.Chart(df).mark_line().encode(
            alt.X(numeric + ':Q', bin=alt.Bin(maxbins=30)),
            y = 'count()',
            color = alt.Color(categorical + ":N", scale=alt.Scale(scheme='blueorange')),
            opacity = alt.condition(click, alt.value(0.9), alt.value(0.2))
    ).add_selection(brush).add_selection(click).properties(width = 400, height = 400)
    
    return chart.to_html()
def plot_sexo():
    df_sexo = suicide_df[["SEXO", "YEAR"]]
    scales = alt.selection_interval(bind='scales')
    graph = alt.Chart(df_sexo, title="Quantidade de suicídios por sexo (2008 - 2018)").mark_line(point=True).encode(
        x=alt.X("YEAR:N", title="Ano"),
        y=alt.Y('count()', title='Quantidade'),
        color='SEXO',
        strokeDash='SEXO',
        tooltip=[alt.Tooltip('SEXO', title='Sexo'), alt.Tooltip('count()', title='Quantidade')],
    ).add_selection(scales).interactive()

    st.altair_chart((graph).configure_view(strokeOpacity=0).configure_title(fontSize=12).properties(width=700, height=410))
def plot_dtobito():
    suicide_df['DTOBITO'] = pd.to_datetime(suicide_df['DTOBITO'])
    df_dtobito = suicide_df[["DTOBITO"]]
    df_dtobito["DTOBITO"] = df_dtobito["DTOBITO"].dt.tz_localize('America/Argentina/Catamarca')

    scales = alt.selection_interval(bind='scales')
    graph = alt.Chart(df_dtobito, title="Quantidade de suicídios por mês (2008-2018)").mark_line(point=True).encode(
        x=alt.X('utcyearmonth(DTOBITO)', title='Mês/Ano'),
        y=alt.Y('count()', title='Quantidade'),
        tooltip=[alt.Tooltip('utcyearmonth(DTOBITO)', title='Mês/Ano'), alt.Tooltip('count()', title='Quantidade')]
    ).add_selection(scales).interactive()

    st.altair_chart((graph).configure_view(strokeOpacity=0).configure_title(fontSize=12).properties(width=700, height=410))
Example #28
0
    def trendmap(self):
        """
        create the trendmap based on the zipcodes and datatype

        Parameters
        ----------
        None, self

        Returns
        -------
        A trendmap plot object
        """
        # create the copy
        df_clean = self.df_clean.copy()
        # convert the string of zipcodes to a list
        zipcodels = self.convert(self.zipcode)
        # select the zipcodes from the graph
        selected_data = df_clean[df_clean['Zipcode'].isin(zipcodels)]
        interval = alt.selection_interval()

        # define the order of the graphs based on input from users
        if self.datatype == "DEATHRT" or "HOSPRT":
            sorteddate = df_clean.iloc[:13, 0].tolist()
        else:
            sorteddate = df_clean.iloc[:36, 0].tolist()
        # create the first graph
        circle = alt.Chart(selected_data).mark_circle().encode(
            x=alt.X('Date', sort=sorteddate),
            y='Zipcode',
            tooltip=['Zipcode', 'Date', 'Case'],
            color=alt.condition(interval, 'Zipcode', alt.value('lightgray')),
            size=alt.Size('Case:Q', scale=alt.Scale(range=[0, 1000]), legend=alt.Legend(title='Case\
                Per 100,000 People'))
            ).properties(
                width=1000,
                height=300,
                selection=interval
            ).interactive()

        # create the second chart
        bars = alt.Chart(selected_data).mark_bar().encode(
            y='Zipcode',
            color='Zipcode',
            x='sum(Case):Q',
            tooltip=['sum(Case):Q'],
        ).properties(
            width=1000
        ).transform_filter(
            interval
        ).interactive()
        return circle & bars
Example #29
0
def render(table: alt.Data, *, height: int, width: int, height_minimap: int):
    brush = alt.selection_interval(encodings=['x'])

    base = alt.Chart(table).mark_bar().encode(y='count():Q')
    detail = base.encode(
        alt.X('value:Q',
              bin=alt.Bin(maxbins=30, extent=brush),
              scale=alt.Scale(domain=brush))).properties(width=width,
                                                         height=height)
    minimap = base.encode(alt.X('value:Q', bin=alt.Bin(
        maxbins=30)), ).add_selection(brush).properties(width=width,
                                                        height=height_minimap)

    return detail & minimap
Example #30
0
def vega_example3():

    brush = alt.selection_interval()

    chart = (alt.Chart(cars).mark_point().encode(
        alt.X(alt.repeat("column"), type="quantitative"),
        alt.Y(alt.repeat("row"), type="quantitative"),
        color=alt.condition(brush, "Origin:N", alt.value("gray")),
    ).add_selection(brush).properties(width=250, height=250).repeat(
        row=["Horsepower", "Miles_per_Gallon"],
        column=["Acceleration", "Displacement"],
    ))

    return jsonify(chart.to_dict())
see the distribution of weather types in a particular date range.
"""
# category: case studies
import altair as alt
from vega_datasets import data

source = data.seattle_weather()

scale = alt.Scale(domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],
                  range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd'])
color = alt.Color('weather:N', scale=scale)

# We create two selections:
# - a brush that is active on the top panel
# - a multi-click that is active on the bottom panel
brush = alt.selection_interval(encodings=['x'])
click = alt.selection_multi(encodings=['color'])

# Top panel is scatter plot of temperature vs time
points = alt.Chart().mark_point().encode(
    alt.X('monthdate(date):T', axis=alt.Axis(title='Date')),
    alt.Y('temp_max:Q',
        axis=alt.Axis(title='Maximum Daily Temperature (C)'),
        scale=alt.Scale(domain=[-5, 40])
    ),
    color=alt.condition(brush, color, alt.value('lightgray')),
    size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))
).properties(
    width=600,
    height=300
).add_selection(