Exemple #1
0
def plot_altair(selected_province, price_value, points_value):
    if selected_province == 'Select your province':
        df_filtered = df
    else:
        df_filtered = df[df['state'] == selected_province]

    state_map = alt.topo_feature(data.us_10m.url, 'states')
    df_filtered = df_filtered[(df_filtered['price'] >= min(price_value))
                              & (df_filtered['price'] <= max(price_value))]
    df_filtered = df_filtered[(df_filtered['points'] >= min(points_value))
                              & (df_filtered['points'] <= max(points_value))]
    states_grouped = df_filtered.groupby(['state', 'state_id'], as_index=False)
    wine_states = states_grouped.agg({
        'points': ['mean'],
        'price': ['mean'],
        'value': ['mean'],
        'description': ['count']
    })

    wine_states.columns = wine_states.columns.droplevel(level=1)
    wine_states = wine_states.rename(
        columns={
            "state": "State",
            "state_id": "State ID",
            "description": "Num Reviews",
            "points": 'Ave Rating',
            "price": 'Ave Price',
            "value": 'Ave Value'
        })
    map_click = alt.selection_multi(fields=['state'])
    states = alt.topo_feature(data.us_10m.url, "states")

    colormap = alt.Scale(domain=[0, 100, 1000, 2000, 4000, 8000, 16000, 32000],
                         range=[
                             '#C7DBEA', '#CCCCFF', '#B8AED2', '#3A41C61',
                             '#9980D4', '#722CB7', '#663399', '#512888'
                         ])

    foreground = alt.Chart(states).mark_geoshape().encode(
        color=alt.Color('Num Reviews:Q', scale=colormap),
        tooltip=[
            alt.Tooltip('State:O'),
            alt.Tooltip('Ave Rating:Q', format='.2f'),
            alt.Tooltip('Ave Price:Q', format='$.2f'),
            alt.Tooltip('Ave Value:Q', format='.2f'),
            alt.Tooltip('Num Reviews:Q')
        ]).mark_geoshape(stroke='black', strokeWidth=0.5).transform_lookup(
            lookup='id',
            from_=alt.LookupData(wine_states, 'State ID', [
                'State', 'State ID', 'Ave Rating', 'Ave Price', 'Ave Value',
                'Num Reviews'
            ])).project(type='albersUsa')

    background = alt.Chart(states).mark_geoshape(
        fill='gray', stroke='dimgray').project('albersUsa')
    chart = (background + foreground).configure_view(
        height=400, width=570, strokeWidth=4,
        fill=None, stroke=None).encode(opacity=alt.condition(
            map_click, alt.value(1), alt.value(0.2))).add_selection(map_click)
    return chart.to_html()
Exemple #2
0
def generate_regions_choropleth(
    data: pd.DataFrame,
    feature: str,
    title: str,
    width: int = 700,
    height: int = 1000,
    log_scale: bool = True,
    is_region: bool = True,
) -> alt.Chart:
    if is_region:
        shape = alt.topo_feature(
            "https://raw.githubusercontent.com/openpolis/geojson-italy/master/topojson/limits_IT_regions.topo.json",
            "regions",
        )
    else:
        shape = alt.topo_feature(
            "https://raw.githubusercontent.com/openpolis/geojson-italy/master/topojson/limits_IT_provinces.topo.json",
            "provinces",
        )

    area_name = "reg_name" if is_region else "prov_name"
    lookup_in_shape = "reg_istat_code_num" if is_region else "prov_istat_code_num"
    lookup_in_df = "codice_regione" if is_region else "codice_provincia"

    chart_data = data[data[feature] > 0][[feature, lookup_in_df]]

    base_chart = (alt.Chart(shape).mark_geoshape(
        stroke="black", strokeWidth=0.5, color="white").encode(
            tooltip=[alt.Tooltip(f"properties.{area_name}:N", title=title)]))
    scale = (alt.Scale(type="log", scheme="teals")
             if log_scale else alt.Scale(type="linear", scheme="teals"))
    color_chart = (alt.Chart(shape).mark_geoshape(
        stroke="black", strokeWidth=0.5).encode(
            color=alt.Color(
                f"{feature}:Q",
                title=formatter(feature),
                scale=scale,
                legend=alt.Legend(labelLimit=50),
            ),
            tooltip=[
                alt.Tooltip(f"properties.{area_name}:N", title=title),
                alt.Tooltip(f"{feature}:Q",
                            title=formatter(feature),
                            format=".4~f"),
            ],
        ).transform_lookup(
            f"properties.{lookup_in_shape}",
            from_=alt.LookupData(
                data=chart_data,
                key=lookup_in_df,
                fields=[feature],
            ),
        ))

    final_chart = ((base_chart + color_chart).configure_view(
        strokeWidth=0).properties(width=width, height=height))

    return final_chart
