Open In App

Scatter Plot with Regression Line using Altair in Python

Prerequisite: Altair

In this article, we are going to discuss how to plot to scatter plots with a regression line using the Altair library.



Scatter Plot and Regression Line

Installation:



To install the Altair library, write the below command on your command prompt.

pip install altair

In this article for datasets, we are using the vega_datasets package, to install write the below command on your command prompt.

pip install vega_datasets

Approach:

Example 1: Default Scatter plot with Regression line on airports dataset.




# importing libraries
import altair as alt
from vega_datasets import data
  
# importing airports dataset from 
# vega_datasets package
airport = data.airports()
  
# making the scatter plot on latitude and longitude
fig = alt.Chart(airport).mark_point().encode(x='latitude',y='longitude')
  
# making the regression line using transform_regression 
# function and add with the scatter plot
final_plot = fig + fig.transform_regression('latitude','longitude').mark_line()
  
# saving the scatter plot with regression line
final_plot.save('output1.html')

Output:

Example 2: Scatter Plot with Regression Line by setting up the color using airport dataset.




# importing libraries
import altair as alt
from vega_datasets import data
  
# importing airports dataset from vega_datasets package
airport = data.airports()
  
# making the scatter plot on latitude and longitude
# setting color on the basis of country
fig = alt.Chart(airport).mark_point().encode(
  x='latitude',y='longitude',color='country')
  
# making the regression line using transform_regression
# function and add with the scatter plot
final_plot = fig + fig.transform_regression('latitude','longitude').mark_line()
  
# saving the scatter plot with regression line
final_plot.save('output2.html')

Output:

Example 3: Default Scatter Plot with Regression Line using seattle_weather dataset.




# importing libraries
import altair as alt
from vega_datasets import data
  
# importing weather dataset from vega_datasets package
weather_data = data.seattle_weather()
  
# making the scatter plot on temp_max and temp_min
fig = alt.Chart(weather_data).mark_point().encode(x='temp_max',y='temp_min')
  
# making the regression line using transform_regression
# function and add with the scatter plot
final_plot = fig + fig.transform_regression('temp_max','temp_min').mark_line()
  
# saving the scatter plot with regression line
final_plot.save('output3.html')

Output:

Example 4: Scatter Plot with Regression Line by setting up color using seattle_weather dataset.




# importing libraries
import altair as alt
from vega_datasets import data
  
# importing weather dataset from vega_datasets package
weather_data = data.seattle_weather()
  
# making the scatter on temp_max and temp_min
fig = alt.Chart(weather_data).mark_point().encode(
  x='temp_max',y='temp_min',color='weather')
  
# making the regression line using transform_regression
# function and add with the scatter plot
final_plot = fig + fig.transform_regression('temp_max','temp_min').mark_line()
  
# saving the scatter plot with regression line
final_plot.save('output4.html')

Output:


Article Tags :