website/examples/gallery/line-1.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 [(1, 10), (2, 12), (3, 15), (4, 20), (5, 18), (6, 16)]


def intake_temperatures(data):
    days, temperatures = zip(*data, strict=True)
    fig = go.Figure(go.Scatter(x=days, y=temperatures, mode="lines+markers"))
    fig.update_layout(
        template="warm_editorial",
        title="Reservoir intake temperature",
        xaxis_title="Day of monitoring",
        yaxis_range=[8, 22],
        showlegend=False,
    )
    return fig


def make_before(data):
    fig = intake_temperatures(data)
    fig.update_xaxes(
        tickvals=[1, 2, 3, 4, 5, 6],
        autorange="reversed",
        ticks="inside",
        ticklen=12,
        showgrid=True,
        gridcolor="#888888",
    )
    fig.update_yaxes(
        title="water_temp",
        tickvals=list(range(8, 23)),
        ticks="inside",
        ticklen=12,
        showgrid=True,
        gridcolor="#888888",
    )
    for position, label in ((10, 50), (15, 64), (20, 77)):
        fig.add_annotation(
            x=1.02, xref="paper", y=position, text=f"{label} °F", xanchor="left", showarrow=False
        )
    fig.update_layout(margin={"r": 82})
    return fig


def make_after(data):
    fig = intake_temperatures(data)
    fig.update_xaxes(tickvals=[1, 4, 6], ticks="outside")
    fig.update_yaxes(title="Water temperature (°C)", tickvals=[10, 16, 20], ticks="outside")
    for value in (10, 16, 20):
        fig.add_annotation(
            x=1.02,
            xref="paper",
            y=value,
            text=f"{32 + value * 1.8:g} °F",
            xanchor="left",
            showarrow=False,
        )
    fig.update_layout(margin={"r": 82})
    return fig


EXAMPLE = {
    "kind": "line",
    "source": "Illustrative daily readings at a reservoir intake.",
    "before": {"summary": "The water warms through day four and then cools."},
    "after": {"summary": "The highest intake temperature was 20 °C, or 68 °F, on day four."},
    "principles": {
        "axis-reading-conventions": {
            "question": "When did the water reach its peak?",
            "book": {"quote": "the tick marks are outside of the rectangle", "page": 16},
            "before": {"caption": "Days run backward and long ticks point into the plot."},
            "after": {"caption": "Days increase from left to right and ticks point outward."},
        },
        "grid": {
            "question": "Where does the warming trend reverse?",
            "book": {"quote": "the grid should usually be muted", "page": 114, "line": 4531},
            "before": {
                "caption": "Dark horizontal and vertical rules compete with the temperature trace."
            },
            "after": {"caption": "Faint horizontal guides leave the rise and fall visible."},
        },
        "multifunction": {
            "question": "Which days mark the start, peak and end?",
            "book": {"quote": "Mobilize every graphical element", "page": 140, "line": 5258},
            "before": {
                "caption": "A tick at every degree gives unused levels the same emphasis as observations."
            },
            "after": {"caption": "Selected ticks identify the first, highest and final readings."},
        },
        "words-spelled-out": {
            "question": "What quantity does the vertical axis measure?",
            "book": {"quote": "words are spelled out", "page": 183, "line": 7966},
            "before": {
                "caption": "The field name water_temp leaves the measurement unit unstated."
            },
            "after": {"caption": "The axis names water temperature and gives its Celsius unit."},
        },
        "secondary-unit-axis": {
            "question": "What is the peak in Fahrenheit?",
            "book": {"quote": "to use the pair of scale lines for a variable to show", "page": 83},
            "before": {"caption": "The right-hand scale incorrectly pairs 20 °C with 77 °F."},
            "after": {"caption": "A Fahrenheit scale at the right aligns 68 °F with 20 °C."},
        },
    },
}