Altair Stock Charts Example

Visualizing Stocks in Altair

This is an example of using line charts to visualize stocks data. I have created a 7-minute video to accompany this article.

1
2
import altair as alt
from vega_datasets import data
1
source = data.stocks()
1
source.head()
symbol date price
0 MSFT 2000-01-01 39.81
1 MSFT 2000-02-01 36.35
2 MSFT 2000-03-01 43.22
3 MSFT 2000-04-01 28.37
4 MSFT 2000-05-01 25.45
1
source.info()
1
2
3
4
5
6
7
8
9
10
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 560 entries, 0 to 559
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   symbol  560 non-null    object        
 1   date    560 non-null    datetime64[ns]
 2   price   560 non-null    float64       
dtypes: datetime64[ns](1), float64(1), object(1)
memory usage: 13.2+ KB
1
2
3
4
alt.Chart(source).mark_line().encode(
    x = 'date', y = 'price', color = 'symbol',
    strokeDash = 'symbol'
).properties(width=600).configure_axis(grid=False)
1