Altair is a Python library for statistical visualization. It provides a declarative interface, allowing users to easily create interactive and static visualizations. It supports various plot types, integrates seamlessly with pandas DataFrames, and offers extensive customization options for creating publication-quality graphics. Altair is widely used for exploratory data analysis, data storytelling, and communicating insights in data science and analytics projects.
A box plot is a graphical representation of the distribution of a continuous variable through its quartiles. It consists of a box representing the interquartile range (IQR) of the data, with a line inside representing the median. Additionally, it often includes whiskers extending from the box, representing the data range and excluding outliers. Box plots are useful for visually summarising the spread, central tendency, and skewness of the data and identifying potential outliers.
We can use the mark_boxplot()
function to draw a box plot in Altair. Here’s a basic example:
import altair as altimport pandas as pdimport os# Sample datadata = pd.DataFrame({'category': ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C'],'value': [1, 2, 3, 4, 5, 6, 7, 8, 9]})# Create box plot with customized propertiesboxplot = alt.Chart(data).mark_boxplot(color='skyblue', # Set the color of the boxes to sky bluesize=20, # Set the size of the boxes to 20opacity=0.7 # Set the opacity of the boxes to 0.7).encode(x='category:O',y='value:Q')# Display the chartboxplot.save('chart.html')os.system('cat chart.html')
Lines 1–3: We import Altair and other necessary libraries.
Lines 5–9: We create a pandas DataFrame named data
with category
and value
columns.
Lines 11–19: We initialize an Altair chart with data. We specify that it’s a box plot (boxplot
). We set the color
of the boxes in the box plot to skyblue
, size
to 20
, and opacity
to 0.7
. We map category
to the x-axis and value
to the y-axis. 'O'
and 'Q'
represent ordinal and quantitative data types respectively.
Line 22: We save the chart using boxplot.save('chart.html')
. It exports the chart to an HTML file named chart.html
.
Line 23: 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