Tx · LaTeX & TikZ

Why Are PGFPlots ybar Patterns Not Showing?

Published By the 9to5Science editorial team

At a glance

  1. 1. Load the pattern library

    Add \usetikzlibrary{patterns} in the preamble so TikZ recognizes built-in pattern names.

  2. 2. Set the bar pattern

    Give the plot a pattern style such as pattern=north east lines.

  3. 3. Set pattern color

    Use pattern color to make the hatch lines visible and draw to outline the bars.

  4. 4. Check competing styles

    Remove or adjust later styles that replace the patterned fill, then compile the minimal example.

PGFPlots draws ybar bars as filled shapes. Their hatch patterns come from TikZ, so the pattern library must be loaded before a built-in pattern name such as north east lines can be used. You should also choose a visible pattern color and check that another style is not replacing the patterned fill.

Minimal working example

This complete example draws three patterned bars. It uses the patterns library and sets both the pattern color and bar outline explicitly.

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  ybar,
  bar width=18pt,
  symbolic x coords={A,B,C},
  xtick=data,
  ymin=0,
  ylabel={Value},
]
\addplot+[
  pattern=north east lines,
  pattern color=blue,
  draw=blue,
] coordinates {
  (A,2) (B,4) (C,3)
};
\end{axis}
\end{tikzpicture}
\end{document}

The key line for recognizing the pattern is \usetikzlibrary{patterns}. The plot option pattern=north east lines selects a built-in hatch; pattern color=blue colors its strokes. draw=blue gives the bar outlines a matching color. You can change both colors to suit the plot.

Check these settings if the pattern is missing

  1. The library is loaded. Put \usetikzlibrary{patterns} in the preamble, after loading TikZ or PGFPlots. A missing library usually causes an unknown-pattern error rather than silently hiding the pattern.
  2. The plot actually has the pattern style. Put pattern=... in the options for the relevant \addplot, or in a style that applies to that plot. Setting it on a different plot will not change these bars.
  3. The pattern has a visible color. pattern color sets the hatch-line color. A light color on a white page, for example, can make the pattern appear absent.
  4. No later style replaces the fill. Inspect plot options, cycle lists, and axis-wide styles for fill settings that may take effect after the pattern style. Remove or reorder conflicting fill styles so the pattern remains the bar fill. If several plots share axis-level styles, test the pattern directly on one \addplot first.
  5. The bars are large enough to show it. Very narrow bars or a small exported figure can make the default hatch spacing difficult to see. Increase the bar width or use a stronger contrasting pattern color to check.

Using a different pattern or separate styles

TikZ’s standard patterns library provides several named patterns, including horizontal lines, vertical lines, north east lines, and crosshatch. Change the value of pattern to try another. For example:

\addplot+[pattern=crosshatch, pattern color=red, draw=black]
  coordinates {(A,2) (B,4) (C,3)};

If you want different patterns for multiple bar series, assign a pattern to each plot rather than relying on an automatic color cycle to choose one. For example, one plot can use pattern=north east lines and the next pattern=north west lines. Give each plot an appropriate pattern color and outline so the series remain distinguishable in print.

Common mistakes

  • Using pattern=... without loading the library: add \usetikzlibrary{patterns} to the preamble.
  • Changing fill instead of pattern color: use pattern color to control the hatch strokes. A fill option is not a substitute for selecting a visible pattern color.
  • Applying the option to the wrong plot: each \addplot can have its own style; confirm the patterned option is attached to the bar series you are viewing.
  • Expecting a pattern to fix a solid style applied later: if another style overwrites the patterned fill, adjust the style order or remove the conflicting setting.

When diagnosing a complicated chart, reduce it to the minimal example above, compile it, and then add your original axis styles and plot options back one at a time. That reveals which setting is hiding or replacing the pattern.

References

  1. PGF and TikZ: Manual for Version 3.1.10
  2. PGFPlots Package

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