def serialize_as_altair( topo_object, mesh=True, color=None, tooltip=True, projection="identity", objectname="data", geo_interface=False, ): import altair as alt # create a mesh visualization if geo_interface and mesh: # chart object chart = ( alt.Chart(topo_object, width=300) .mark_geoshape(filled=False) .project(type=projection, reflectY=True) ) # create a mesh visualization elif mesh and color is None: data = alt.InlineData( values=topo_object, format=alt.DataFormat(mesh=objectname, type="topojson") ) chart = ( alt.Chart(data, width=300) .mark_geoshape(filled=False) .project(type=projection, reflectY=True) ) # creating a chloropleth visualisation elif color is not None: data = alt.InlineData( values=topo_object, format=alt.DataFormat(feature=objectname, type="topojson"), ) if tooltip is True: tooltip = [color] chart = ( alt.Chart(data, width=300) .mark_geoshape() .encode( color=alt.Color(color, legend=alt.Legend(columns=2)), tooltip=tooltip ) .project(type=projection, reflectY=True) ) return chart
def convert_gfp_to_alt(gdf: geopandas.geodataframe) -> alt.InlineData: """ Converts a geopandas geoDataFrame into a Altair InlineData instance so it can be converted into a chart. Args: gdf (geopandas.geodataframe): Input geodataframe Returns: alt.InlineData: An instance of Altair's InlineData class. """ data = alt.InlineData( values=gdf.to_json(), # geopandas to geojson string # root object type is "FeatureCollection" but we need its features format=alt.DataFormat(property="features", type="json"), ) return data
def load_district_arrests(): # url for district arrests crime_url = "https://data.cityofchicago.org/resource/ijzp-q8t2.json?$select=year,district,count(district),sum(case(arrest='1',1,true,0))&$group=year,district" #crime_url = "https://data.cityofchicago.org/resource/ijzp-q8t2.json?$select=year,district,count(district)&$group=year,district" df_district_count = pd.read_json(crime_url) df_district_count['district'] = df_district_count['district'].dropna().astype(int).astype(str) url_geojson = "https://data.cityofchicago.org/resource/24zt-jpfn.geojson" data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='the_geom',type='json')) stations = pd.read_json("https://data.cityofchicago.org/resource/z8bn-74gv.json?$select=district,latitude,longitude") stations = stations[stations['district'] != 'Headquarters'] #st.write(type(stations['district'])) #st.write(type(df_district_count['district'])) data = pd.merge(stations, df_district_count, on='district') data.columns = ['district','latitude','longitude','year','count_district','arrests'] data['arrest_rate'] = data['arrests']/data['count_district'] return data
def country_codes(): cc = coco.CountryConverter() df = pd.read_csv( 'https://query.data.world/s/atfqu76bmkmwiranfkzfiqqdnvpzle', encoding="ISO-8859-1") url_geojson = "https://query.data.world/s/ziovq5tz5zcqvizgghygjucscii32t" countries = alt.Data(url=url_geojson, format=alt.DataFormat(property="features", type="json")) unique = pd.DataFrame(df["Country"].unique()) unique = unique.rename(columns={0: "Country"}) unique['iso_a3'] = unique["Country"].apply( lambda x: coco.convert(names=x, to="ISO3")) df = pd.merge(df, unique, how="left", left_on="Country", right_on="Country") return df, countries, unique
import altair as alt from vega_datasets import data alt.renderers.enable('altair_viewer') movies = alt.UrlData(data.movies.url, format=alt.DataFormat(parse={"Release_Date": "date"})) ratings = ['G', 'NC-17', 'PG', 'PG-13', 'R'] genres = [ 'Action', 'Adventure', 'Black Comedy', 'Comedy', 'Concert/Performance', 'Documentary', 'Drama', 'Horror', 'Musical', 'Romantic Comedy', 'Thriller/Suspense', 'Western' ] base = alt.Chart(movies, width=200, height=200).mark_point(filled=True).transform_calculate( Rounded_IMDB_Rating="floor(datum.IMDB_Rating)", Hundred_Million_Production= "datum.Production_Budget > 100000000.0 ? 100 : 10", Release_Year="year(datum.Release_Date)").transform_filter( alt.datum.IMDB_Rating > 0).transform_filter( alt.FieldOneOfPredicate( field='MPAA_Rating', oneOf=ratings)).encode(x=alt.X( 'Worldwide_Gross:Q', scale=alt.Scale(domain=(100000, 10**9), clamp=True)), y='IMDB_Rating:Q', tooltip="Title:N") # A slider filter year_slider = alt.binding_range(min=1969, max=2018, step=1) slider_selection = alt.selection_single(bind=year_slider,
#st.write(type(stations['district'])) #st.write(type(df_district_count['district'])) data = pd.merge(stations, df_district_count, on='district') data.columns = ['district','latitude','longitude','year','count_district','arrests'] data['arrest_rate'] = data['arrests']/data['count_district'] return data data = load_district_arrests() #st.write(data) #### new stuff from vivian #### url_geojson = "https://data.cityofchicago.org/resource/24zt-jpfn.geojson" data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='the_geom',type='json')) stations = pd.read_json("https://data.cityofchicago.org/resource/z8bn-74gv.json") stations = stations[stations['district'] != 'Headquarters'] # print(stations) #st.write(stations) #url_geojson = "https://data.cityofchicago.org/resource/24zt-jpfn.geojson" data_geojson_remote = alt.Data(url=url_geojson, format=alt.DataFormat(property='the_geom',type='json')) # chart object district_map = alt.Chart(data_geojson_remote).mark_geoshape( fill='lightgray', stroke='white'
corr = tracts_gdf_selected[var_demographic2].corr() corr.style.background_gradient(cmap='coolwarm') # It turns out that racial composition (pct_white) is substantially related to education level (pct_bachelor). It's surprisingly that mdeian household income is statistically associated with population, that is, tracts with larger number of population exhibit higher income level. # ### Spatial Patterns # In[34]: import altair as alt alt.renderers.enable('notebook') # In[44]: tracts_alt = alt.InlineData(values=tracts_gdf_selected.dropna().to_json(), format=alt.DataFormat(property='features', type='json')) alt.Chart(tracts_alt).mark_geoshape(stroke='population', ).properties( width=500, height=400, projection={ "type": 'mercator' }, ).encode(tooltip=['properties.population:Q', 'properties.NAME:N'], color='properties.population:Q') # In[45]: alt.Chart(tracts_alt).mark_geoshape(stroke='pct_male', ).properties( width=500, height=400,
def generate_regions_map(data: pd.DataFrame, date, feature: str, title: str, width: int = 600, height: int = 600, log_scale: bool = True) -> alt.Chart: fechas = data['fecha'].apply(lambda x: x.date()) data = data[(fechas == date)] url_geojson = 'https://raw.githubusercontent.com/rubens36/covid19/master/maps/regiones.geojson' regions_shape = alt.Data(url=url_geojson, format=alt.DataFormat(property='features', type='json')) chart_data = data[(data[feature] > 0)][[feature, 'id']] base_chart = (alt.Chart( regions_shape, title='Ubicación de los casos confirmados por región').mark_geoshape( stroke='black', strokeWidth=0.5, color='white').encode( tooltip=[alt.Tooltip('properties.Region:N', title='Región')])) scale = alt.Scale(type='log', scheme='teals') if log_scale else alt.Scale( type='linear', scheme='teals') color_chart = (alt.Chart(regions_shape).mark_geoshape( stroke='black', strokeWidth=0.5).encode( color=alt.Color(f"{feature}:Q", title=(get_title(feature)), scale=scale), tooltip=[ alt.Tooltip('properties.Region:N', title='Región'), alt.Tooltip(f"{feature}:Q", title=(get_title(feature))) ]).transform_lookup('properties.id', from_=alt.LookupData(data=chart_data, key='id', fields=[feature]))) single = alt.selection_single(on='mouseover') circle_chart = (alt.Chart(regions_shape).mark_circle( opacity=0.4, color='red').encode( longitude='properties.centroid_lon:Q', latitude='properties.centroid_lat:Q', size=(alt.condition( single, alt.Size('infectados:Q', scale=alt.Scale(range=[50, 4000]), legend=None), alt.value(0))), tooltip=[ alt.Tooltip('properties.Region:N', title='Región'), alt.Tooltip(f"{feature}:Q", title=(get_title(feature))) ]).transform_lookup('properties.id', from_=alt.LookupData( data=chart_data, key='id', fields=['infectados' ])).properties(selection=single)) final_chart = ((base_chart + color_chart + circle_chart).configure_view( strokeWidth=0).properties(width=width, height=height)) return final_chart
color_continuous_scale=px.colors.sequential.Viridis, height=1200, ) fig.update_geos(fitbounds="locations", visible=False) fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) fig fig.write_html("plotly_choropleth.html") # + # aug_df.crs = {"init": "epsg:27700"} # aug_df = aug_df.to_crs({"init": "epsg:4326"}) # define inline geojson data object data_geojson = alt.InlineData( values=aug_df.to_json(), format=alt.DataFormat(property="features", type="json") ) # chart object poverty_chart = ( alt.Chart(data_geojson) .mark_geoshape() .encode( color=alt.Color("properties.Taux de pauvreté:Q", legend=alt.Legend(columns=2),), tooltip=[ "properties.Délégation:N", "properties.Gouvernorat:N", "properties.Taux de pauvreté:Q", ], ) .properties(width=500, height=700)
color=alt.condition( selector_map, alt.Color("properties.region:N", scale=alt.Scale(scheme="category20")), alt.value("lightgray"), ), tooltip=[ alt.Tooltip("properties.name:N", title="County"), alt.Tooltip("properties.region:N", title="Region"), ], ).add_selection(selector_map).properties(width=500, height=800)) bar_chart = (alt.Chart( alt.Data(url=data_url, format=alt.DataFormat( property="features", type="json"))).transform_fold( ["properties." + c for c in columns], as_=["metric", "value"]).mark_bar().encode( x=alt.X("metric:N"), y=alt.Y("value:Q", axis=alt.Axis(format="%")), color="properties.region:N", ).add_selection(selector_map).transform_filter( selector_map).properties(width=200, height=200)) chart = CA_map | bar_chart if __name__ == "__main__": docs_dir = Path(__file__).parent.parent.absolute() with open(docs_dir / "docs" / "_vega_out.html", "w") as f: