liveBook · Manning

Rectangle

As explained in Data Storytelling with Altair and AI

A rectangle is a four-sided graphical shape used as a visual mark to cover a region of a chart.

A rectangle is a four-sided graphical mark that covers a chart region, making a time period, option, or other visual unit easier to notice. section 7.5.3 section 9.2.1

Use a rectangle when the chart needs visual context rather than data alone. A shaded region can identify an important interval, while labeled rectangles can organize possible next steps. section 7.5.3 section 9.2.1

Build rectangles by defining their bounds and then encoding those bounds in Altair.

Highlighting a decline

A light-gray, semi-transparent rectangle spans 1992–1998 to mark the salmon-sales decline. A text annotation explains that fish health issues partially caused the decrease, turning a visual pattern into contextual knowledge. section 7.5.3

Mapping next steps

Three parametrically positioned rectangles represent Online Campaign, Influencers Engagement, and Social Media Promotion. Labels are placed inside the shapes, and connecting lines turn the rectangles into a roadmap. section 9.2.1

Listing 7.17 The rectangle
N = 100000000           #1
y = df['AMOUNT'].max() + N

rect_df = pd.DataFrame({'x': [1992], 
            'x2': [1998],
            'y' : [0],
            'y2': [y]
        })

rect = alt.Chart(rect_df).mark_rect(
    color='lightgrey',
    opacity=0.5
).encode(
    x='x:O',
    x2='x2:O',
    y= 'y:Q',
    y2= 'y2:Q'
)

Inspect a minimal Altair implementation that creates rectangle bounds in pandas and renders a shaded chart region with mark_rect(). section 7.5.3

Listing 9.1 Setting the rectangles’ positions
import pandas as pd

width = 10
space = 5
N = 3
x = [i*(width+space) for i in range(N)]
y = [0 for i in range(N)]
x2 = [(i+1)*width+i*space for i in range(N)]
y2 = [10 for i in range(N)]
text = ['Online Campaign', 'Influencers Engagement', 'Social Media Promotion']

df_rect = pd.DataFrame(
    {   'x': x,
        'y': y,
        'x2': x2,
        'y2': y2,
        'text' : text
    }
)

Inspect a reusable pattern for generating multiple rectangles from width, spacing, count, coordinates, and labels. section 9.2.1

How Data Storytelling with Altair and AI covers it

How deep it goes
Builds rectangles from coordinate DataFrames, encodes their bounds, and extends the pattern to labeled, connected sets.
Examples
Uses a salmon-sales decline and a dogs-and-cats campaign roadmap as concrete rectangle examples.
What you can do after
Builds contextual highlights and labeled roadmaps with Python, pandas, and Altair.
How it connects
Connects rectangles with annotations, text labels, lines, charts, and data storytelling.

Also explained in

Compare how 3 books explain Rectangle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Storytelling with Altair and AI. The section links point to the parts of the book it draws on.