Exemple #3
0
def map_usa_cases(curr_date):
    # Obtain altair topographic information
    us_states = alt.topo_feature(topo_usa, 'states')
    us_counties = alt.topo_feature(topo_usa, 'counties')

    # state borders
    base_states = alt.Chart(us_states).mark_geoshape().encode(
        stroke=alt.value('lightgray'), fill=alt.value('white')).properties(
            width=1200,
            height=960,
        ).project(type='albersUsa', )

    # confirmed cases by county
    base_counties = alt.Chart(us_counties).mark_geoshape().encode(
        color=alt.Color(
            'confirmed_per100K:Q',
            scale=alt.Scale(domain=(1, 7500), type='log'),
            title='Confirmed per 100K'), ).transform_lookup(
                lookup='id',
                from_=alt.LookupData(
                    pdf_usa[(pdf_usa['confirmed_per100K'] > 0)
                            & (pdf_usa['process_date'] == curr_date)], 'fips',
                    ['confirmed_per100K']))

    # deaths by long, latitude
    points = alt.Chart(
        pdf_usa[(pdf_usa['deaths_per100K'] > 0)
                & (pdf_usa['process_date'] == curr_date)]
    ).mark_point(opacity=0.75, filled=True).encode(
        longitude='long_:Q',
        latitude='lat:Q',
        size=alt.Size('deaths_per100K:Q',
                      scale=alt.Scale(domain=(1, 1000), type='log'),
                      title='deaths_per100K'),
        #size=alt.Size('deaths_per100K:Q', title='deaths_per100K'),
        color=alt.value('#BD595D'),
        stroke=alt.value('brown'),
        tooltip=[
            alt.Tooltip('state', title='state'),
            alt.Tooltip('county', title='county'),
            alt.Tooltip('confirmed_per100K', title='confirmed'),
            alt.Tooltip('deaths_per100K', title='deaths'),
        ],
    ).properties(
        # update figure title
        title=
        f'COVID-19 Confirmed Cases and Deaths by County (by 100K) {curr_date}')

    # display graph
    return (base_states + base_counties + points)
def create_single_map(df):
    df2 = df.groupby('context').mean()
    df2['context'] = df2.index
    print(df2)
        
    countries = alt.topo_feature('https://raw.githubusercontent.com/anitagraser/sandbox/master/land40.topojson','land40')
    basemap = alt.Chart(countries).mark_geoshape(
        stroke='white',
        strokeWidth=2
    ).encode(
        color=alt.value('#eee'),
    ).project(
        type='mercator'
    ).properties(
        width=500,
        height=300
    )
    
    grid = alt.topo_feature('https://raw.githubusercontent.com/anitagraser/sandbox/master/grid40.topojson', 'grid40')
    variable_list = ['distance_error', 'along_track_error', 'cross_track_error']
    map_chart = alt.Chart(grid).mark_geoshape(
        stroke='white',
        strokeWidth=2    
    ).encode(
        alt.Color('distance_error' , type='quantitative')
    ).transform_lookup(
        lookup='properties.id',
        from_=alt.LookupData(df2, 'context', variable_list)
    )
    
    cities = pd.DataFrame([
        {"lon":11.9667, "lat":57.7, "city":"Gothenburg"},
        {"lon":10.534, "lat":57.441, "city":"Frederikshavn"}
    ]) 
    city_points = alt.Chart(cities).mark_point().encode(
            color=alt.value('black'),
            longitude='lon:Q',
            latitude='lat:Q'
        )
    city_labels = alt.Chart(cities).mark_text(dy=-5).encode(
            longitude='lon:Q',
            latitude='lat:Q',
            text='city:N'
        )

    map_chart = map_chart + basemap + city_points + city_labels
    
    with open(INPUT.replace('.csv','_single_map.vega'),'w') as map_output:
        map_output.write(map_chart.to_json(indent=2))
Exemple #5
0
def get_altair_map(tip_size=400):
    countries = alt.topo_feature(data.world_110m.url, 'countries')

    points = alt.Chart(mydf).mark_circle().encode(
        longitude='long',
        latitude='lat',
        size=alt.value(tip_size),
        tooltip='tooltip'
    ).project(
        type= 'mercator',
        scale= 350,
        center= [20,50],
        clipExtent= [[0, 0], [400, 300]],
    ).properties(
        width=500,
        height=400
    )

    background = alt.Chart(countries).mark_geoshape(
        fill='#CCCCCC',
        stroke='white'
    ).project(
        type= 'mercator',
        scale= 350,                          # Magnify
        center= [20,50],                     # [lon, lat]
        clipExtent= [[0, 0], [400, 300]],    # [[left, top], [right, bottom]]
    ).properties(
        width=400, height=300
    )

    return background + points
Exemple #6
0
    def plot_map(area_type, width=500, height=300):
        map_json = alt.topo_feature(
            'https://cdn.jsdelivr.net/npm/world-atlas@2/countries-50m.json',
            'countries')

        country_map = alt.Chart(
            data, title=f'# Respondents by {area_type}').transform_filter(
                col1_brush).transform_filter(col2_brush).transform_lookup(
                    lookup='CountryCode',
                    from_=alt.LookupData(map_json, 'properties.id'),
                    as_='geom',
                    default='Other').transform_aggregate(
                        counter='count()',
                        groupby=['geom', area_type]).transform_calculate(
                            geometry='datum.geom.geometry',
                            type='datum.geom.type').transform_filter(
                                alt.datum.date == 2018).mark_geoshape().encode(
                                    color='counter:Q',
                                    tooltip=[
                                        alt.Tooltip(f'{area_type}:N',
                                                    title='Area'),
                                        alt.Tooltip('counter:Q',
                                                    title='# Respondents')
                                    ]).properties(width=width, height=width)

        borders = alt.Chart(map_json).mark_geoshape(fill='#EEEEEE',
                                                    stroke='gray',
                                                    strokeWidth=1).properties(
                                                        width=width,
                                                        height=height)

        return (borders + country_map)
