Introduction to Altair: A Declarative Python Library for Data Visualization
This article introduces Altair, a simple yet powerful Python library built on the Vega‑Lite JSON specification, explains its declarative syntax, showcases basic and advanced visualizations with code examples, and highlights its advantages for efficient data analysis and presentation.
Altair is a simple, friendly Python library for statistical data visualization that builds on the powerful Vega‑Lite JSON specification, allowing users to generate attractive visualizations with minimal code.
Altair is a statistical visualization library with over 3000 stars on GitHub; it lets users focus on understanding data rather than low‑level chart construction, using a declarative language that describes visual appearance and interaction in JSON.
Key advantages include support for aggregation, data transformation, interaction, and composition; charts can be displayed in Jupyter Notebook, JupyterLab, or nteract, and exported as PNG, SVG, or standalone HTML, or viewed in the online Vega‑Lite editor.
Altair works best with tidy data, typically loaded as a Pandas DataFrame. Example code demonstrates importing Altair and Pandas, reading an Excel file, and creating a chart:
<code>import altair as alt
import pandas as pd
data = pd.read_excel("Index_Chart_Altair.xlsx", sheet_name="Sales", parse_dates=["Year"])
alt.Chart(data)
</code>A simple bar chart can be created by mapping a quantitative variable to the x‑axis and a nominal variable to the y‑axis using mark_bar() :
<code>chart = alt.Chart(df).mark_bar().encode(x="profit:Q", y="product:N")
</code>Altair supports various variable type combinations such as nominal + quantitative, temporal + quantitative, temporal + nominal, and quantitative + quantitative, enabling flexible chart designs.
For more complex visualizations, an area chart with faceting demonstrates Seattle’s monthly precipitation from 2012 to 2015. The code uses mark_area() , aggregates with mean() , and facets by year:
<code>chart = alt.Chart(df).mark_area(color="lightblue", interpolate="step", line=True, opacity=0.8)
.encode(
alt.X("month(date):T", axis=alt.Axis(format="%b", formatType="time", labelAngle=-15, labelBaseline="top", labelPadding=5, title="month")),
y="mean(precipitation):Q",
facet=alt.Facet("year(date):Q", columns=4,
header=alt.Header(labelColor="red", labelFontSize=15,
title="Seattle Monthly Precipitation from 2012 to 2015",
titleFont="Calibri", titleFontSize=25, titlePadding=15))
)
</code>The Header wrapper customizes facet labels and titles with parameters such as labelColor , labelFontSize , title , titleFont , titleFontSize , and titlePadding .
Images illustrating the bar chart, area chart, and faceted precipitation charts are included throughout the article.
Disclaimer: This content is compiled from the web; copyright belongs to the original author. Contact us for removal or licensing requests.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.