5

Plotting Scatter Plots With Altair in Python

 2 years ago
source link: https://www.journaldev.com/59301/scatter-plots-with-altair-in-python
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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 DataVega 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 1Altair 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 WeatherAltair 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 PrecipitationAltair 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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK