Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 57 ยท Line Plots and Basic Styling

Colors, Line Styles and Markers

Control how lines look in Matplotlib: named colors, hex codes, RGB tuples, the C0-C9 cycle, line styles, markers, format strings, alpha and zorder.

Every line you draw has a color, a thickness, a dash pattern and, optionally, a marker on each data point. Matplotlib picks sensible defaults, but good charts usually need you to choose these on purpose: to tell five series apart, to make a forecast look tentative, or to match a brand palette.

This lesson covers the styling vocabulary you will reuse in every other chart type. It builds on line plots and uses the object-oriented fig, ax = plt.subplots() pattern from pyplot vs the object-oriented API.

Five knobs on every line

Color Name, hex, RGB or cycle slot.
Line style Solid, dashed, dotted.
Width linewidth in points.
Marker A symbol on each point.

Each one is a keyword argument of ax.plot.

1 / 2

Specifying colors

The color keyword (or its short form c) accepts several notations. They all end up as the same thing: four numbers for red, green, blue and alpha between 0 and 1.

Notation Example Notes
Named color "tomato", "steelblue" Over 140 CSS-style names
Single letter "r", "g", "b", "k" Quick but harsh: "k" is black
Hex string "#2a9d8f" Six digits, optionally eight with alpha
RGB(A) tuple (0.16, 0.62, 0.56) Floats from 0 to 1, not 0 to 255
Gray string "0.6" A string holding a float: light to dark
Cycle color "C0" to "C9" The nth color of the current cycle
Python Five ways to name a color

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 helper functions to_rgba and to_hex from matplotlib.colors convert between notations, which is handy when you need to compute a darker shade or store a color in a config file. Notice that "C0" resolves to the first color of the default cycle, a blue.

The default color cycle

When you call ax.plot repeatedly without a color, Matplotlib walks through ten default colors, C0 to C9, and then starts over. Using "C3" explicitly is useful when you want a second kind of element, such as a marker overlay, to match a line that got its color automatically. You can replace the whole cycle later in Styles, rcParams and Themes.

Line styles and widths

linestyle (short form ls) controls the dash pattern and linewidth (short form lw) the thickness in points. A point is 1/72 of an inch, so the default width of 1.5 is a thin, crisp line.

Name Short Looks like
"solid" "-" continuous
"dashed" "--" long dashes
"dotted" ":" dots
"dashdot" "-." dash, dot, dash
(0, (5, 2)) custom: 5 points on, 2 off

The tuple form is (offset, (on, off, on, off, ...)) and gives full control. Use it when the built-in dashes are too similar to each other.

Python Line styles and widths

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.

Dash patterns matter for accessibility. If a chart may be printed in black and white or read by someone with color-vision deficiency, vary the line style as well as the color so each series stays identifiable.

Markers

Markers draw a symbol at each data point. Pass marker= for the shape, markersize (or ms) for its size in points, and optional markerfacecolor, markeredgecolor and markeredgewidth for the details. Common shapes are "o" (circle), "s" (square), "^" (triangle up), "D" (diamond), "x" and "+" (unfilled crosses), and "." (a small dot).

A useful trick for dense data is markevery: markevery=5 puts a marker on every fifth point only, so the line stays readable while you still see where symbols repeat.

Python Markers, sizes and markevery

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.

Setting linestyle="none" (or "") hides the connecting line and leaves only the markers. For marker-only charts, though, scatter plots are usually a better tool because they let each point have its own size and color.

Format strings

Matplotlib borrows a compact notation from MATLAB: a single string that combines a color letter, a marker and a line style. "ro--" means red circles joined by a dashed line, and "g^:" means green triangles on a dotted line. The pieces can come in any order, and each is optional.

Python
ax.plot(x, y, "ro--")      # red, circle markers, dashed line
ax.plot(x, y2, "k-")       # black solid line
ax.plot(x, y3, "b.")       # blue dots only: a marker with no line style gives no line

Format strings are fine for quick exploration. For anything you will share, prefer the keyword arguments: they read like documentation, they support hex colors and any named color (a format string only takes the single letters b g r c m y k w and C0-style cycle slots), and you can change one setting without rewriting the rest. If you pass both, the keywords win.

Transparency and draw order

alpha sets opacity from 0 (invisible) to 1 (solid). It is the simplest cure for overplotting, where many shapes pile up on top of each other, and it lets you see where lines cross.

zorder decides which artist is drawn on top. Lines default to 2, patches (bars, shaded areas) to 1, and text to 3. Higher values draw later, so they appear in front. Use it when a shaded band hides a line or a reference line should sit behind your data.

Python Alpha and zorder together

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 faint blue lines show the overall spread, while one opaque red line stands out. This "context in the background, focus in the foreground" pattern is one of the most effective uses of styling.

Choosing styles wisely

Styling should carry meaning. Use color to separate categories, line style to separate kinds of series (observed versus forecast), width to show importance, and markers when the exact sample positions matter. Avoid using all of them at once: a chart in which every line has a unique color, dash, marker and width is harder to read than one with a clear highlight. See Designing Clear Charts for more guidance.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which value is a valid RGB tuple for a pure red line in Matplotlib?

    RGB tuples use floats between 0 and 1. Values like 255 are out of range and raise an error.
  2. 2. What does the format string "ro--" mean?

    r is red, o is a circle marker and -- is the dashed line style.
  3. 3. A thick red line is hidden behind a semi-transparent shaded band. What is the cleanest fix?

    zorder controls draw order: artists with a higher value are drawn later and appear in front.
  4. 4. What does color="C2" select?

    The cycle is zero-based, so C0 is the first color and C2 is the third.

Summary

  • Colors can be names, hex strings, RGB(A) tuples with floats from 0 to 1, gray strings such as "0.5", or cycle slots "C0" to "C9".
  • linestyle, linewidth, marker, markersize and markevery shape the line and its points; tuples like (0, (5, 2)) make custom dashes.
  • Format strings such as "ro--" are a handy shortcut, but keyword arguments are clearer and more capable.
  • alpha makes shapes transparent and zorder controls what is drawn in front.
  • Vary line style as well as color so charts survive black-and-white printing and color-vision differences.

Next, learn how to color whole surfaces of data in Colormaps and Color Normalization.