Annotate an Altair chart
Altair allows you to annotate your charts with text, images, and emojis. You can do this by overlaying two charts to create a layered chart.
Applied concepts
- Use layered charts in Altair to create annotations.
Prerequisites
-
This tutorial requires the following Python libraries:
txt -
This tutorial assumes you have a clean working directory called
your-repository. -
You should have a basic understanding of the Vega-Altair charting library.
Summary
In this example, you will create a time-series chart to track the evolution of stock prices. The chart will have two layers: a data layer and an
annotation layer. Each layer is an altair.Chart object. You will combine the two charts with the + opterator to create a layered chart.
Within the data layer, you'll add a multi-line tooltip to show information about datapoints. To learn more about multi-line tooltips, see this example in Vega-Altair's documentation. You'll add another tooltip to the annotation layer.
Here's a look at what you'll build:
Build the example
Initialize your app
-
In
your_repository, create a file namedapp.py. -
In a terminal, change directories to
your_repository, and start your app:TerminalYour app will be blank because you still need to add code.
-
In
app.py, write the following:PythonYou'll be using these libraries as follows:
- You'll download a dataset using
vega_datasets. - You'll maniputate the data using
pandas. - You'll define a chart using
altair.
- You'll download a dataset using
-
Save your
app.pyfile, and view your running app. -
In your app, select "Always rerun", or press the "A" key.
Your preview will be blank but will automatically update as you save changes to
app.py. -
Return to your code.
Build the data layer
You'll build an interactive time-series chart of the stock prices with a multi-line tooltip. The x-axis represents the date, and the y-axis represents the stock price.
-
Import data from
vega_datasets.PythonThe
@st.cache_datadecorator turnsget_data()into a cahced function. Streamlit will only download the data once since the data will be saved in a cache. For more information about caching, see Caching overview. -
Define a mouseover selection event in Altair.
PythonThis defines a mouseover selection for points.
fields=["date"]allows Altair to identify other points with the same date. You will use this to create a vertical line highlight when a user hovers over a point. -
Define a basic line chart to graph the five series in your data set.
Python -
Draw points on the lines and highlight them based on the mouseover selection.
PythonSince the mouseover selection includes
fields=["date"], Altair will draw circles on each series at the same date. -
Draw a vertical rule at the location of the mouseover selection.
PythonThe
opacityparameter ensures each vertical line is only visible when it's part of a mouseover selection. Eachalt.Tooltipadds a line to your multi-line tooltip. -
Combine the lines, points, and tooltips into a single chart.
Python -
Optional: Test out your code by rendering your data layer.
PythonSave your file and examine the chart in your app. Use your mouse to hover over points. Observe the circle marks, vertical line, and tooltip as you hover over a point. Delete the line or keep it at the end of your app to be updated as you continue.
Build the annotation layer
Now that you have the first chart that shows the data, you can annotate it with text and an emoji. In this section, you'll add some emojis and tooltips to mark specifc points of interest.
-
Create a list of annotations.
PythonThe first two columns ("date" and "price") determine where Altair will place the marker. The third column ("marker") determines what icon Altair will place. The last column ("description") will fill in the associated tooltip.
-
Create a scatter plot with the x-axis representing the date and the y-axis representing the height ("price") of each annotation.
PythonThe
dx=-10, dy=0inside of.mark_text()offsets the icons so they are centered at the coordinate in your annotations dataframe. The four columns are passed to the chart through the.encode()method. If you want to use the same marker for all points, you can removetext="marker"from the.encode()method and add the marker to.mark_text(). For example,.mark_text(text="🥳")would make all the icons be "🥳". For more information about.mark_text(), see Altair's documentation.
Combine the chart layers
-
Define the combined chart.
Python -
Display the chart in Streamlit.
Python
Next steps
Play around with your new app.
-
If you want to use custom images instead of text or emojis to annotation your chart, you can replace the line containing
.mark_text()with.mark_image(). For some URL string stored in a variableIMAGE_URL, you could do something like this:Python -
If you want to enable panning and zooming for your chart, add
.interactive()when you define your combined chart:Python
Still have questions?
Our forums are full of helpful information and Streamlit experts.
