Ejemplo n.º 1
0
    def __create_world_map_chart(self, data, type):
        source = alt.topo_feature(dt.world_110m.url, "countries")
        base = (
            alt.Chart(source, title="")
            .mark_geoshape(fill="lightgray", stroke="white")
            .properties(width=800, height=450)
            .project("equirectangular")
        )

        points = (
            alt.Chart(data[data["variable"] == data["variable"].unique().max()])
            .mark_circle()
            .encode(
                longitude="Long:Q",
                latitude="Lat:Q",
                size=alt.Size(
                    "value:Q",
                    title="Number of Cases",
                    scale=alt.Scale(range=[250, 2000]),
                    # legend=None,
                ),
                color=alt.Color("value", scale=alt.Scale(scheme="orangered")),
                tooltip=[
                    alt.Tooltip("Country/Region:N"),
                    alt.Tooltip("value:Q", format=",.0f"),
                ],
            )
        )

        chart = base + points
        chart = chart.configure_legend(orient="bottom")

        return chart.to_html()
Ejemplo n.º 2
0
def visualize_freq(output_path):
    data = pd.read_csv(output_path)
    data['year'] = data.htrc_vol.str.split('_').str[2]
    data['months'] = data.htrc_vol.str.split('_').str[3]
    data['first_month'] = data.months.str.split('-').str[0]
    data['second_month'] = data.months.str.split('-').str[1]
    data['second_month'].fillna('dec', inplace=True)
    data.first_month = data.first_month.str.capitalize()
    data.second_month = data.second_month.str.capitalize()
    data['first_month_index'] = pd.to_datetime(data['first_month'],
                                               format='%b',
                                               errors='coerce').dt.month
    data['second_month_index'] = pd.to_datetime(data['second_month'],
                                                format='%b',
                                                errors='coerce').dt.month
    data = data.sort_values(by=['year', 'first_month_index'])
    data.htrc_vol.str.split('_')
    data['date'] = pd.to_datetime(data['year'].apply(str) + '-' +
                                  data['first_month_index'].apply(str),
                                  format='%Y-%m')
    data.date = data.date.dt.strftime('%Y-%m')
    data = data[data['frequency'] > 3]
    chart = alt.Chart(data).mark_circle(
        opacity=0.8, stroke='black', strokeWidth=1).encode(
            alt.X('date:O'), alt.Y('word', axis=alt.Axis(labelAngle=0)),
            alt.Size('frequency',
                     scale=alt.Scale(range=[0, 2000]),
                     legend=alt.Legend(title='counts')),
            alt.Color('word',
                      scale=alt.Scale(scheme='category20'),
                      legend=None)).properties(width=1400, height=10000)
    chart.serve()
Ejemplo n.º 3
0
def plot_heatmap(mat, str_tit):
    ncat_men, ncat_women = mat.shape
    mat_arr = np.empty((mat.size, 4))
    mat_min, mat_max = np.min(mat), np.max(mat)
    i = 0
    for ix in range(ncat_men):
        for iy in range(ncat_women):
            m = mat[ix, iy]
            s = m - mat_min + 1
            mat_arr[i, :] = np.array([ix, iy, m, s])
            i += 1

    mat_df = pd.DataFrame(mat_arr, columns=['Men', 'Women', 'Value', 'Size'])
    mat_df = mat_df.astype(dtype={
        'Men': int,
        'Women': int,
        'Value': float,
        'Size': float
    })
    base = alt.Chart(mat_df).encode(x='Men:O',
                                    y=alt.Y('Women:O', sort="descending"))
    mat_map = base.mark_circle(opacity=0.4).encode(
        size=alt.Size('Size:Q',
                      legend=None,
                      scale=alt.Scale(range=[1000, 10000])),
        color=alt.Color('Value:Q'),
        # tooltip=alt.Tooltip('Value', format=".2f")
    )
    text = base.mark_text(baseline='middle',
                          fontSize=16).encode(text=alt.Text('Value:Q',
                                                            format=".2f"), )
    both = (mat_map + text).properties(title=str_tit, width=500, height=500)
    return both
Ejemplo n.º 4
0
def plot_scatter_alt(
    self,
    color='run:Q',
    color_scheme='purplebluegreen',
    shape='method:N',
    width=400,
    height=400,
):
    """Plot the trials total flops vs max size.
    """
    import altair as alt

    df = self.to_df()
    scatter = (alt.Chart(df).mark_point().encode(
        x=alt.X('size:Q', title='log2[SIZE]', scale=alt.Scale(zero=False)),
        y=alt.Y('flops:Q', title='log10[FLOPS]', scale=alt.Scale(zero=False)),
        size=alt.Size(
            'random_strength:Q',
            scale=alt.Scale(range=[50, 150], type='log'),
            legend=None,
        ),
        shape=alt.Shape(shape),
        color=alt.Color(color, scale=alt.Scale(scheme=color_scheme)),
        tooltip=list(df.columns)))
    return (scatter.properties(
        width=width,
        height=height,
    ).configure_axis(gridColor='rgb(248, 248, 248)')).interactive()
