Tx · LaTeX & TikZ

Why Are Some PGFPlots Histogram Series Not Showing?

Published By the 9to5Science editorial team

At a glance

  1. 1. Check for overlap

    Histograms drawn on the same bins occupy the same horizontal intervals, so opaque bars can hide earlier series.

  2. 2. Set shared bins

    Give each histogram the same bin count and data range when you want a direct comparison.

  3. 3. Make bars visible

    Use transparent fills and distinct outline colors, or pre-bin the data for side-by-side bars.

If a PGFPlots histogram seems to omit one or more data series, the plots may all be present but drawn on top of one another. This commonly happens when each histogram uses the same bin edges and the bars have opaque fills.

The example below plots two data columns as separate histograms. It fixes the bin range and count so the bars are comparable, then uses transparency so you can see where they overlap.

Why a histogram can hide another

A histogram turns observations into bar heights over intervals, or bins. If two plots use the same bins, the bars occupy the same horizontal positions. PGFPlots draws plots in order: a later opaque bar can cover a bar drawn earlier. The first series may therefore be present in the figure but not visible.

There is a related comparison issue: if each series chooses its own bin range, the bars may not line up. Even if both are visible, comparing their heights can then be misleading. Specify the same bin count and range for every series you want to compare.

A working example with visible overlap

This complete example uses PGFPlots’ statistics library and two sets of observations. Both histograms use five bins over the range from 0 to 10.

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  xmin=0, xmax=10,
  xlabel={Value},
  ylabel={Count},
  legend pos=north west,
]
  \addplot+[
    hist={bins=5, data min=0, data max=10},
    ybar interval,
    fill=blue!60,
    draw=blue!80!black,
    fill opacity=0.45,
  ] table[y index=0] {
    value
    0.4
    1.2
    2.3
    3.4
    4.4
    5.5
    6.2
    7.8
    8.9
  };
  \addlegendentry{Series A}

  \addplot+[
    hist={bins=5, data min=0, data max=10},
    ybar interval,
    fill=orange!75,
    draw=orange!80!black,
    fill opacity=0.45,
  ] table[y index=0] {
    value
    0.6
    0.9
    1.1
    1.8
    2.2
    2.7
    3.2
    4.1
    4.5
    5.1
    6.5
    7.2
    7.5
    8.2
    9.2
  };
  \addlegendentry{Series B}
\end{axis}
\end{tikzpicture}

\end{document}

The key settings are fill opacity=0.45, which lets the earlier fill show through, and the different fill and outline colors. data min, data max, and bins keep the bin boundaries consistent. The table selector y index=0 tells PGFPlots to read the first data column as the observations for that histogram.

If your data are in a file

Add one \addplot for each series and select the intended column for each one. For example, if a table has columns named A and B, the plot commands can use table[y=A] {data.dat} and table[y=B] {data.dat}. Apply the same histogram options to both commands. Check that the column names match the file exactly and that each column contains numeric observations.

If the series are still indistinguishable, temporarily give them strongly different colors and use different opacity values. This helps separate a visibility problem from a data-reading problem. Also check that the selected columns actually contain different data.

When you want side-by-side bars

Transparency shows overlap; it does not move bars apart or create grouped bins. The histogram handler places bars on the intervals defined by the bins. For a side-by-side comparison, first count the observations in each shared bin, then plot those counts as grouped bars with PGFPlots’ bar-plot options, such as ybar. This makes each series visible without stacking or overlaying the bars.

Common mistakes

  • Using different ranges or bin counts: matching bin settings are needed for aligned, comparable histogram intervals.
  • Assuming the last plot is the only one computed: it may simply be covering earlier plots with opaque bars.
  • Using transparency but expecting separation: transparent fills reveal overlap; they do not produce side-by-side bars.
  • Selecting the wrong table column: when reading a file with multiple series, verify the column selector for each \addplot.
  • Expecting the legend to reveal hidden bars: legend entries can appear even when the corresponding plot is obscured, so inspect the plot styling and data selection too.

References

  1. PGFPlots 1.18.2 Manual

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