Fundamentals 5 min read

Creating Beautiful Charts with Altair: Scatter, Line, Bar, Box, Area, and Pie Plots in Python

This article demonstrates how to use the Python Altair library to create a variety of visualizations—including scatter, line, bar, box, area, and pie charts—by loading sample datasets and applying concise, consistent API calls, while also highlighting customization options and interactive capabilities.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Creating Beautiful Charts with Altair: Scatter, Line, Bar, Box, Area, and Pie Plots in Python

Altair is a Python library for data visualization that follows the Vega-Lite grammar and offers a clean, consistent API for creating attractive charts.

1. Scatter Plot

import altair as alt
from vega_datasets import data
# Load dataset
cars = data.cars()
# Create scatter plot
scatter_plot = alt.Chart(cars).mark_circle().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    tooltip=['Name', 'Horsepower', 'Miles_per_Gallon']
).properties(
    width=500,
    height=400
)
scatter_plot

2. Line Chart

import altair as alt
from vega_datasets import data
# Load dataset
stocks = data.stocks()
# Create line chart
line_plot = alt.Chart(stocks).mark_line().encode(
    x='date:T',
    y='price:Q',
    color='symbol:N'
).properties(
    width=600,
    height=400
)
line_plot

3. Bar Chart

import altair as alt
from vega_datasets import data
# Load dataset
barley = data.barley()
# Create bar chart
bar_plot = alt.Chart(barley).mark_bar().encode(
    x='variety:N',
    y='mean(yield):Q',
    color='site:N'
).properties(
    width=500,
    height=400
)
bar_plot

4. Box Plot

import altair as alt
from vega_datasets import data
# Load dataset
iris = data.iris()
# Create box plot
box_plot = alt.Chart(iris).mark_boxplot().encode(
    x='species:N',
    y='petalWidth:Q'
).properties(
    width=400,
    height=300
)
box_plot

5. Area Chart

import altair as alt
from vega_datasets import data
# Load dataset
stocks = data.stocks()
# Create area chart
area_plot = alt.Chart(stocks).mark_area().encode(
    x='date:T',
    y='price:Q',
    color='symbol:N'
).properties(
    width=600,
    height=400
)
area_plot

6. Column Chart (Duplicate of Bar Chart)

import altair as alt
from vega_datasets import data
# Load dataset
barley = data.barley()
# Create column chart
bar_plot = alt.Chart(barley).mark_bar().encode(
    x='variety:N',
    y='mean(yield):Q',
    color='site:N'
).properties(
    width=500,
    height=400
)
bar_plot

7. Pie Chart

import altair as alt
from vega_datasets import data
# Load dataset
cars = data.cars()
# Compute counts per manufacturer
manufacturers = cars['Origin'].value_counts().reset_index()
manufacturers.columns = ['Origin', 'Count']
# Create pie chart
pie_chart = alt.Chart(manufacturers).mark_arc().encode(
    theta='Count:Q',
    color='Origin:N',
    tooltip=['Origin', 'Count']
).properties(
    width=400,
    height=400
)
pie_chart

These examples show the basic methods for creating area, column, and pie charts with Altair. You can adjust and modify them according to your data and requirements. Altair offers rich visualization options, allowing customization of styles, colors, labels, and supports interactive exploration and dynamic updates for flexible and engaging data visualizations.

上方商品有需要的话,感谢购买支持一下

给作者以更大的动力,发更多优秀的文章‍

点赞、关注、分享

TutorialData Visualizationchartsaltair
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.