Ejemplo n.º 5
0
def plot_contractions_alt(
    tree,
    x='peak-size',
    y='flops',
    color='stage',
    size='scaling',
    width=400,
    height=400,
    point_opacity=0.8,
    color_scheme='lightmulti',
    x_scale='log',
    y_scale='log',
    color_scale='log',
    size_scale='linear',
):
    import altair as alt

    df = tree_to_df(tree)
    chart = alt.Chart(df)

    encoding = chart.encode(
        x=alt.X(x, scale=alt.Scale(type=x_scale, padding=10)),
        y=alt.Y(y, scale=alt.Scale(type=y_scale, padding=10)),
        color=alt.Color(color,
                        scale=alt.Scale(scheme=color_scheme,
                                        type=color_scale)),
        size=alt.Size(size, scale=alt.Scale(type=size_scale)),
        tooltip=list(df.columns),
    )
    plot = encoding.mark_point(opacity=point_opacity)

    return (plot.configure_axis(gridColor='rgb(248,248,248)').properties(
        width=width, height=height).interactive())
Ejemplo n.º 6
0
def projects_hours_days(df):
    """
    """
    df_copy = df.copy()
    single = alt.selection_single()

    chart = alt.Chart(
        df_copy, title='Projects').mark_point(filled=True).encode(
            alt.X('yearmonthdate(start)', title="Project starting date"),
            alt.Y('days', title='Days since the start'),
            color=alt.Color(
                'status:N',
                title='Project current status',
                sort='descending',
            ),
            size=alt.Size('hours',
                          title='Total hours invested in the project'),
            tooltip=[
                alt.Tooltip('category', title='Category'),
                alt.Tooltip('project', title='Project'),
                alt.Tooltip('start', title='Project starting date'),
                alt.Tooltip('status', title='Status'),
                alt.Tooltip('days', title='Days since the start'),
                alt.Tooltip('working_days',
                            title='Days with at least 1 pomodoro'),
                alt.Tooltip('hours', title='Total hours invested'),
                alt.Tooltip('pomodoros', title='Amount of pomodoros made')
            ]).add_selection(single).properties(width=800).interactive()

    return chart
Ejemplo n.º 7
0
def chart_troops_paths(troops, advancing=True):
    if advancing:
        domain = CFG['AdvDomain'].split(',')
    else:
        domain = CFG['RetDomain'].split(',')
    colors = CFG['AdvColors'].split(',') + CFG['RetColors'].split(',')

    return alt.Chart(troops).mark_trail(order=False).encode(
        x=alt.X('LONP:Q',
                scale=alt.Scale(
                    domain=[troops["LONP"].min(), troops["LONP"].max()]),
                axis=alt.Axis(title=None,
                              labels=False,
                              grid=True,
                              gridColor=CFG['TempGridColor'])),
        longitude='LONP:Q',
        latitude='LATP:Q',
        size=alt.Size('SURV',
                      scale=alt.Scale(
                          range=[int(i)
                                 for i in CFG['TroopScale'].split(',')]),
                      legend=None),
        color=alt.Color('DIV',
                        scale=alt.Scale(domain=domain, range=colors),
                        legend=None),
        tooltip=[
            alt.Tooltip('SURV', title='Troop Numbers'),
            alt.Tooltip('DIR', title='Direction'),
            alt.Tooltip('LONP', title='Longitude'),
            alt.Tooltip('LATP', title='Latitude')
        ])
Ejemplo n.º 8
0
def make_interactive_chart():
    '''
    '''
    pts = alt.selection(type="single", encodings=['x'])

    rect = alt.Chart(data.movies.url).mark_rect().encode(
        alt.X('IMDB_Rating:Q', bin=True),
        alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
        alt.Color('count()',
                  scale=alt.Scale(scheme='greenblue'),
                  legend=alt.Legend(title='Total Records')))

    circ = rect.mark_point().encode(
        alt.ColorValue('grey'),
        alt.Size('count()', legend=alt.Legend(
            title='Records in Selection'))).transform_filter(pts)

    bar = alt.Chart(data.movies.url).mark_bar().encode(
        x='Major_Genre:N',
        y='count()',
        color=alt.condition(pts, alt.ColorValue("steelblue"),
                            alt.ColorValue("grey"))).properties(selection=pts,
                                                                width=550,
                                                                height=200)

    return alt.vconcat(rect + circ, bar).resolve_legend(color="independent",
                                                        size="independent")
