When to reach for a line chart
A line chart is the go-to shape for change over an ordered axis — almost always time (days, months, years). The line itself draws attention to trend: is the metric rising, falling, or flat? Markers on top of the line let you also read off exact points without losing the trend shape.
Basic interactive line
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"date": pd.date_range("2025-01-01", periods=7, freq="D"),
"orders": [120, 140, 130, 160, 155, 170, 180],
})
fig = px.line(df, x="date", y="orders", markers=True, title="Daily orders")
fig.show()Because this is Plotly (not Matplotlib), the rendered chart already lets you hover each marker for its exact date/value, drag to zoom into a date range, and double-click to reset.
Multiple lines (color)
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"date": list(pd.date_range("2025-01-01", periods=5, freq="D")) * 2,
"orders": [120, 140, 130, 160, 155, 100, 110, 120, 125, 140],
"product": ["A"] * 5 + ["B"] * 5,
})
fig = px.line(df, x="date", y="orders", color="product", markers=True, title="Orders by product")
fig.show()Passing color="product" does two things at once: it splits the data into one line per group, and it adds a legend you can click to hide/show a series.
Tips
- Use
hover_data=[...]to show extra fields (e.g. region, channel) in the tooltip. - Use
fig.update_layout(...)for axis labels, margins, and legend position. - Sort your DataFrame by the x-axis column first — Plotly draws points in row order, so an unsorted date column produces a zig-zag line.
flowchart TD A["Is the x-axis ordered
and continuous (time, sequence)?"] -->|"Yes"| B["Does trend/direction
matter most?"] A -->|"No, unordered categories"| C["Use a bar chart instead"] B -->|"Yes"| D["px.line(...)
add markers=True for exact points"] B -->|"No, comparing totals"| C
Check your understanding
What is a line chart the go-to choice for?
The line draws attention to trend across an ordered axis such as days or months.
Show answer
B — Change over an ordered axis, usually time — The line draws attention to trend across an ordered axis such as days or months.
What does color="product" do in px.line?
It creates one line per product and a legend that can hide or show each series.
Show answer
B — Splits into one line per group and adds a clickable legend — It creates one line per product and a legend that can hide or show each series.
What does markers=True add?
markers=True places a dot on every observed point so you see where the real data is.
Show answer
A — A marker on each data point — markers=True places a dot on every observed point so you see where the real data is.
Why only connect points that are genuinely ordered and continuous?
The eye reads the segment as continuity between observations, which misleads for unrelated categories.
Show answer
B — The connecting segment implies what probably happened in between — The eye reads the segment as continuity between observations, which misleads for unrelated categories.
Next
Continue to: Interactive Bar Charts for comparing categories.
🧪 Try It Yourself
Exercise 1 – Draw a Line Chart
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Show the Markers
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – One Line per Group
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.