Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

Interactive Line Charts

Build interactive time-series and trend charts, add markers, and format axes in Plotly.

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

Python
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)

Python
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.

sketch A line chart, traced point by point p5.js
The line connects ordered points on an x-axis; the traveling dot mimics hovering along the 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.
diagram Choosing a line chart mermaid
Line charts fit ordered, continuous axes where trend matters more than exact category comparison.

Check your understanding

quizCheck yourself
  1. What is a line chart the go-to choice for?

    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.

  2. What does color="product" do in px.line?

    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.

  3. What does markers=True add?

    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.

  4. Why only connect points that are genuinely ordered and continuous?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Show the Markers

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – One Line per Group

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did