Ejemplo n.º 9
0
def plot_stocks(state, list_of_stocks):
    for stock in list_of_stocks:
        i = [i for i, _ in enumerate(state.products) if _['name'] == stock][0]
        name = state.products[i]['name']
        transactions = state.products[i]['transactions'].copy()
        historical_data = state.products[i]['historical_data']
        general_data = state.products[i]['general_data']

        st.write(f'## {name}')
        st.write('### General Data')
        show_general_data(general_data)
        st.write('### Transactions')
        st.write(transactions)
        st.write('### Historical Data')

        if historical_data is None:
            st.warning('Couldn\'t get historical data')
        else:
            transactions.totalPlusFeeInBaseCurrency = (
                transactions.totalPlusFeeInBaseCurrency.abs())
            line = alt.Chart(historical_data).mark_line().encode(x='date:T',
                                                                 y='price:Q')
            circle = alt.Chart(transactions).mark_circle().encode(
                x=alt.X('date:T', title='Date'),
                y=alt.Y('price:Q', title='Price'),
                size=alt.Size('totalPlusFeeInBaseCurrency', legend=None),
                tooltip=['quantity', 'totalPlusFeeInBaseCurrency'],
                color=alt.Color(
                    'buysell',
                    legend=alt.Legend(title='Buy/Sell'),
                    scale=alt.Scale(scheme='dark2'))).interactive()

            st.altair_chart(line + circle, use_container_width=True)
Ejemplo n.º 10
0
def plot_anomalies(df, title='Anomaly detection'):
    interval = alt.Chart(df).mark_area(interpolate="basis", color='#7FC97F').encode(
        x='ds:T',
        y='yhat_lower',
        y2='yhat_upper',
        tooltip=['ds:T', 'y', 'yhat_lower', 'yhat_upper']
    ).interactive().properties(
        title=title
    )

    y = alt.Chart(df[df.anomaly == 0]).mark_circle(size=15, opacity=0.9, color='Black').encode(
        x='ds:T',
        y='y',
        tooltip=['ds:T', 'y', 'yhat_lower', 'yhat_upper']
    ).interactive()

    anomalies = alt.Chart(df[df.anomaly != 0]).mark_circle(size=30, color='Red').encode(
        x='ds:T',
        y='y',
        tooltip=['ds:T', 'y', 'yhat_lower', 'yhat_upper'],
        size=alt.Size('importance', legend=None)
    ).interactive()

    anomaly_chart = alt.layer(interval, y, anomalies) \
        .properties(width=870, height=450) \
        .configure_title(fontSize=20)
    anomaly_chart.save('./test/anomaly_chart.html')
