A histogram is a great tool for quickly assessing a probability distribution
. Python offers a handful of different options for building and plotting histograms. Altair is one of the new Data Visualization techniques in Python. In this tutorial, we will understand how to plot histograms using the Python library.
Also Read: Python Altair tutorial: Creating Interactive Visualizations
Advantages and Disadvantages of Altair
It provides a very different and innovative approach to plotting charts in Python. Another unique feature of Altair is that it allows us to plot interactive plots
. Some disadvantages include limitations on customizing the plots and the absence of 3D visualization.
Code Implementation of Histograms
For installing the Altair library and vega_datasets we have the run the following command in our command prompt.
pip install altair
pip install vega-datasets
Importing the Modules
We will start off by loading the Pandas
and NumPy
libraries. We will also import Altair
and vega_datasets
to get the dataset in the later sections.
import pandas as pd
import numpy as np
import altair as alt
import matplotlib.pyplot as plt
from vega_datasets import data
Loading Dataset for Altair Histogram Plot
When we are working on any visualization, we surely need a dataset. In this tutorial, we will be making use of the vega_datasets
. It is a Python library that gives access to over 60 datasets
of different sizes.
For this tutorial, we will be using the weather data set
from Seattle under the vega_datasets.
seattle_weather_data = data.seattle_weather()
print(seattle_weather_data.head())

Create a simple Histogram Altair Plot
In this tutorial, we want to build a histogram using the mark_bar
function. We can also specify the variable name we want to use with the help of the encode
function using the code below.
alt.Chart(seattle_weather_data).mark_bar().encode(
alt.X("wind:Q",
bin=alt.BinParams()),
y='count(*):Q'
)
And here is what the histogram looks like:

Adding colors on the basis of a column
The next step in the visualization is adding colors to the plot on the basis of a certain column using the codes below. We will be plotting on the basis of two columns, weather
and precipitation
.
alt.Chart(seattle_weather_data).mark_bar().encode(
alt.X("wind:Q",
bin=alt.BinParams()),
y='count(*):Q',
color='weather'
)
And here is what the histogram looks like:

alt.Chart(seattle_weather_data).mark_bar().encode(
alt.X("wind:Q",
bin=alt.BinParams()),
y='count(*):Q',
color='precipitation'
)
And here is what the histogram looks like:

Conclusion
I hope you are now clear with what Altair is and how to plot histograms using the same in the Python programming language.
Thank you for reading!