How to Color Different Segments of a TikZ Edge
At a glance
- 1. Split the path
Treat each differently colored segment as its own drawing operation.
- 2. Set each color
Put the color option on the command or edge operation that should use it.
- 3. Check the joins
Use matching widths and, if needed, rounded line caps to make adjoining segments look continuous.
TikZ applies drawing styles to paths and edges. If you want different parts of a line to have different colors, give each segment its own drawing operation and set that operation’s color.
This works for a simple polyline as well as for edges in a graph. The key is to make the color change explicit where the segment is drawn.
Draw each segment separately
A single command such as \draw (A) -- (B) -- (C); describes one continuous path. For a path with several colors, split it at the points where the color should change:
\documentclass[tikz,border=4pt]{standalone}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}[line width=2pt, line cap=round]
\coordinate (A) at (0,0);
\coordinate (B) at (2,0);
\coordinate (C) at (2,1.5);
\draw[red] (A) -- (B);
\draw[blue] (B) -- (C);
\fill (A) circle (2pt);
\fill (B) circle (2pt);
\fill (C) circle (2pt);
\node[below] at (A) {A};
\node[below] at (B) {B};
\node[right] at (C) {C};
\end{tikzpicture}
\end{document}
The first \draw command colors the segment from A to B red; the second colors B to C blue. Both commands use the same coordinates at B, so the segments meet at the intended point. The line cap=round option gives the ends a rounded appearance and can help hide small rasterization gaps in the rendered image. Remove it if you prefer square ends.
You can set other styles independently for each segment, too:
\draw[red, very thick, dashed] (A) -- (B);
\draw[blue, thick, densely dotted] (B) -- (C);
Set styles on graph edges
If you are using TikZ’s edge syntax, put the color option on each edge operation. For example:
\path (A) edge[draw=red, thick] (B)
(B) edge[draw=blue, thick] (C);
Each edge receives its own style, so the two edges can have different colors even though they appear in the same \path command. Use draw=red or draw=blue to make clear that the option controls the edge stroke. If the edge also has an arrow, its color will normally follow the edge’s drawing color.
You can also write the graph edges as separate commands, which is often easier to read when the styles or labels differ:
\draw[->, red] (A) -- (B);
\draw[->, blue] (B) -- (C);
Why not color the whole path?
An option such as \draw[red] ... applies a drawing style to that draw operation. It does not automatically assign a new color to each straight section of a connected path. To control colors predictably, divide the path into separately styled segments, or use separate edge operations with their own options.
This approach is also useful when the segments have labels: place a node on the segment whose label you want to style, for example node[midway, above, black] {label}. The node’s text color can be set independently from the edge’s stroke color.
Common mistakes
- Setting the color only once:
\draw[red] (A) -- (B) -- (C);draws the connected path in one color. Split it into separately styled commands if the colors should differ. - Putting the option in the wrong place: For a standalone segment, use
\draw[blue] (B) -- (C);. For graph syntax, use an option on the particularedge, such asedge[draw=blue] (C). - Drawing endpoints that do not quite match: If one segment ends at
(2,0)and the next starts at(2.01,0), a visible gap can appear. Reuse a named coordinate or the exact same coordinate expression. - Confusing line and label colors: The edge’s
drawstyle controls its stroke. Set a node’stextcolor separately if its label should differ.
Separate drawing operations give you direct control over color, width, dash pattern, arrows, and labels for every segment.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.