Ejemplo n.º 11
0
def plot_anomalies(forecasted, chart_title=''):
    interval = alt.Chart(forecasted).mark_area(
        interpolate="basis", color='#7FC97F').encode(
            x=alt.X('ds:T', title='date'),
            y='yhat_upper',
            y2='yhat_lower',
            tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper'
                     ]).interactive().properties(title=chart_title +
                                                 ' Anomaly Detection')

    fact = alt.Chart(forecasted[forecasted.anomaly == 0]).mark_circle(
        size=15, opacity=0.7, color='Black').encode(
            x='ds:T',
            y=alt.Y('fact', title='sales'),
            tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper']).interactive()

    anomalies = alt.Chart(forecasted[forecasted.anomaly != 0]).mark_circle(
        size=30,
        color='Red').encode(x='ds:T',
                            y=alt.Y('fact', title='sales'),
                            tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper'],
                            size=alt.Size('importance',
                                          legend=None)).interactive()

    return alt.layer(interval, fact, anomalies)\
              .properties(width=870, height=450)\
              .configure_title(fontSize=20)
Ejemplo n.º 12
0
    def plot(self,position:str, test_df = None):
        
        data = self.target_data().copy()

        data["test"] = False

        if test_df is not None:
            test_df["test"] = True
            data = pd.concat([data, test_df]).reset_index()

        data = data.query(f"id == '{position}'").copy()

        x = np.arange(data["depth"].min(), data["depth"].max(), 1000).reshape(-1,1)
        y = self.linear_models[position].predict(x)
        y_seuil = x * 0.005

        line = pd.DataFrame({"x":x.reshape(-1), "y":y.reshape(-1)})
        line2 = pd.DataFrame({"x":x.reshape(-1), "y":y_seuil.reshape(-1)})
        
        data["fraction"] = data["second_vaf"] / data["depth"] * 100
        data["s"] = data["sample"].str.replace(r"-\d","", regex=True)
        data["outlier"] = self.outlier_models[position].predict(data[["depth","second_vaf"]])
        
        c1 = alt.Chart(data).mark_circle(color="green").encode(
            x="depth",
            y="second_vaf",
            size=alt.Size("test", scale = alt.Scale(type = "ordinal", range=[50, 400])),
            color=alt.Color("test", scale = alt.Scale(range=["gray","green"])),
            tooltip = ["file","sample","depth","fraction", "outlier"])

        c2 = alt.Chart(line).mark_line(color="green",strokeDash=[1,3]).encode(x="x",y="y")
        c3 = alt.Chart(line2).mark_line(color="red", strokeDash=[1,3]).encode(x="x",y="y")


        return (c1 + c2 + c3).interactive()
Ejemplo n.º 13
0
def generate_align_vs_features(data_frame, output_folder, file_name):
    """
    Generates a chart of the relation between align and other features in dataset.
    Also saves resulting image as file in given output folder.
    Parameters:
    -----------
    data_frame : pandas.DataFrame
        input path to be verified
    output_folder : str
        output folder path to save the chart
    file_name : str
        file name for generated chart image
        
    Returns:
    -----------
    None
    """
    features = ['id', 'eye', 'hair', 'sex', 'gsm', 'publisher']
    align_vs_features = (alt.Chart(data_frame).mark_circle().encode(
        alt.Y(alt.repeat(), type='ordinal'),
        alt.X('count()', title = "Character Count"),
        size =alt.Size('count()', legend=alt.Legend(title="Characters")),
        color = alt.Color("align", legend=alt.Legend(title="Alignment"))
        ).properties(height=300, width=200).repeat(repeat=features, columns=3))

    save(align_vs_features, output_folder +"/figures/" + file_name + '.png', method='selenium', webdriver=driver)
    if verbose: print("Alignment vs features chart created, saved to " + 
                      output_folder + 
                      "/figures/" + 
                      file_name + 
                      '.png')
def visualise_tsne(tsne_df, save=True, fig_num=15):
    """Visualise tsne plot"""
    tsne_base = alt.Chart(tsne_df).encode(
        x=alt.X("x:Q", title="", axis=alt.Axis(ticks=False, labels=False)),
        y=alt.Y("y:Q", title="", axis=alt.Axis(ticks=False, labels=False)),
    )

    tsne_points = ((
        tsne_base.mark_point(
            filled=True, opacity=0.5, stroke="black",
            strokeOpacity=0.5).encode(
                color=alt.Color("org_type", title="Organisation type"),
                strokeWidth=alt.Stroke("top",
                                       scale=alt.Scale(range=[0, 1]),
                                       legend=None),
                # stroke = alt.value('blue'),
                size=alt.Size("activity:Q", title="Number of papers"),
                facet=alt.Facet("size",
                                columns=2,
                                title="Number of organisations in plot"),
                tooltip=["index"],
            )).interactive().resolve_scale(
                y="independent", x="independent").properties(width=250,
                                                             height=250))

    if save is True:
        save_altair(tsne_points, "fig_15_tsne", driv)

    return tsne_points
Ejemplo n.º 15
0
def plot_suicide_gdp(data, sex, country, age, year):
    alt.themes.enable('dark')
    df = data[data['country'] == country]
    #for gender in sex:
    #if type(sex) == 'str':
    #df = df[df['sex'] == sex]
    if not type(sex) == list:
        df = df[df['sex'] == sex]
    else:
        pass
    df = df[df['age'] == age]
    # df = df[df['generation'] == generation]
    df['year'] = pd.to_datetime(df['year'], format='%Y')

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

    df = df[df['year'] > year[0]]
    df = df[df['year'] < year[1]]

    chart = alt.Chart(df, title='Suicide Trends per Country').mark_circle(
        color='yellow').encode(alt.X('year', title='Year'),
                               alt.Y('suicides_no',
                                     title='Total Number of Suicides'),
                               alt.Color('sex',
                                         scale=alt.Scale(scheme='blueorange')),
                               alt.Size('gdp_per_capita ($):Q', legend=None),
                               tooltip=[
                                   alt.Tooltip('country:N'),
                                   alt.Tooltip('gdp_per_capita ($):Q')
                               ]).interactive().configure_axis(grid=False)
    return chart.to_html()
Ejemplo n.º 16
0
    def get_map(self, day_str, title=""):
        chart_data = self._prepare_dataset(day_str)

        source = alt.topo_feature(data.world_110m.url, 'countries')
        background = alt.Chart(source).mark_geoshape(
            fill="lightgray",
            stroke="white").properties(width=1000,
                                       height=500).project("equirectangular")

        hover = alt.selection(type='single',
                              on='mouseover',
                              nearest=False,
                              fields=[self.col_lat, self.col_long])
        text = background.mark_text(dy=-5, align='right').encode(
            alt.Text(f'{self.col_name_countries}:N', type='nominal'),
            opacity=alt.condition(~hover, alt.value(0), alt.value(1)))

        points = alt.Chart(chart_data).mark_circle().encode(
            latitude=f"{self.col_lat}:Q",
            longitude=f"{self.col_long}:Q",
            size=alt.Size(f"{day_str}:Q",
                          scale=alt.Scale(range=[0, 7000]),
                          legend=None),
            order=alt.Order(f"{day_str}:Q", sort="descending"),
            tooltip=[f'{self.col_name_countries}:N', f'{day_str}:Q'
                     ]).add_selection(hover).properties(title=title)

        chart = alt.layer(background, points, text)

        return chart
Ejemplo n.º 17
0
def country_plot(source):
    alt.themes.enable('dark')
    combined11 = source
    selection = alt.selection_single()
    country_chart = alt.Chart(
        combined11,
        title='Spread of Countries - GDP Vs Total Suicide Numbers').mark_point(
            filled=True).encode(
                alt.X('gdp_per_capita:Q'),
                alt.Y('suicides_no'),
                alt.Size('population:Q',
                         scale=alt.Scale(range=[0, 2000]),
                         legend=None),
                alt.Order('population:Q', sort='ascending'),
                tooltip=[
                    alt.Tooltip('country:N'),
                    alt.Tooltip('suicides_no:Q'),
                    alt.Tooltip('gdp_per_capita:Q'),
                    alt.Tooltip('population')
                ],
                color=alt.condition(
                    selection,
                    'country',
                    alt.value('darkgrey'),
                    scale=alt.Scale(scheme='blueorange'),
                    legend=None)).add_selection(selection).configure_axis(
                        grid=False)
    return country_chart.to_html()
Ejemplo n.º 18
0
    def plot_anomalies(self, forecasted):
        """Show plot with anomalies
           Inspired from https://towardsdatascience.com/anomaly-detection-time-series-4c661f6f165f"""
        interval = alt.Chart(forecasted).mark_area(
            interpolate="basis", color='#adadad').encode(
                x=alt.X('ds:T', title='Time'),
                y='yhat_upper',
                y2='yhat_lower',
                tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper'
                         ]).interactive().properties(title='Anomaly Detection')

        fact = alt.Chart(forecasted[forecasted.anomaly == 0]).mark_circle(
            size=15, opacity=0.7, color='Black').encode(
                x='ds:T',
                y=alt.Y('fact', title='CPU Utilization [%]'),
                tooltip=['ds', 'fact', 'yhat_lower',
                         'yhat_upper']).interactive()

        anomalies = alt.Chart(forecasted[forecasted.anomaly != 0]).mark_circle(
            size=30, color='Red').encode(
                x='ds:T',
                y=alt.Y('fact', title='CPU Utilization [%]'),
                tooltip=['ds', 'fact', 'yhat_lower', 'yhat_upper'],
                size=alt.Size('importance', legend=None)).interactive()

        return alt.layer(interval, fact, anomalies) \
            .properties(width=870, height=450) \
            .configure_title(fontSize=20)