Exemple #7
0
def norway_plot():
    # Get covids numbers
    df = covids_numbers()

    # Gets the topojson of norway counties from random gitub
    counties = alt.topo_feature("https://raw.githubusercontent.com/deldersveld/topojson/master/countries/norway/norway-new-counties.json", "Fylker")

    # Define nearest selection (used for the highlighting)
    nearest = alt.selection(type="single", on="mouseover", fields=["properties.navn"], empty="none")

    # Plot the map
    fig = alt.Chart(counties).mark_geoshape().encode(
	# Enable hover efect
	tooltip=[
	    alt.Tooltip("properties.navn:N", title="County"),
	    alt.Tooltip("Insidens:Q", title="Cases per 100k capita"),
	],
	color=alt.Color("Insidens:Q", scale=alt.Scale(scheme="reds"), 
			legend=alt.Legend(title="Cases per 100k capita")),
	stroke=alt.condition(nearest, alt.value("gray"), alt.value(None)),
	opacity=alt.condition(nearest, alt.value(1), alt.value(0.8)),

    # Lookup number of cases from Pandas table and map to counties
    ).transform_lookup(
	lookup="properties.navn",
	from_=alt.LookupData(df, "Category", ["Insidens"])
    ).properties(
	width=500,
	height=600,
	title="Number of cases per 100k in every county",
    ).add_selection(
	nearest
    )

    return fig
def international_vax_data():
    "Reads in internation data and data for international geo graph"
    df = pd.read_csv(
        "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv",
        names=[
            'Country', 'iso_code', 'date', 'total_vaccinations',
            'people_vaccinated', 'people_fully_vaccinated',
            'daily_vaccinations_raw', 'daily_vaccinations',
            'total_vaccinations_per_hundred', 'people_vaccinated_per_hundred',
            'people_fully_vaccinated_per_hundred',
            'daily_vaccinations_per_million'
        ],
        encoding='latin-1')
    country_info = pd.read_csv(
        'https://gist.githubusercontent.com/komasaru/9303029/raw/9ea6e5900715afec6ce4ff79a0c4102b09180ddd/iso_3166_1.csv'
    )
    country_info = country_info.rename(columns={'Alpha-3 code': 'iso_code'})
    df = df[2:]
    df['date'] = pd.to_datetime(df['date'], errors='coerce', format='%Y-%m-%d')
    country_info = pd.read_csv(
        'https://gist.githubusercontent.com/komasaru/9303029/raw/9ea6e5900715afec6ce4ff79a0c4102b09180ddd/iso_3166_1.csv'
    )
    country_info = country_info.rename(columns={'Alpha-3 code': 'iso_code'})
    all_data = pd.merge(country_info, df, how='inner', on="iso_code")
    all_data = all_data.sort_values('date', ascending=True)
    all_data = all_data.tail(5000)
    all_data['id'] = all_data['Numeric']
    countries = alt.topo_feature(data.world_110m.url, 'countries')
    return all_data, countries
Exemple #9
0
def plot_worldmap(year_range):
    df1 = dataset_df.copy()
    world = data.world_110m()
    world_map = alt.topo_feature(data.world_110m.url, "countries")
    chosen_ending_year = year_range[1]

    country_ids = pd.read_csv(
        "data/processed/country-ids.csv"
    )
    df1 = pd.merge(df1, country_ids, left_on="country", right_on="name").iloc[:, :-1]
    df1 = df1[df1["year"] == chosen_ending_year]

    map_click = alt.selection_multi()
    chart = (
        alt.Chart(world_map)
        .mark_geoshape(xOffset=2)
        .transform_lookup(
            lookup="id", from_=alt.LookupData(df1, "id", ["life_expectancy", "country"])
        )
        .encode(
            tooltip=["country:N", "life_expectancy:Q"],
            color=alt.Color("life_expectancy:Q", title = "Life Expectancy"),
            opacity=alt.condition(map_click, alt.value(1), alt.value(0.2)),
        )
        .add_selection(map_click)
        .project("equalEarth", scale=140)
        .properties(width = 625, height = 445,)
        .configure_view(strokeWidth=0)
    )
    return chart.to_html()
Exemple #10
0
def plot_map(country=None):
    #wine_country = pd.DataFrame()
    #if country:
    #    wine_country = wine_df[wine_df['country'] == country]
    #else:
    #   wine_country = wine_df

    wine_countryid = wine_df.merge(country_ids,
                                   left_on='country',
                                   right_on='name',
                                   how='left')
    wine_countryid = wine_countryid.value_counts(
        ['id']).reset_index(name='wine_count')

    alt.renderers.enable('default')
    world = data.world_110m()
    world_map = alt.topo_feature(data.world_110m.url, 'countries')

    chart = (alt.Chart(world_map).mark_geoshape().transform_lookup(
        lookup='id',
        from_=alt.LookupData(
            wine_countryid, 'id',
            ['wine_count'])).encode(color='wine_count:Q').project('equalEarth',
                                                                  scale=90))
    return chart.to_html()
Exemple #11
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
Exemple #12
0
def create_map_series(df):
    # Note that a LayeredChart cannot contain faceted charts as its elements.
    # Therefore, we cannot use a basemap in this configuration, since it would produce an invalid specification.
    # More info: https://github.com/altair-viz/altair/issues/785

    df2 = df.groupby('context').mean()
    df2['context'] = df2.index
    print(df2)

    grid = alt.topo_feature(
        'https://raw.githubusercontent.com/anitagraser/sandbox/master/grid40.topojson',
        'grid40')
    variable_list = [
        'distance_error', 'along_track_error', 'cross_track_error'
    ]
    map_chart = alt.Chart(grid, title=SHIPTYPE).mark_geoshape(
        stroke='white', strokeWidth=2).encode(
            alt.Color(
                alt.repeat('column'),
                type='quantitative',
                scale=alt.Scale(domain=(0, 2000)))).transform_lookup(
                    lookup='properties.id',
                    from_=alt.LookupData(
                        df2, 'context',
                        variable_list)).project(type='mercator').properties(
                            width=300, height=200).repeat(
                                column=variable_list).resolve_scale(
                                    color='shared')

    with open(INPUT.replace('.csv', '_{}_map_series.vega'.format(SHIPTYPE)),
              'w') as map_output:
        map_output.write(map_chart.to_json(indent=2))
