

Import altair as alt # Create a selection that chooses the nearest point & selects based on x-value nearest = alt. By adding a "legend" (actually another chart with interactivity) we can click to highlight one or multiple origins while hiding the others.

interactive() method we can scroll on the chart to zoom in and out as well as pan around. By adding encodings for origin of manufacture (color) and size (number of cylinders) we can view more dimensions of the dataset.
#Altair mark text code#
save ( 'cars-clickable-legend.html' )įor about 3x the amount of code we get a lot more information and ease of exploring this data. properties ( selection = click ) chart = ( scatter | legend ) chart. value ( 'lightgray' ), legend = None ), size = alt. Axis ( title = 'Select Origin' )), color = alt. Color ( 'Origin:N', legend = None ), tooltip =, ). encode ( x = 'Horsepower:Q', y = 'Miles_per_Gallon:Q', size = alt. selection_multi ( encodings = ) # scatter plots of points scatter = alt. Import altair as alt from vega_datasets import data cars = data. Again we simply specify the data (along with data types for each value denoted by :Q in this case), chart type, and encoding. We'll start with a basic static scatter plot showing the relationship between Horsepower and Gas Mileage for a number of cars. Note that the interactivity is best supported by viewing this on a laptop rather than mobile. These are also available in the original Jupyter Notebook. Next I'll walk through several examples of interactive Altair charts. encode ( x = 'x:Q', y = 'mean(y)', )Ī quick summary of the properties available is given in the table below, with links to the Altair documentation of the corresponding section: | Marks |Īltair is well-documented with many helpful examples-see the resources at the bottom of this page for links to more information. Given a simple dataset with columns of x and y we can define a barebones Altair chart like this:Īlt. This approach makes for rapid exploration of your data and iteration between chart types.Įvery Altair chart is made up of Data, Marks, and Encodings, which can be modified with Binning and Aggregation.
#Altair mark text how to#
Instead of imperatively specifying how to render the visualization as in matplotlib, with Altair (and vega/vega-lite) you specify what to visualize. As Jake VanderPlas explains when presenting Altair, this allows visualization concepts to map directly to visualization implementation. Intro to AltairĪltair is a visualization library for Python notable for taking a declarative approach based on a grammar of graphics using Vega and Vega-Lite. The examples below are largely derived from the excellent Altair gallery-I claim no original work on these but enjoyed working with them to learn the mechanics of interactive visualization in Altair.

In this post I will show some examples of using the Altair library to create and share some simple interactive visualizations. Sharing interactive visualizations online extends the benefits to others.