Ejemplo n.º 19
0
def test_shorthand_typecodes(mydata):
    opts = {
        "xvar": "name",
        "yvar": "amount:O",
        "fillvar": "max(amount)",
        "sizevar": "mean(amount):O",
    }
    cg = ChannelGroup(opts, mydata)

    assert cg == {
        "x":
        alt.X(**{
            "field": "name",
            "type": "nominal"
        }),
        "y":
        alt.Y(**{
            "field": "amount",
            "type": "ordinal"
        }),
        "fill":
        alt.Fill(**{
            "field": "amount",
            "type": "quantitative",
            "aggregate": "max",
        }),
        "size":
        alt.Size(**{
            "field": "amount",
            "type": "ordinal",
            "aggregate": "mean",
        }),
    }
Ejemplo n.º 20
0
def punchcode():
    dat = df.copy()
    dat['mnth_yr'] = dat['workshop_start'].dt.to_period('M').astype(str)
    dat['workshop_category'] = dat['workshop_category'].apply(
        lambda x: 'Corporate' if (x == 'Corporate') else 'Public')
    dat['contrib'] = dat['workshop_hours'] * dat['class_size']

    chart = alt.Chart(
        dat[dat.name != 'Capstone']).mark_circle(color='#bbc6cbe6').encode(
            x=alt.X('mnth_yr:T', axis=alt.Axis(title='')),
            y='name:O',
            size=alt.Size('sum(contrib):Q', legend=None),
            column=alt.Column('workshop_category:O',
                              title=None,
                              sort="descending",
                              header=alt.Header(titleColor='#bbc6cbe6',
                                                labelColor='#bbc6cbe6',
                                                labelAngle=30,
                                                titleFontSize=40,
                                                titleAngle=30))).properties(
                                                    width=300,
                                                    height=320).configure_axis(
                                                        labelColor='#bbc6cbe6',
                                                        titleColor='#bbc6cbe6',
                                                        grid=False)
    return chart.to_json()
