Altair is a Python library for declarative data visualization. It provides a high-level interface for creating interactive and informative visualizations from data. Altair is built on top of Vega and Vega-Lite, which are visualization grammars designed to make it easier to generate visualizations in a consistent and concise manner.
In Altair, a scatter plot is a form of visualization depicting individual data points as markers on a 2D plane, where one variable is associated with the x-axis and another variable with the y-axis. Each marker denotes a distinct data point, and its position reflects the values of the respective variables.
Here's a basic example of how we might create a scatter plot in Altair:
Importing Altair: We import the Altair library in our Python code. We need a dataset to visualize. We can use pandas or other data manipulation libraries to load our data.
import altair as altimport pandas as pd# Load your data into a Pandas DataFramedata = pd.read_csv('your_data.csv')
Creating an Altair chart: We use the alt.Chart
function to create the base chart object. We pass our data to this function.
chart = alt.Chart(data)
Creating scatter plot: We map our x
to the x-axis and our y
to the y-axis. We can also customize the line style, color, and other visual attributes.
# Create a scatter plotscatter_plot = chart.mark_point().encode(x='x',y='y')
Customizing the chart: We can add various customizations to our scatter plots, such as axis labels, titles, tooltips, and color schemes.
scatter_plot = scatter_plot.properties(title='Scatter Plot',).encode(tooltip=['x', 'y'],)
Let’s create a basic scatter plot in Altair using sample data.
import altair as altimport pandas as pdimport os# Sample datadata = pd.DataFrame({'x': [2, 3, 6, 8, 10],'y': [3, 6, 5, 11, 8]})# Create a scatter plotscatter_plot = alt.Chart(data).mark_point().encode(x='x',y='y').properties(title='Scatter Plot',).encode(tooltip=['x', 'y'],)scatter_plot.save('chart.html')os.system('cat chart.html')
Lines 1–3: We import Altair and the necessary libraries.
Lines 5–9: We create a pandas DataFrame named data
with x
and y
columns.
Lines 11–19: We initialize an Altair chart with data. We specify that it’s a scatter plot (mark_point
) and maps x
to the x-axis and y
to the y-axis. We set the chart title
and add tooltips
.
Line 21: We save the chart using chart.save('chart.html')
. It exports the chart to an HTML file named chart.html
.
Line 22: We display the chart on the console.
Unlock your potential: Data visualization with the Altair series, all in one place!
To continue your exploration of data visualization using the Altair library, check out our series of Answers below:
Data visualization using the Python Altair library
Get an introduction to Altair, its purpose, installation, and basic usage for data visualization.
What are the main elements of an Altair chart?
Learn about the key components that make up an Altair chart and how they contribute to creating meaningful visualizations.
How to implement encoding in Altair
Understand how encoding is used to map data to visual properties.
How to draw a line chart in Altair
Discover how to create a simple yet effective line chart using Altair.
How to draw a bar chart in Altair
Understand how to create bar charts in Altair for comparing categories and visualizing data values.
How to draw a scatter plot in Altair
Explore the process of creating scatter plots in Altair to visualize relationships between variables.
How to draw a box plot in Altair
Learn how to create box plots in Altair for displaying the distribution of data through quartiles.
How to draw a heatmap in Altair
Discover how to create heatmaps in Altair to represent data intensity and patterns using color coding.
How to draw a stacked area chart in Altair
Understand how to create stacked area charts in Altair to visualize cumulative data over time.
How to draw a geographical map in Altair
Learn how to visualize geographical data and create interactive maps using Altair’s geospatial capabilities.
How to draw a pie chart in Altair
Discover how to create pie charts in Altair, ideal for visualizing proportions of a whole.
Free Resources