# first we include the two libraries that we will work with
import pandas as pd
import altair as alt
cities = pd.read_csv("cities.csv")
# create a simple scatterplot with two dimensions for x and y
alt.Chart(cities).mark_circle().encode(
# position based on population and elevation:
x='population',
y='elevation',
).interactive() # this makes the axes interactive: now you can zoom & pan
alt.Chart(cities).mark_circle().encode(
x='population',
y='elevation',
tooltip=['name', 'country'] # add tooltip for name and country
).interactive()
# create a selection based on the column continent, linked to the legend:
selection = alt.selection_point(fields=['continent'], bind='legend')
alt.Chart(cities).mark_circle().encode(
x='population',
y='elevation',
tooltip=['name', 'country'],
# we're adding a color coding, which comes with a legend
color = 'continent',
# the opacity is based on the selection
opacity=alt.condition(selection, alt.value(1), alt.value(0.2))
).add_params(selection) # the selection is applied to the chart
countries = cities.country.unique().tolist()
len(countries)
countries
countries.append(None)
# creating a dropdown element with the countries as options and a name
dropdown = alt.binding_select(options=countries, name="Select a country")
# applying the selection to the country column and using the dropwdown element
selection = alt.selection_point(fields=['country'], bind=dropdown)
alt.Chart(cities).mark_circle().encode(
x='population',
y='elevation',
tooltip=['name', 'country'],
color = 'continent',
opacity=alt.condition(selection, alt.value(1), alt.value(0))
).add_params(selection)
# we again create a selection, which links the interaction based on continents
selection = alt.selection_point(fields=['continent'])
# this sets up a layout of charts with a size of 250x250 pixels each
base = alt.Chart(cities).properties(width=250, height=250)
# this creates a scatterplot of city population & elevation averages by continent
scat = base.mark_circle().encode(
x='mean(population)',
y='mean(elevation)',
size='count()', # this refers to the number of cities
tooltip=['continent', alt.Tooltip('count()', title='big cities')],
color=alt.condition(selection, 'continent', alt.value('lightgray'))
).add_params(selection)
# this last step specifies where the selection can be made
# histogram showing the distribution of cities according to population
hist = base.mark_bar(color="darkgray").encode(
x=alt.X('population', bin=alt.Bin(maxbins=30)),
y='count()',
).transform_filter(selection)
# this last step applies the continent selection from scatterpot to histogram
# the two charts are arranged together with the pipe operator
scat | hist
# the type parameter has changed, this time we set intervals in both x and y position
selection = alt.selection_interval(encodings=['x', 'y'])
# first we create a pseudo map using the geographic positions (lat and long) for a scatterplot
map = alt.Chart(cities).mark_point(filled=True, size=5).encode(
x='long',
y='lat',
# the dots are rendered black, unless outside of the selection
color=alt.condition(selection, alt.value("black"), alt.value("lightgray"))
).properties(
width=400, height=200
).add_params(selection)
# define background chart
base = alt.Chart().mark_bar(color="black").encode(
x=alt.X("population:Q", bin=alt.Bin(maxbins=30)),
y="count()",
).properties(
width=200, height=200
)
# lightgray background with selection
background = base.encode( color=alt.value('lightgray') ).add_params(selection)
# black highlights on the transformed data
highlight = base.transform_filter(selection)
# layer the two charts on top of each other
layers = alt.layer(background, highlight, data=cities)
map | layers