Ejemplo n.º 21
0
def world_map(highlight, highlight2):
    slider = alt.binding_range(min=1991, max=2011, step=1)
    select_year = alt.selection_single(name="Year",
                                       fields=['Year'],
                                       bind=slider,
                                       init={'Year': 2011})

    map = alt.Chart(df).mark_geoshape(stroke='#aaa', strokeWidth=0.25).encode(
        color=alt.condition(highlight2 | highlight,
                            'CO2 emissions (kt):Q',
                            alt.value('lightgrey'),
                            scale=alt.Scale(scheme='redyellowblue',
                                            reverse=True)),
        tooltip=[
            "Country Name", "CO2 emissions (kt)", "CO2 emissions per capita"
        ]
    ).transform_lookup(
        lookup='Country Name',
        from_=alt.LookupData(
            "https://raw.githubusercontent.com/KoGor/Map-Icons-Generator/master/data/world-110m-country-names.tsv",
            'name', ['id', "name"])).transform_lookup(
                lookup='id',
                from_=alt.LookupData(
                    countries,
                    'id',
                    fields=["id", "type", "properties", "geometry"])
            ).project(type="equirectangular").properties(
                width=1100,
                height=650,
                title='worldwide CO2 total emissions and emissions per capita'
            ).add_selection(highlight, highlight2)

    percapita = alt.Chart(df).mark_circle(opacity=0.4, ).encode(
        size=alt.Size('CO2 emissions per capita:Q',
                      scale=alt.Scale(range=[10, 3000])),
        color=alt.condition(highlight2 | highlight, alt.value('red'),
                            alt.value('lightgrey')),
        longitude='Longitude (average):Q',
        latitude='Latitude (average):Q',
        tooltip=[
            "Country Name", "CO2 emissions (kt)", "CO2 emissions per capita"
        ]
    ).transform_lookup(
        lookup='Country Name',
        from_=alt.LookupData(
            "https://raw.githubusercontent.com/KoGor/Map-Icons-Generator/master/data/world-110m-country-names.tsv",
            'name', ['id', "name"])).transform_lookup(
                lookup='id',
                from_=alt.LookupData(
                    countries,
                    'id',
                    fields=["id", "type", "properties", "geometry"
                            ])).project(type="equirectangular").properties(
                                width=900,
                                height=400,
                            )
    return alt.layer(map, percapita) \
        .add_selection(select_year) \
        .transform_filter(select_year)