def global_covid_map(cc_m):
    world_source = cc_m

    source = alt.topo_feature(data.world_110m.url, "countries")
    background = alt.Chart(source).mark_geoshape(fill="white")

    foreground = (alt.Chart(source).mark_geoshape(
        stroke="black", strokeWidth=0.15).encode(
            color=alt.Color(
                "TotalConfirmed:N",
                scale=alt.Scale(scheme="redpurple"),
                legend=None,
            ),
            tooltip=[
                alt.Tooltip("Country:N", title="Country"),
                alt.Tooltip("TotalConfirmed:Q", title="confirmed cases"),
            ],
        ).transform_lookup(
            lookup="id",
            from_=alt.LookupData(world_source, "id",
                                 ["TotalConfirmed", "Country"]),
        ))

    final_map = ((background + foreground).configure_view(
        strokeWidth=0).properties(width=700,
                                  height=400).project("naturalEarth1"))
    final_map_json = final_map.to_json()
    return final_map_json
    def map_hu(self):
        hurricanes_full_set = self.full_storm_data[
            self.full_storm_data['identifier'].isin(self.hurricane_history)]
        world = data.world_110m.url
        the_map = alt.layer(
            # use the sphere of the Earth as the base layer
            alt.Chart({
                'sphere': True
            }).mark_geoshape(fill='#e6f3ff'),
            # add a graticule for geographic reference lines
            alt.Chart({
                'graticule': True
            }).mark_geoshape(stroke='#ffffff', strokeWidth=1),
            # and then the countries of the world
            alt.Chart(alt.topo_feature(world, 'countries')
                      ).mark_geoshape(fill='#2a1d0c',
                                      stroke='#706545',
                                      strokeWidth=0.5),
            # plot paths of hurricanes that have hit location
            alt.Chart(hurricanes_full_set).mark_line(
                strokeWidth=2,
                opacity=0.5,
            ).encode(latitude='latitude:Q',
                     longitude='longitude:Q',
                     color=alt.Color('identifier:N', legend=None),
                     tooltip=[
                         'name:N', 'identifier:N', 'year(datetime)',
                         'latitude:Q', 'longitude:Q'
                     ]),
        ).project(type='mercator', scale=400,
                  translate=[700, 350]).properties(width=600, height=400)

        return the_map.to_json()
def make_map(site):

    if not isinstance(site, list):
        site_temp = list(site)
    else:
        site_temp = site

    states = alt.topo_feature(data.us_10m.url, feature='states')
    background = alt.Chart(states).mark_geoshape(
        fill='lightgray',
        stroke='blue').properties(width=500, height=300).transform_filter(
            (alt.datum.id == 27))

    sites = pd.DataFrame({
        'site': wr.barley_df['site'].unique().tolist(),
        'lat': [10, 0, -38, -43, 10, 18],
        'long': [-40, -70, -30, 50, 30, 10]
    })

    sites_filter = sites[sites['site'].isin(site_temp)]

    points = alt.Chart(sites_filter).mark_circle(size=100, color='red').encode(
        x=alt.X('lat:Q', scale=alt.Scale(domain=[-100, 100]), axis=None),
        y=alt.Y('long:Q', scale=alt.Scale(domain=[-100, 100]), axis=None),
        tooltip=['site'])

    chart = (background + points)

    return chart.to_html()
Exemple #16
0
def plot_worldmap(year_range):
    df1 = dataset_df.copy()
    world = data.world_110m()
    world_map = alt.topo_feature(data.world_110m.url, "countries")
    chosen_ending_year = year_range[1]

    country_ids = pd.read_csv(
        "https://raw.github.ubc.ca/MDS-2020-21/DSCI_532_viz-2_students/master/data/country-ids.csv?token=AAAANV4AYXPDYXASWHWFLDLACHCJK"
    )
    df1 = pd.merge(df1, country_ids, left_on="country",
                   right_on="name").iloc[:, :-1]
    df1 = df1[df1["year"] == chosen_ending_year]

    map_click = alt.selection_multi()
    chart = (alt.Chart(world_map).mark_geoshape(xOffset=2).transform_lookup(
        lookup="id",
        from_=alt.LookupData(
            df1, "id", ["life_expectancy", "country"])).encode(
                tooltip=[
                    alt.Tooltip("country:N", title="Country"),
                    alt.Tooltip("life_expectancy:Q", title="Life Exp")
                ],
                color=alt.Color("life_expectancy:Q", legend=None),
                opacity=alt.condition(map_click, alt.value(1), alt.value(0.2)),
            ).add_selection(map_click).project("equalEarth",
                                               scale=100).properties(
                                                   width=475,
                                                   height=475,
                                               ).configure_view(strokeWidth=0))
    return chart.to_html()
