Plotting Scatter Plots With Altair in Python

Filed Under: Python Modules
Altair in Python

scatter plot (scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot which makes use of the Cartesian coordinates to display values typically two variables for a dataset. In this tutorial, we will understand how to plot scatter plots using the Altair library in Python.

Also read: Python Altair tutorial: Creating Interactive Visualizations

Code Implementation of Altair Scatter Plots

Altair is a statistical visualization library in Python. It is declarative in nature and is based on Vega and Vega-Lite visualizations. We’ll use this library to plot our scatter plots now.

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

In this tutorial, we will be making use of the vega_datasets which is a Python library that gives access to over 60 datasets of varying sizes. We will be using the weather data set from Seattle using the code below.

seattle_weather_data = data.seattle_weather()
print(seattle_weather_data.head())
Vega Datasets Weather Data
Vega Datasets Weather Data

Create a simple Scatter Plot

In this tutorial, we want to build a scatter chart using the mark_point function. With the help of encode function, we can decide the variable we want to consider.

alt.Chart(seattle_weather_data).mark_bar().encode(
    alt.X("wind:Q",
          bin=alt.BinParams()),
    y='count(*):Q'
)
Altair Scatter Plot Chart 1
Altair Scatter Plot Chart 1

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'
)
Altair ScatterPlot Chart Weather
Altair ScatterPlot Chart Weather
alt.Chart(seattle_weather_data).mark_bar().encode(
    alt.X("wind:Q",
          bin=alt.BinParams()),
    y='count(*):Q',
    color='precipitation'
)
Altair ScatterPlot Chart Precipitation
Altair ScatterPlot Chart Precipitation

Conclusion

I hope you are now clear with what Altair is and how to plot scatter plots using the same in the Python programming language. There are many more features of the library in terms of interactivity components.

Thank you for reading!

close
Generic selectors
Exact matches only
Search in title
Search in content