Ejemplo n.º 22
0
def scatter_plot(df, picked_interval, single_select):
    point = alt.Chart(df).mark_circle().encode(
        x=alt.X('Year:O', title="Year"),
        y=alt.Y('CO2 emissions (kt)',
                title='Total CO2 emissions (kt)',
                scale=alt.Scale(zero=False, padding=1)),
        # color = alt.Color('Country Name:N',scale=alt.Scale(domain=dataset,type='ordinal'))
        # color = alt.Color('CO2 emissions (kt):Q')
        color=alt.condition(picked_interval | single_select,
                            "CO2 emissions (kt):Q",
                            alt.value("lightgray"),
                            scale=alt.Scale(scheme='redyellowblue',
                                            reverse=True),
                            title="Total CO2 emissions (kt)"),
        size=alt.Size('CO2 emissions per capita:Q',
                      scale=alt.Scale(range=[300, 1000])),
        tooltip=[
            "Country Name", "CO2 emissions (kt)", "CO2 emissions per capita",
            "Year"
        ]).add_selection(single_select)

    line = alt.Chart(df).mark_line(
        # strokeWidth=0.7
    ).encode(
        x=alt.X('Year:N', title="Year"),
        y=alt.Y('CO2 emissions (kt):Q', title='Total CO2 emissions (kt)'),
        color=alt.condition(picked_interval | single_select,
                            "Country Name",
                            alt.value("lightgray"),
                            legend=None),
        # color = alt.Color('CO2 emissions (kt):Q')
        # color=alt.condition(picked_interval, "CO2 emissions (kt):Q", alt.value("lightgray"),
        #                     scale=alt.Scale(scheme='redyellowblue', reverse=True), title="Total CO2 emissions (kt)")
        size=alt.condition(~(picked_interval | single_select), alt.value(1),
                           alt.value(3)),
        tooltip=[
            "Country Name", "CO2 emissions (kt)", "CO2 emissions per capita",
            "Year"
        ]).properties(
            width=650,
            height=500,
            title='CO2 total emission and emission per capita overtime')

    labels = alt.Chart(df).mark_text(align='center', dx=-20, dy=-25).encode(
        alt.X('Year:O', aggregate='max'),
        alt.Y('CO2 emissions (kt)', aggregate={'argmax': 'Year'}),
        alt.Text('Country Name'),
        alt.Color('CO2 emissions (kt):Q',
                  aggregate={'argmax': 'Year'},
                  scale=alt.Scale(scheme='redyellowblue', reverse=True),
                  legend=None),
        size=alt.condition(~(single_select), alt.value(17), alt.value(20)),
    ).properties(title='CO2 total emission and emission per capita', width=600)

    points = line + point + labels
    return points
Ejemplo n.º 23
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)
def status_by_dep_arr():
    selection = alt.selection_multi(fields=['STATUS'])
    brush = alt.selection(type='interval')

    color_select_brush = alt.condition(selection | brush,
                                       alt.Color('STATUS:N', legend=None),
                                       alt.value('lightgrey'))

    color_select = alt.condition(selection, alt.Color('STATUS:N', legend=None),
                                 alt.value('lightgrey'))

    # color_brush = alt.condition(brush,
    #                       alt.Color('STATUS:N', legend = None),
    #                       alt.value('lightgray'))

    dep_arr = alt.Chart(df).mark_circle().encode(
    # dep_arr = alt.Chart(df).mark_circle().transform_filter(alt.datum['STATUS'] != 'on time').encode(
        x = alt.X('CRS_DEP_TIME:Q', title = 'Departure Time', axis = alt.Axis(labelOverlap = True)),
        y = alt.Y('CRS_ARR_TIME:Q', title = 'Arrival Time', axis = alt.Axis(labelOverlap = True), sort = '-y'),
        # color = alt.condition(brush, 'STATUS', alt.value('lightgray')),
        color = color_select_brush,
        size = alt.Size('average(ARR_DELAY):Q', title = 'Arrival Delay', scale = alt.Scale(domain = [1, 800]), legend = None),
        tooltip = [alt.Tooltip('CRS_DEP_TIME', title = 'Departure Time'), alt.Tooltip('CRS_ARR_TIME', title = 'Arrival Time'), \
        alt.Tooltip('average(ARR_DELAY):Q', title = 'Arrival Delay')],
    ).add_selection(brush).properties(width = 600, height = 400)

    bars = alt.Chart(df).mark_bar().encode(
        # bars = alt.Chart(df).mark_bar().transform_filter(alt.datum['STATUS'] != 'on time').encode(
        y=alt.Y('STATUS', title=''),
        # color = 'STATUS',
        color=color_select,
        x=alt.X('count(STATUS):Q', title='Delayed Flights Count'),
    ).transform_filter(brush)

    bar_text = bars.mark_text(
        align='left',
        baseline='middle',
        dx=3  # Nudges text to right so it doesn't appear on top of the bar
    ).encode(text='count(STATUS):Q')

    # count = alt.Chart(df).mark_bar().encode(
    count = alt.Chart(df).mark_bar().transform_filter(
        alt.datum['STATUS'] != 'on time').encode(x=alt.X(
            'count(STATUS):Q',
            title='Total Delayed Flights Count'), ).transform_filter(brush)

    count_text = count.mark_text(align='left', baseline='middle',
                                 dx=3).encode(text='count(STATUS):Q')

    legend = alt.Chart(df).mark_point().encode(
        # legend = alt.Chart(df).mark_point().transform_filter(alt.datum['STATUS'] != 'on time').encode(
        y=alt.Y('STATUS:N', axis=alt.Axis(orient='right')),
        color=color_select).add_selection(selection)

    (dep_arr & (count + count_text) & (bars + bar_text)) | legend
