[Altair](https://altair-viz.github.io/) is a wrap-around package for [[Vega-lite]] in [[Python]] for [[data visualization]] with [[JavaScript]]. An alternative to Altaire is [[Plotly]]. See this [Altair Tutorial](https://altair-viz.github.io/altair-tutorial/README.html) for a helpful introduction.
Install Altair using [[mamba]].
```bash
mamba install altair
# optionally add datasets
mamba install vega_datasets
```
Charts are create using the [[grammar of graphics]], for example a bar chart can be created with (where `data` is a [[pandas]] dataframe):
```python
import altair as alt
alt.Chart(data)
.mark_bar()
.encode(
x="<variable>",
y="<response>"
)
```
Export charts as SVG or PNG from a [[Jupyter Notebook]], or export JavaScript code that can be embedded in an [[HTML]] page.
```python
my_chart = alt.Chart(data).mark_bar().encode(...)
my_chart.save("<path.html>", embed_options={'render': 'svg'})
```
Default colors in Altair map closely to the [[Tableau]] colors. Altair also offers [alternative color schemes](https://altair-viz.github.io/user_guide/customization.html#customizing-colors) based on the Vega-Lite package. Size can also be adjusted to encode additional information.
```python
alt.Chart(data)
.mark_circle()
.encode(
x="<variable>",
y="<response>",
color=alt.Color("<variable>", scale=alt.Scale(scheme="spectral"))
size="<variable2>"
)
```
Add interactions with tooltips.
```python
alt.Chart(data)
.mark_bar()
.encode(
x="<variable>",
y="<response>",
color=alt.Color("<variable>", scale=alt.Scale(scheme="spectral")),
size="<variable2>",
tooltip=["<variable1>", "<variable2>"]
)
```
To create a side-by-side facet plot, save two or more charts to a variable and use the pipe `|` command. Use the ampersand `&` for vertical concatenation.
```python
# side-by-side
my_barchart | my_scatterplot
# vertical concatentation
my_barchart & my_scatterplot
```
To create small multiples, use the [`repeat`](https://altair-viz.github.io/altair-tutorial/notebooks/04-Compound-charts.html#repeat-chart) command.