website/examples/gallery/line-4.py

import plotly.graph_objects as go
import plotly.io as pio

INK = "#1C1917"
MUTED = "#57534E"
WARM_EDITORIAL_PALETTE = [
    "#0F766E",
    "#C2410C",
    "#1D4ED8",
    "#A16207",
    "#7E22CE",
    "#475569",
    "#0369A1",
    "#1C1917",
]
CANVAS = "#FFF7ED"
RULE = "#E7DDD2"

pio.templates["warm_editorial"] = go.layout.Template(
    layout=go.Layout(
        colorway=WARM_EDITORIAL_PALETTE,
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "size": 13, "color": INK},
        title={
            "x": 0,
            "xanchor": "left",
            "font": {"family": "Georgia, 'Times New Roman', serif", "size": 18, "color": INK},
        },
        paper_bgcolor=CANVAS,
        plot_bgcolor=CANVAS,
        xaxis={
            "showgrid": False,
            "showline": True,
            "linecolor": "#A8A29E",
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "automargin": True,
        },
        yaxis={
            "showgrid": True,
            "gridcolor": RULE,
            "gridwidth": 1,
            "showline": False,
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "zeroline": False,
            "automargin": True,
        },
    )
)


def load_data():
    return [("North trail", 2, 6), ("South trail", 4, 20)]


def make_before(data):
    fig = go.Figure()
    for name, start, end in data:
        fig.add_scatter(
            x=[str(start), str(end)],
            y=[1, 1],
            mode="lines+markers",
            name=name,
            line={"color": "#968D81", "width": 4},
            marker={"symbol": ["circle", "x"], "size": 8},
        )
    fig.update_layout(
        template="warm_editorial",
        title="JULY TRAIL CLOSURES",
        plot_bgcolor="#BEB5A9",
        xaxis_title="Day in July",
        xaxis={
            "type": "category",
            "categoryorder": "array",
            "categoryarray": ["2", "4", "6", "20"],
        },
        yaxis={"visible": False},
        legend={"orientation": "h"},
    )
    return fig


def make_after(data):
    fig = go.Figure()
    for name, start, end in data:
        fig.add_scatter(
            x=[start, end],
            y=[name, name],
            mode="lines+markers",
            name=name,
            line={"color": WARM_EDITORIAL_PALETTE[0], "width": 3},
            marker={"size": 9},
        )
    fig.update_layout(
        template="warm_editorial",
        title="South trail stayed closed through July 20",
        xaxis={"title": "Day in July", "range": [1, 21], "dtick": 2},
        yaxis={"showgrid": False},
        showlegend=False,
    )
    return fig


EXAMPLE = {
    "kind": "line",
    "source": "Illustrative maintenance closure log for two reservoir trails in July.",
    "before": {"summary": "The two closures began in the first week of July."},
    "after": {
        "summary": "North trail closed July 2 through 6 and South trail closed July 4 through 20."
    },
    "principles": {
        "foreground-contrast": {
            "question": "Where are the closure endpoints?",
            "book": {
                "quote": "background requires darker and more saturated foreground colors",
                "page": 283,
            },
            "before": {"caption": "Gray endpoints have little contrast against the gray plot."},
            "after": {"caption": "Dark teal endpoints stand out against pale paper."},
        },
        "legible-overlapping-layers": {
            "question": "When were both trails closed?",
            "book": {"quote": "to make the filled areas partially transparent", "page": 299},
            "before": {
                "caption": "The two closure lines share a row and overlap between days 4 and 6."
            },
            "after": {"caption": "Separate labeled rows keep both intervals visible."},
        },
        "type-case": {
            "question": "What does the headline say?",
            "book": {"quote": "type is upper-and-lower case", "page": 183, "line": 7994},
            "before": {"caption": "The entire heading uses capital letters."},
            "after": {"caption": "A sentence-case heading identifies the longer closure."},
        },
        "scale-discontinuities": {
            "question": "How much time passed between closure events?",
            "book": {"quote": "Full scale breaks should be used only when necessary.", "page": 104},
            "before": {"caption": "Days 6 and 20 sit one category apart, just like days 2 and 4."},
            "after": {"caption": "Equal horizontal distances represent equal numbers of days."},
        },
        "time-interval-encoding": {
            "question": "How long did each closure last?",
            "book": {
                "quote": "undefined parts and different granularities are indicated visually",
                "page": 260,
            },
            "before": {
                "caption": "Categorical spacing makes the two segments appear equally long."
            },
            "after": {
                "caption": "Each segment spans the actual closure and reopening dates on a continuous scale."
            },
        },
    },
}