Geoshape#
mark_geoshape
represents an arbitrary shapes whose geometry is determined by specified spatial data.
Geoshape Mark Properties#
A geoshape
mark can contain any standard mark properties.
Basic Map#
Altair can work with many different geographical data formats, including geojson and topojson files. Often, the most convenient input format to use is a GeoDataFrame
. Here we load the Natural Earth 110m Cultural Vectors dataset and create a basic map using mark_geoshape
:
import altair as alt
from vega_datasets import data
import geopandas as gpd
url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
gdf_ne = gpd.read_file(url) # zipped shapefile
gdf_ne = gdf_ne[["NAME", "CONTINENT", "POP_EST", 'geometry']]
alt.Chart(gdf_ne).mark_geoshape()
In the example above, Altair applies a default blue fill
color and uses a default map projection (equalEarth
). We can customize the colors and boundary stroke widths using standard mark properties. Using the project
method we can also define a custom map projection manually:
alt.Chart(gdf_ne).mark_geoshape(
fill='lightgrey', stroke='white', strokeWidth=0.5
).project(
type='albers'
)
Focus & Filtering#
By default Altair automatically adjusts the projection so that all the data fits within the width and height of the chart. Multiple approaches can be used to focus on specific regions of your spatial data. Namely:
Filter the source data within your GeoDataFrame.
Filter the source data using a
transform_filter
.Specify
scale
(zoom level) andtranslate
(panning) within theproject
method.Specify
fit
(extent) within theproject
&clip=True
in the mark properties.
The following examples applies these approaches to focus on continental Africa:
Filter the source data within your GeoDataFrame:
gdf_sel = gdf_ne.query("CONTINENT == 'Africa'")
alt.Chart(gdf_sel).mark_geoshape()
Filter the source data using a
transform_filter
:
alt.Chart(gdf_ne).mark_geoshape().transform_filter(
alt.datum.CONTINENT == 'Africa'
)