Ejemplo n.º 25
0
def bubble_chart(df, year):
    df_year = df[df['year'] == year].copy()
    alt_chart = alt.Chart(df_year).mark_circle().encode(
        alt.X('gdpPercap:Q', scale=alt.Scale(type='log',
                                             domain=(100, 100000))),
        alt.Y('lifeExp:Q', scale=alt.Scale(domain=(25, 90))),
        size=alt.Size('pop:Q'),
        color='continent:N',
        tooltip=['country', 'lifeExp', 'pop',
                 'gdpPercap']).properties(title=str(year)).interactive()
    return alt_chart
Ejemplo n.º 26
0
def chart_obj(request):
    h_list = Homicide.objects.annotate(c=Count('count')).values(
        'age', 'c', 'gender')
    data = alt.Data(values=list(h_list))
    chart = alt.Chart(data, height=300, width=300,
                      title='Age / Gender').mark_circle().encode(
                          alt.X('age:Q', bin=True),
                          alt.Y('gender:N'),
                          alt.Size('sum(c):Q', title='Counts'),
                          color='sum(c):Q')
    return JsonResponse(chart.to_dict(), safe=False)
def plot_chart4(level):
    chart = alt.Chart(hr4[hr4.experience_level == level]).mark_point().encode(
        alt.X("education_level", title="Educaiton Level"),
        alt.Y("major_discipline", title="Major Discipline"),
        alt.Size("mean(target)", legend=alt.Legend(title="Change Job Ratio")),
        alt.Color("mean(target)"),
        fill="mean(target)",
    ).properties(width=400, height=400)
    chart = alt.layer(chart).configure_axis(
        labelFontSize=16, titleFontSize=20).configure_legend(labelFontSize=16,
                                                             titleFontSize=16)
    return chart.to_html()
Ejemplo n.º 28
0
def punchcard_repo(ds):
    ds.engine = "altair"
    ds.rcolor()
    ds.width(630)
    ds.height(15)
    ds.chart("Date:T", "Repository:N")
    ds.aenc("size", alt.Size('Commits', legend=None))
    c = ds.square_().configure_axis(
        titleColor="transparent", grid=False, tickSize=0,
        domain=False, labelFontSize=0).configure_view(strokeWidth=0)
    ds.raencs()
    return c
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
def show_chart(data,items):
    stat = {'rank':'Rank','All Employees':'All Employees (1K)','Avg Hourly Wages':'Avg Hourly Wages','eWage':'Pricing Power','score':'Employment Strengths','eCPI':'Price Index'}

    stat_text = list(stat.values())
    stat_keys = list(stat.keys())
    items = [key for key, value in items.items() if value!=0]

    data = data[['year','cbsa_area','Metro area']+stat_keys+items]

    st.subheader('Employment strengths')

    # Employment strengths scatter
    scatter = (alt.Chart(data)
        .mark_circle()
        .encode(
            x=alt.X(stat_keys[1],title=stat_text[1]),
            y=alt.Y(stat_keys[2],title=stat_text[2]),
            size=alt.Size(stat_keys[4],legend=None),
            tooltip=[alt.Tooltip('Metro area'),alt.Tooltip(stat_keys[0],title=stat_text[0]),alt.Tooltip(stat_keys[1],title=stat_text[1]),alt.Tooltip(stat_keys[2],title=stat_text[2],format='$')]
        )
    )

    st.altair_chart(scatter,use_container_width=True)

    st.subheader('Price Index')

    # Price Index stacked_bar
    stacked_cpi = (alt.Chart(data)
        .transform_fold(items,['Item','Price Index'])
        .mark_bar()
        .encode(
            x=alt.X('Price Index:Q'),
            y=alt.Y('Metro area:N',sort=alt.EncodingSortField(field=stat_keys[5],order='descending')),
            color=alt.Color('Item:N'),
            tooltip=[alt.Tooltip('Metro area'),alt.Tooltip('Item:N'),alt.Tooltip('Price Index:Q'),alt.Tooltip(stat_keys[5],title=stat_text[5])]
        )
    )

    st.altair_chart(stacked_cpi,use_container_width=True)

    st.subheader('Pricing Power')

    # Employment strengths scatter
    stacked_pp = (alt.Chart(data)
        .mark_bar()
        .encode(
            x=alt.X(stat_keys[3],title=stat_text[3]),
            y=alt.Y('Metro area:N',sort=alt.EncodingSortField(field=stat_keys[3],order='descending')),
            tooltip=[alt.Tooltip('Metro area'),alt.Tooltip(stat_keys[2],title=stat_text[2]),alt.Tooltip(stat_keys[5],title=stat_text[5]),alt.Tooltip(stat_keys[3],title=stat_text[3])]
        )
    )

    st.altair_chart(stacked_pp,use_container_width=True)