def plot_norway():
    """ Makes a interactive geomap of norway with number of cases per 100k in every county.

    :return: the html file showing interactive geomap.
    """

    data = pd.read_csv("resources/covid_rate_per_100000.csv",
                       sep=';',
                       index_col=False)
    county_list = data["Category"].to_list()
    insidens_list = [
        float(i.replace(',', '.')) for i in data["Insidens"].to_list()
    ]

    data_as_dict = {"Category": county_list, "Insidens": insidens_list}
    df = pd.DataFrame.from_dict(data_as_dict)

    counties = alt.topo_feature(
        "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/norway/norway-new-counties.json",
        "Fylker")

    nearest = alt.selection(type="single",
                            on="mouseover",
                            fields=["properties.navn"],
                            empty="none")

    fig = alt.Chart(counties).mark_geoshape().encode(
        tooltip=[
            alt.Tooltip("properties.navn:N", title="County"),
            alt.Tooltip("Insidens:Q", title="Cases per 100k capita"),
        ],
        color=alt.Color("Insidens:Q",
                        scale=alt.Scale(scheme="reds"),
                        legend=alt.Legend(title="Cases per 100k capita")),
        stroke=alt.condition(nearest, alt.value("gray"), alt.value(None)),
        opacity=alt.condition(nearest, alt.value(1), alt.value(0.8)),
    ).transform_lookup(
        lookup="properties.navn",
        from_=alt.LookupData(df, "Category", ["Insidens"])).properties(
            width=700,
            height=800,
            title="Number of cases per 100k in every county",
        ).add_selection(nearest)

    fig.save("templates/interactive_map.html")

    soup = BeautifulSoup(open("templates/interactive_map.html"), 'html.parser')

    head = soup.find('head')
    body = soup.find('body')
    script = soup.find('body').find('script')

    return render_template(
        "map.html",
        head=head,
        body=body,
        script=script,
        from_date=from_date,
        to_date=to_date,
    )
    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()
Exemple #19
0
def get_demographic_country(df):

    df2 = df.copy()
    df2.Country = df2.Country.fillna('<MISSING>')
    df2 = pd.DataFrame(df2.groupby(by=['ROWID','Country']).size()\
        .reset_index()[['ROWID','Country']].Country.value_counts(dropna=False))\
        .reset_index().rename(columns={'index':'country', 'Country':'count'}).sort_values(by='country')

    ctry = pd.DataFrame({
        'country': [
            '<MISSING>', 'Afghanistan', 'Canada', 'China', 'France',
            'Hong Kong (S.A.R.)', 'India', 'Italy', 'Mexico', 'New Zealand',
            'Portugal', 'Singapore',
            'United Kingdom of Great Britain and Northern Ireland',
            'United States of America'
        ],
        'id':
        [0, 4, 124, 156, 250, 344, 356, 380, 484, 554, 620, 702, 826, 840]
    })

    df2 = df2.merge(ctry, how='inner', on='country')

    source = alt.topo_feature(data.world_110m.url, "countries")
    background = alt.Chart(source).mark_geoshape(fill="white")

    foreground = (alt.Chart(source).mark_geoshape(
        stroke=berkeley_palette['bay_fog'], strokeWidth=0.25).encode(
            color=alt.Color(
                "count:N",
                scale=alt.Scale(range=[
                    berkeley_palette['pacific'], berkeley_palette['lawrence'],
                    berkeley_palette['lap_lane'],
                    berkeley_palette['founders_rock'],
                    berkeley_palette['founders_rock'],
                    berkeley_palette['berkeley_blue']
                ]),
                legend=None,
            ),
            tooltip=[
                alt.Tooltip("country:N", title="Country"),
                alt.Tooltip("count:Q", title="Participants"),
            ],
        ).transform_lookup(
            lookup="id",
            from_=alt.LookupData(df2, "id", ["count", "country"]),
        ))

    final_map = alt.layer(background, foreground)\
        .properties(width=700, height=400, title={'text':'Distribution of Country'})\
        .configure_title(anchor='middle')\
        .configure_title(dy=-10)\
        .project("naturalEarth1")\
        .configure(padding={'top':20, 'left':20, 'right':20,'bottom':20})\
        .configure_view(stroke=None, strokeWidth=0)\
        .configure_axis(grid=False)

    return final_map
Exemple #20
0
def plot_choro_usa(dataset=f"data/{_OUTPUT_JSON_FILENAME}"):
    """
    Function to plot a choropleth map of the mean sentiment per US state.

    Args:
        dataset: JSON formatted file with tweet data.
            Defaults to "data/_OUTPUT_JSON_FILENAME".

    Returns:
        output map: choropleth map of the mean sentiment per US state.

    Raises:
        ValueError: If no tweets from the US can be found in the dataset.
    """
    try:
        # calling utility function to create dataframe
        df_choro = create_dataframe_choropleth(dataset)
        # using vega_datasets(data) to get the borders of the states of the US
        states = alt.topo_feature(data.us_10m.url, feature="states")

        # generating choropleth chart
        choro = alt.Chart(df_choro).mark_geoshape().encode(
            color=alt.Color('sentiment',
                            legend=alt.Legend(title="Positivity",
                                              tickCount=5,
                                              tickMinStep=0.5),
                            scale=alt.Scale(
                                scheme='redyellowgreen', domain=[-1, 1])
                            ),
            tooltip=["name:N", "sentiment"]
        ).properties(
            width=720,
            height=400,
            title="Average tweet sentiment per state of the US"
        )

        # # generating chart consisting of the states
        background_usa = alt.Chart(states).mark_geoshape(
            fill="lightgray",
            stroke="white"
        ).properties(
            width=720,
            height=400
        )

        # generating the final layered chart
        output_map = (background_usa + choro).project("albersUsa")

        # output_map = background_usa.project("albersUsa")

        return output_map

    except Exception:
        raise ValueError("""There are no tweets from the US in the dataset yet!
            Try running get_data() again to collect more data or
            select another dataset.""")
Exemple #21
0
 def get_geomap(self, feature):
     """Returns topological encoding, topological style,
     and translation function based on geographic feature"""
     maps = {
         "state": (
             alt.topo_feature(Choropleth.us_url, feature="states"),
             f"alt.topo_feature('{Choropleth.us_url}', feature='states')",
             "albersUsa",
             self.get_us_fips_code,
         ),
         "country": (
             alt.topo_feature(Choropleth.world_url, feature="countries"),
             f"alt.topo_feature('{Choropleth.world_url}', feature='countries')",
             "equirectangular",
             self.get_country_iso_code,
         ),
     }
     assert feature in maps
     return maps[feature]
