Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 42 ยท Basic Charts

Line Charts in Plotly

Draw line charts in Plotly with px.line and go.Scatter: multiple lines, markers, dash styles, line shapes, gaps, hover labels and tips for time series and wide data.

A line chart connects points in order and shows how a value changes, most often over time. It is the first chart most people learn, and in Plotly it also teaches the most important trace of all: the scatter trace, which draws lines, markers or both, depending on its mode.

In this lesson you build line charts with Express (px.line) and graph objects (go.Scatter), draw many lines from one table, style lines and markers, and use line shapes such as steps and smooth curves.

Your first line chart

With Express, give px.line a DataFrame and the x and y columns:

Python px.line with a color per country

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that color="country" produced four traces. The markers=True argument changed the trace mode from lines to lines+markers. Each country has 12 points, one for each census year from 1952 to 2007.

The same chart with graph objects

go.Scatter draws lines when mode="lines". Here the chart is drawn in the browser with plotly.js, using the same trace properties you would pass to go.Scatter:

Lines, markers and dash styles

The four traces use the four line shapes you will use most: linear (default), spline (smoothed), and the step shapes hv, vh, hvh, vhv. Steps are the honest choice for values that hold until they change, such as a price list or a thermostat setting.

Line properties

All the styling lives in the line and marker groups of a trace:

Property Values Effect
line.color any CSS color Line color
line.width number (pixels) Thickness
line.dash solid, dot, dash, longdash, dashdot, longdashdot Dash pattern
line.shape linear, spline, hv, vh, hvh, vhv How points are connected
line.smoothing 0 to 1.3 Curve tightness for spline
mode lines, markers, lines+markers, text, combinations What to draw
marker.size, marker.symbol number, circle, square, diamond, x, ... Point style
connectgaps True / False Join across missing values

In Express the same options appear as arguments: line_shape="spline", line_dash="species" (a column!), markers=True, symbol="country", and line_dash_sequence.

Python Style lines with graph objects

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Several lines from wide data

A DataFrame with one column per series, such as px.data.stocks(), works as wide data. Pass the column names to y:

Python Wide data: one line per column

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dates in px.data.stocks() are strings in YYYY-MM-DD format, which plotly.js recognizes as dates, so the axis shows month ticks and supports date zoom.

Time series tips

  • Sort by x first. A line connects points in the order they appear in the data. Unsorted data makes a tangled scribble, so call df.sort_values("date").
  • Several observations per group: use line_group="id" to draw separate lines with the same color, for example one line per user.
  • Range slider and selectors make long time series easy to explore (covered in the axes lesson).
  • Too many points? Use go.Scattergl or aggregate with resample.
  • Hover: hovermode="x unified" shows all values at the hovered x in one box, which is easiest for comparing lines.

Common mistakes

  • Writing px.line(df, x="a", y="b") where the data repeats x values, producing a zig-zag line. Use color or line_group to separate the series.
  • Forgetting markers=True when there are only a few points and the line alone hides them.
  • Smoothing (spline) data where the smooth curve implies values that never existed. Use it for decoration, not analysis.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which trace type does px.line create?

    There is no line trace type. Lines are scatter traces drawn with mode="lines".
  2. 2. How do you draw a stepped line, where the value holds until the next point?

    The step shapes are hv, vh, hvh and vhv, set through the line shape.
  3. 3. A line chart looks like a tangled scribble. What is the most likely cause?

    Plotly joins points in data order, so rows must be sorted along the x axis.
  4. 4. What does mode="lines+markers" draw?

    Modes can be combined with a plus sign.
  5. 5. Which Express call draws one line per column GOOG, AAPL, MSFT of a wide DataFrame?

    A list of columns in y creates one trace per column.

Summary

  • Line charts are scatter traces with mode="lines"; px.line makes them from a DataFrame.
  • Use color or line_group for several lines, or a list of columns in y for wide data.
  • Style with line.color, width, dash, shape and with marker properties.
  • Sort by x, use real dates, and pick hovermode="x unified" for comparisons.

Next: Scatter and Bubble Charts