Tx · LaTeX & TikZ

How to Use the Same Color with Different Bar Patterns in PGFPlots

Published By the 9to5Science editorial team

At a glance

  1. 1. Load patterns

    Add \usetikzlibrary{patterns} to make TikZ’s built-in hatch patterns available.

  2. 2. Set each plot’s style

    Give every bar series the same pattern color, then choose a different pattern for each series.

  3. 3. Check the legend

    Add a legend and compile; PGFPlots uses each series’ pattern in its legend sample.

You can distinguish bar series by their hatch patterns while keeping the pattern strokes the same color. The key is to set pattern and pattern color separately for each \addplot; the patterns library supplies the built-in hatch styles.

Minimal working example

This example makes two adjacent bar series with blue patterns and a legend. It uses the standalone document class so you can compile it as a complete file or copy the tikzpicture into a larger document.

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  bar width=14pt,
  symbolic x coords={A,B,C},
  xtick=data,
  ymin=0,
  ylabel={Value},
  enlarge x limits=0.25,
  legend style={at={(0.5,-0.15)}, anchor=north, legend columns=2},
]
\addplot+[
  draw=blue!70!black,
  pattern=north east lines,
  pattern color=blue!70!black,
] coordinates {(A,3) (B,5) (C,4)};

\addplot+[
  draw=blue!70!black,
  pattern=horizontal lines,
  pattern color=blue!70!black,
] coordinates {(A,4) (B,2) (C,6)};

\legend{Group 1, Group 2}
\end{axis}
\end{tikzpicture}
\end{document}

The two series use different hatches, but both the pattern strokes and bar outlines use blue!70!black. The ybar option arranges the two series side by side at each x-coordinate. symbolic x coords and xtick=data provide the category labels A, B, and C.

How the pattern options work

  • \usetikzlibrary{patterns} loads TikZ’s built-in patterns. Without it, pattern names such as north east lines may be undefined.
  • pattern=north east lines chooses the hatch style. Change this value for each series; other built-in choices include horizontal lines and dots.
  • pattern color=blue!70!black sets the color of the pattern strokes. Give each series the same value to keep the patterns the same color.
  • draw=blue!70!black sets the bar outline color. It is independent of the pattern color, so set both if you want matching outlines.

The pattern fill is transparent: the gaps between hatch strokes show the plot background. On a white page, they appear white. If you need to force a white background for each bar, add preaction={fill=white} to the plot options, before the pattern is drawn.

Common mistakes

Forgetting to load the library. Add \usetikzlibrary{patterns} in the preamble, not inside the axis environment.

Changing the fill color instead of the pattern color. For hatched fills, use pattern color to control the hatch strokes. draw controls the outline. The fill option alone does not select a hatch style.

Giving every series the same pattern. If both color and pattern are identical, the series are difficult to tell apart. Use a distinct pattern for each series and keep the color consistent.

Expecting color to appear in the legend automatically. The legend sample follows the plot style, but you still need \legend{...} (or another legend setup) to display the labels.

References

  1. PGFPlots Manual
  2. PGF/TikZ Manual: Patterns Library

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.