Exemple #22
0
 def get_background(self, feature):
     """Returns background projection based on geographic feature."""
     maps = {
         "state": (
             alt.topo_feature(Choropleth.us_url, feature="states"),
             "albersUsa",
             f"alt.topo_feature('{Choropleth.us_url}', feature='states')",
         ),
         "country": (
             alt.topo_feature(Choropleth.world_url, feature="countries"),
             "equirectangular",
             f"alt.topo_feature('{Choropleth.world_url}', feature='countries')",
         ),
     }
     assert feature in maps
     background = (alt.Chart(maps[feature][0]).mark_geoshape(
         fill="lightgray", stroke="white").project(maps[feature][1]))
     background_str = f"(alt.Chart({maps[feature][2]}).mark_geoshape(fill='lightgray', stroke='white').project('{maps[feature][1]}'))"
     return background, background_str
Exemple #23
0
def plot_tweets_usa(dataset=f"data/{_OUTPUT_JSON_FILENAME}"):
    """
    Function to plot all tweets of the US as points using altair.

    Args:
        dataset: JSON formatted file with tweet data.
            Defaults to "data/_OUTPUT_JSON_FILENAME".

    Returns:
        output_map: map of the US and the tweets sent from there.

    Raises:
        ValueError: If no tweets from the US can be found in the dataset.
    """
    try:
        # calling utility function to create dataframe
        df = create_dataframe_us(dataset)
        # using vega_datasets(data) to get the borders of the states of the US
        states = alt.topo_feature(data.us_10m.url, feature="states")

        # disable the max rows restriction to be able to chart larger datasets
        alt.data_transformers.disable_max_rows()

        # generating chart consisting of the states
        background_usa = alt.Chart(states).mark_geoshape(
            fill="lightgray",
            stroke="white"
        ).properties(
            width=720,
            height=400
        )

        # generating chart consisting of the tweets from the US
        points_usa = alt.Chart(df).mark_square(size=5, opacity=0.5).encode(
            longitude="coord_lon",
            latitude="coord_lat",
            color=alt.Color("sentiment:N",
                            scale=alt.Scale(domain=[
                                "positive", "neutral", "negative"
                            ],
                                range=["green", "orange", "red"]
                            )),
            tooltip=["user_name", "text", "sentiment"]
        ).properties(title="Tweets of the US")

        # generating the final layered chart
        output_map = (background_usa + points_usa).project("albersUsa")

        return output_map

    except Exception:
        raise ValueError("""There are no tweets from the US in the dataset yet!
            Try running get_data() again to collect more data or
            select another dataset.""")
Exemple #24
0
def plot_map(df, state_id=6):
    """
    Plot a Choropleth map of US States and Wine Reviews.

    Parameters:
    -----------
    df -- (pandas DataFrame) Cleaned data in a dataframe.
    state_id -- (int) Integer of state_id (ex. 6 for 'California)

    Returns altiar plot objects.
    """

    wine_counties = wrangle_counties(df)
    counties = alt.topo_feature(data.us_10m.url, 'counties')

    state = lookup_state_id(df, state_id)
    state_id = state['state_id']

    colormap = alt.Scale(domain=[0, 100, 500, 1000, 2000, 4000, 8000],
                         range=[
                             '#C7DBEA', '#CCCCFF', '#B8AED2', '#3A41C61',
                             '#9980D4', '#4634A7', '#4C2C96'
                         ])

    c_foreground = (alt.Chart(counties).mark_geoshape(
        stroke='black', strokeWidth=1).encode(
            color=alt.Color('Num Reviews:Q', scale=colormap),
            tooltip=[
                alt.Tooltip('County:O'),
                alt.Tooltip('Ave Rating:Q', format='.2f'),
                alt.Tooltip('Ave Price:Q', format='$.2f'),
                alt.Tooltip('Ave Value:Q', format='.2f'),
                alt.Tooltip('Num Reviews:Q')
            ]).transform_calculate(
                state_id="(datum.id / 1000)|0").transform_filter(
                    (alt.datum.state_id) == state_id).transform_lookup(
                        lookup='id',
                        from_=alt.LookupData(wine_counties, 'County ID', [
                            'County', 'County ID', 'Ave Rating', 'Ave Price',
                            'Ave Value', 'Num Reviews'
                        ])))

    c_background = (alt.Chart(counties).mark_geoshape(
        fill='dimgray', stroke='gray').transform_calculate(
            state_id="(datum.id / 1000)|0").transform_filter(
                (alt.datum.state_id) == state_id)).project('albersUsa')

    return (c_background + c_foreground).configure_view(height=400,
                                                        width=240,
                                                        strokeWidth=4,
                                                        fill=None,
                                                        stroke=None)
def plot_norway():
    """Creates a plot displaying the reported COVID-19 rate per 100000 
        inhabitants by county in Norway.
    
    The data was last updated 11.11.2020.

    Returns:
        altair.vegalite.v4.api.Chart: The plotted chart.
    """
    data = pd.read_csv("antall-meldte-tilfeller.csv", sep=";")

    # topojson of norways counties
    counties = alt.topo_feature(
        "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/norway/norway-new-counties.json",
        "Fylker")

    # Used for highlighting
    nearest = alt.selection(type="single",
                            on="mouseover",
                            fields=["properties.navn"],
                            empty="none")

    fig = alt.Chart(counties).mark_geoshape().encode(
        tooltip=[
            alt.Tooltip("properties.navn:N", title="Fylke"),
            alt.Tooltip("Insidens:Q", title="Tilfeller per 100 000"),
        ],
        color=alt.Color("Insidens:Q",
                        scale=alt.Scale(scheme="reds"),
                        legend=alt.Legend(title="Tilfeller per 100 000",
                                          direction="horizontal",
                                          orient="bottom",
                                          gradientLength=375)),
        stroke=alt.condition(nearest, alt.value("grey"), alt.value(None)),
        opacity=alt.condition(nearest, alt.value(1), alt.value(0.8)),

        # Map cases to counties
    ).transform_lookup(
        lookup="properties.navn",
        from_=alt.LookupData(data, "Category", ["Insidens"])).properties(
            width=375,
            height=450,
            title={
                "text":
                "Antall tilfeller per 100 000 innbyggere i hvert fylke",
                "subtitle": "Oppdatert 11.11.2020",
                "subtitleColor": "lightgray"
            }).add_selection(nearest).configure_view(
                strokeOpacity=0  #  Removes borders
            )

    return fig
