Tx · LaTeX & TikZ

How to Set a Colormap for Bars in PGFPlots

Updated 13 October 2026

At a glance

  1. 1. Choose the coloring goal

    Decide whether all bars should share one fill or each bar should get its own color.

  2. 2. Load a colormap

    Set a built-in or custom colormap in the axis options.

  3. 3. Split bars if needed

    For distinct colors, make each bar a separate plot and use the colormap cycle list.

  4. 4. Keep positions aligned

    Use numerical x positions and set bar shift to zero so the separate plots do not offset the bars.

A colormap in PGFPlots defines a range of colors, but a normal ybar plot usually applies one plot color to all of its bars. To color bars individually, make each bar its own \addplot and let a colormap-based cycle list assign a color to each plot. If you only need one color for every bar, set fill directly instead.

One color for all bars

For a uniform fill, add fill=<color> to the plot options. For example, \addplot[ybar, fill=teal!70] coordinates {...}; fills every bar teal. Defining a colormap in the axis does not, by itself, change that fill.

Give each bar a different colormap color

The following complete example uses PGFPlots’ built-in viridis colormap. Each \addplot contains one bar, so each receives the next color in the cycle list.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=18pt,
    bar shift=0pt,
    xmin=0.5, xmax=5.5,
    ymin=0,
    xtick={1,2,3,4,5},
    xticklabels={A,B,C,D,E},
    ylabel={Value},
    colormap/viridis,
    cycle list={[of colormap]},
]
    \addplot coordinates {(1,2.2)};
    \addplot coordinates {(2,3.5)};
    \addplot coordinates {(3,1.8)};
    \addplot coordinates {(4,4.1)};
    \addplot coordinates {(5,2.9)};
\end{axis}
\end{tikzpicture}

\end{document}

Here, the x coordinates determine where the bars appear, and the y coordinates determine their heights. The colormap cycle list gives successive plots colors sampled from viridis. bar shift=0pt keeps the separately plotted bars centered on their specified x positions rather than shifting them as grouped series. The tick labels replace the numerical positions with category names.

You can replace colormap/viridis with another built-in map or a custom colormap. For example, define a two-color map in the axis options with colormap={mycolors}{color(0cm)=(blue); color(1cm)=(red)} and use the same colormap cycle-list option. The cycle list determines which colors are assigned to the successive bars.

What the color order represents

In this example, colors follow the order of the \addplot commands. They are not automatically chosen according to bar height. If you need colors to represent numerical values—for example, blue for small values and red for large ones—you must decide how values map to colors and organize the plots or data accordingly. A colormap is a scale of colors; it does not infer that mapping just from a standard bar plot.

For many bars, separate plot commands can be repetitive. You can generate those commands from your data or a script, but the key idea remains the same: each independently colored bar needs its own plot color. If the bars should all have the same appearance, keep them in one plot and specify a single fill color.

Common mistakes

  • Setting a colormap and expecting one plot to assign a different color to each bar. A standard ybar plot generally uses one fill for the plot. A colormap does not automatically turn each coordinate into a separately colored bar.
  • Using cycle list={[of colormap]} with only one plot. Cycle-list colors are assigned to successive plots, so one plot still has one plot color.
  • Confusing color order with value-based coloring. The example assigns colors by plot order, not by bar height or a separate data column.
  • Leaving grouped-bar offsets enabled. When each bar is a separate plot, set bar shift=0pt and give each bar its own x position to avoid unintended horizontal offsets.

References

  1. PGFPlots package and manual

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