Altair is the newest to Pyhon’s Data Visualization landscape. In this post we will see a simple example of making a histogram using Python’s Altair package.
Let us import Altair as alt and also import data from vega_datasets.
import altair as alt from vega_datasets import data
We will use weather data set from Seattle.
seattle_weather = data.seattle_weather() print(seattle_weather.head(n=3))
The primary object in Altair to make data visualization is Chart. We provide the dataframe we want to use to Chart function and build layers of visualization on top of it.
In this example, we want to build a histogram and we can make a histogram using mark_bar() function. We specify the variable name we want to use to make histogram using encode() function as shown below.
alt.Chart(seattle_weather).mark_bar().encode( alt.X("wind:Q", bin=alt.BinParams()), y='count(*):Q' )
And here is how the simple histogram looks like in Altair.