Exemple #26
0
def get_demographic_state(df):

    df2 = df.copy()
    df2.State = df2.State.fillna('<MISSING>')
    df2 = pd.DataFrame(df2.groupby(by=['ROWID','State']).size()\
        .reset_index()[['ROWID','State']].State.value_counts(dropna=False))\
        .reset_index().rename(columns={'index':'state', 'State':'count'}).sort_values(by='state')

    codes = pd.DataFrame({
        'state': [
            'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
            'Colorado', 'Connecticut', 'Delaware', 'District of Columbia',
            'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
            'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
            'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
            'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
            'New Jersey', 'New Mexico', 'New York', 'North Carolina',
            'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania',
            'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee',
            'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
            'West Virginia', 'Wisconsin', 'Wyoming', 'Puerto Rico'
        ],
        'id': [
            1, 2, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21,
            22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
            39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 72
        ]
    })

    df2 = df2.merge(codes, how='left', on='state').fillna(-99)
    df2.id = df2.id.astype(int)

    states = alt.topo_feature(data.us_10m.url, 'states')
    b = alt.Chart(states).mark_geoshape(stroke=berkeley_palette['white'], strokeWidth=0.25).encode(
            color=alt.Color(
                "count:N", scale=alt.Scale(range=[berkeley_palette['pacific'], "#00b0da",
                "#009dcb", "#008aba", "#0077aa", "#006598", "#005386", "#004274", "#003262"]), legend=None),
            tooltip=[
                alt.Tooltip("state:N", title="U.S. State"),
                alt.Tooltip("count:Q", title="Participants")]
        ).transform_lookup(
            lookup='id',
            from_=alt.LookupData(df2, 'id', ["count","state"]))\
        .project(type='albersUsa')\
        .properties(width=700, height=400, title={'text':'Distribution of U.S. State'})\
        .configure_title(anchor='middle')\
        .configure_title(dy=-10)\
        .configure(padding={'top':20, 'left':20, 'right':20,'bottom':20})\
        .configure_view(stroke=None, strokeWidth=0)\
        .configure_axis(grid=False)

    return b
Exemple #27
0
def plot_norway():
    """
    Plots number of cases per 100k on each county on a chart depicting Norway.

    Returns:
        jsonDict(dict): plot json structure in dictionary

    """
    #get directory
    currentDir = pathlib.Path(__file__).parent.absolute()
    countyDir = str(currentDir) + "/covcounties/"
    os.chdir(countyDir)
    #create dataframe from county csv file
    data = pd.read_csv(countyDir + "reported-county.csv")
    #extract counties from json file
    counties = alt.topo_feature(
        "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/norway/norway-new-counties.json",
        "Fylker")
    #nearest selection of county
    nearest = alt.selection(type="single",
                            on="mouseover",
                            fields=["properties.navn"],
                            empty="none")
    #plot country chart
    country_chart = alt.Chart(counties).mark_geoshape().encode(
        #hover effect
        tooltip=[
            alt.Tooltip("properties.navn:N", title="Fylke"),
            alt.Tooltip("Insidens:Q", title="Antall meldte tilfeller per 100k")
        ],
        color=alt.Color(
            "Insidens:Q",
            scale=alt.Scale(scheme="reds"),
            legend=alt.Legend(title="Antall meldte tilfeller per 100k")),
        stroke=alt.condition(nearest, alt.value("gray"), alt.value(None)),
        opacity=alt.condition(nearest, alt.value(1), alt.value(0.8)),
        #lookup number of cases from dataframe and map it to counties
    ).transform_lookup(
        lookup="properties.navn",
        from_=alt.LookupData(data, "Category", ["Insidens"])).properties(
            width=500,
            height=600,
            title="Antall meldte tilfeller per 100k i hvert fylke"
        ).add_selection(nearest)

    #Save to file and return json string
    json = country_chart.to_json()

    #Fix json translation of null to None
    #jsonDict['encoding']['stroke']['value'] = "null"

    return json
Exemple #28
0
def draw_map():
    fips=session.get('fips')
    state_id=int(str(fips)[0:-3])

    states_topo=alt.topo_feature(
        'https://raw.githubusercontent.com/vega/vega-datasets/master/data/us-10m.json', feature='states')
    counties_topo=alt.topo_feature(
        'https://raw.githubusercontent.com/vega/vega-datasets/master/data/us-10m.json', feature='counties')

    state_map=Chart(data=states_topo, height=HEIGHT, width=WIDTH).mark_geoshape(
                fill='#827f7f',
                stroke='white'
            ).transform_filter((alt.datum.id == state_id))

    county_map=Chart(data=counties_topo, height=HEIGHT, width=WIDTH).mark_geoshape(
                fill='red',
                stroke='white'
            ).transform_filter((alt.datum.id == int(fips)))

    chart=alt.layer(state_map, county_map).configure_view(strokeWidth=0)

    return chart.to_json()
