The Altair library is used for declarative statistical visualization in Python. It’s designed to simplify the creation of interactive and informative visualizations for data analysis. Altair is built on top of the Vega and Vega-Lite visualization grammars, which are frameworks for creating visualizations using a high-level, declarative syntax. There are two types of visualization libraries:
Imperative libraries: They focus on how to build a visualization, such as specifying the steps manually to build the visualization (axis, size, legend, labels). Matplotlib is an example of an imperative library.
Declarative libraries: They focus on what we want to see. We specify the data and the type of visualization we want to see. The library will do the manipulations to create the visualization for us automatically. Altair is an example of a declarative library.
Every Altair chart comprises three main elements: data, chart, mark, and encodings.
The dataset is a fundamental element in Altair charts. We need a dataset to visualize. We can use pandas or other data manipulation libraries to load our data.
import pandas as pd# Load your data into a pandas DataFramedata = pd.read_csv('your_data.csv')
A chart is the entry point element in Altair. Every Altair chart receives an input of a single argument, which is the dataset.
To start creating a chart, we can import the Altair library and then create the chart.
import altair as altalt.Chart(dataset)
The dataset can be in one of the following formats:
Data or related object (i.e., UrlData, InlineData, NamedData)
URL pointing to a JSON or CSV file
Object supporting the geo_interface
(e.g., Geopandas GeoDataFrame, and so on)
A mark is a chart property that defines how to represent data. Examples of marks include bar charts, line charts, area charts, and many more. To specify a mark, we append it to the chart.
alt.Chart(dataset).mark_bar()
The above example shows how to use Altair to draw a bar chart. In general, the name of each mark is mark_<type_of_graph>()
.
Encodings specify where to represent data, including their position, size, color, etc. To define an encoding, we append the encode()
property to the chart.
alt.Chart(dataset).mark_bar().encode()
Let’s consider the Christmas trees dataset.
Year | RealTree | FakeTree |
2004 | 27100000 | 9000000 |
... | ... | ... |
2016 | 27400000 | 18600000 |
Let’s draw a simple line chart in Altair.
import altair as altimport pandas as pdimport osdf = pd.read_csv('/christmas_trees.csv')chart = alt.Chart(df).mark_line().encode(x = 'Year:O', # O for ordinal datay = 'RealTree:Q' # Q for quantitative data)chart.save('chart.html')os.system('cat chart.html')
We load the dataset as a pandas
DataFrame and then draw a simple line chart in altair
of the RealTree
column versus the Year
column.
We use the mark_line()
property to draw a line and specify the x
and y
axes in the encode()
property. For each column, we must also select the type (O
for Year
and Q
for RealTree
). We use the last two statements of the code to render the chart.
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