Exemple #29
0
def map(subdf, top_map, micro_cluster_selector, date_range):
    ''' generate map with ad location data
        :param df:  Pandas DataFrame with latitude, longitude, and count data
        :return:    altair map with ad counts displayed '''

    if len(micro_cluster_selector):
        top_map = {k: v for k, v in top_map.items(
        ) if v in micro_cluster_selector}
    subdf = subdf[subdf['micro-clusters'].isin(top_map.keys())]
    df = subdf[['ad_id', 'days', 'lat', 'lon', 'location']].copy()
    df['micro-clusters'] = subdf['micro-clusters'].apply(
        lambda val: top_map[val])

    date_range = pd.date_range(*date_range)
    df = df[((df.lat != 1) | (df.lon != 1)) & (df.days.isin(date_range))]

    countries = alt.topo_feature(data.world_110m.url, 'countries')

    base = alt.Chart(countries).mark_geoshape(
        fill='#eeeeee',
        stroke='#DDDDDD'
    ).properties(
        height=650,
        width=1000)

    agg_df = utils.aggregate_locations(df)
    center, scale = utils.get_center_scale(agg_df.lat, agg_df.lon)
    domain = [agg_df['count'].min(), agg_df['count'].max()]

    scatter = alt.Chart(agg_df).transform_aggregate(
        groupby=['location'],
        count='sum(count)',
        lat='mean(lat)',
        lon='mean(lon)'
    ).mark_circle(
        color=utils.LOCATION_COLOR,
        fillOpacity=.5,
    ).encode(
        size=alt.Size('count:Q', scale=alt.Scale(
            domain=domain), legend=None),
        longitude='lon:Q',
        latitude='lat:Q',
        tooltip=['location', 'count']
    )

    return (base + scatter).project(
        'equirectangular',
        center=center,
        scale=scale
    )
Exemple #30
0
def kaart_plot(cn6_x):
    # Title
    st.subheader("International trade flows to and from Europe in 2019")
    x = pd.read_csv('data/edges_puhastus2019.csv', sep=';')
    x = x.loc[:, ~x.columns.str.contains('^Unnamed')]
    x = remap_cn6(x)
    countries = pd.read_csv('countries.csv', sep=';')
    world = alt.topo_feature(data.world_110m.url, 'countries')
    x = x[x["cn6"] == cn6_x]
    conns = x
    select_country = alt.selection_single(on="mouseover",
                                          nearest=True,
                                          fields=["origin"],
                                          empty="none")
    lookup_data = alt.LookupData(countries,
                                 key="country",
                                 fields=["name", "latitude", "longitude"])
    background = alt.Chart(world).mark_geoshape(
        fill="lightgray",
        stroke="white").properties(width=1000,
                                   height=800).project(type='naturalEarth1')
    connections = alt.Chart(conns).mark_rule(opacity=0.35).encode(
        latitude="latitude:Q",
        longitude="longitude:Q",
        latitude2="lat2:Q",
        longitude2="lon2:Q").transform_lookup(
            lookup="origin", from_=lookup_data).transform_lookup(
                lookup="destination",
                from_=lookup_data,
                as_=["country", "lat2",
                     "lon2"]).transform_filter(select_country)
    points = alt.Chart(conns).mark_circle().encode(
        latitude="latitude:Q",
        longitude="longitude:Q",
        size=alt.Size("connections:Q",
                      scale=alt.Scale(range=[0, 400]),
                      legend=None),
        order=alt.Order("connections:Q", sort="descending"),
        tooltip=["origin:N", "connections:Q"]).transform_aggregate(
            connections="count()", groupby=["origin"]).transform_lookup(
                lookup="origin",
                from_=lookup_data).add_selection(select_country)
    st.altair_chart((background + connections +
                     points).configure_view(stroke=None).resolve_scale())
    # Description
    st.markdown("""
    International trade flows to and from EU in 2019. The size of the country node shows
    the number of trade flows to and from other countries. Data: 
    [Comext](https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&dir=comext) yearly data.
                """)
Exemple #31
0
"""
U.S. state capitals overlayed on a map of the U.S
-------------------------------------------------
This is a layered geographic visualization that shows US capitals
overlayed on a map.
"""
# category: case studies
import altair as alt
from vega_datasets import data

states = alt.topo_feature(data.us_10m.url, 'states')
capitals = data.us_state_capitals.url

# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    title='US State Capitols',
    width=700,
    height=400
).project('albersUsa')

# Points and text
hover = alt.selection(type='single', on='mouseover', nearest=True,
                      fields=['lat', 'lon'])

base = alt.Chart(capitals).encode(
    longitude='lon:Q',
    latitude='lat:Q'
)
Exemple #32
0
"""
World Projections
-----------------
This example shows a map of the countries of the world using four available
geographic projections. For more details on the projections available in
Altair, see https://vega.github.io/vega-lite/docs/projection.html
"""
# category: maps
import altair as alt
from vega_datasets import data

source = alt.topo_feature(data.world_110m.url, 'countries')

base = alt.Chart(source).mark_geoshape(
    fill='#666666',
    stroke='white'
).properties(
    width=300,
    height=180
)

projections = ['equirectangular', 'mercator', 'orthographic', 'gnomonic']
charts = [base.project(proj).properties(title=proj)
          for proj in projections]

alt.vconcat(
    alt.hconcat(*charts[:2]),
    alt.hconcat(*charts[2:])
)
Exemple #33
0
"""
Choropleth Map
==============
A choropleth map of unemployment rate per county in the US
"""
# category: maps
import altair as alt
from vega_datasets import data

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

alt.Chart(counties).mark_geoshape().encode(
    color='rate:Q'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).project(
    type='albersUsa'
).properties(
    width=500,
    height=300
)