6

How can I go about putting a grid in front of a bar chart in PGFPlots, preferably as automagically as possible? Ed Tufte demonstrates a rather appealing way of drawing bar charts in The Visual Display of Quantitative Information that consists of removing horizontal grid lines from the bars (example centre), and it occurs to me that this can be replicated by drawing white horizontal lines over the chart, or perhaps through some other route.

Any ideas?

  • Perhaps this would help? http://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz/9562#9562 – Martin Tapankov Mar 10 '11 at 06:12
  • Good suggestion, however unfortunately it doesn't. help lines can indeed be made to draw on top of the chart, however their spacing has no correspondence to the PGFPlots coordinate system of the chart. – Richard Terrett Mar 10 '11 at 06:19

1 Answers1

10

There's an axis on top for the axis environment that can be used to achieve what you asked for:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar,
  xtick=data,
  ymin=0,
  axis lines*=left,
  axis on top,
  ymajorgrids=true,
  grid style={white}]
\addplot [fill=black] coordinates { (0,2) (1,1.5) (2,3)};
\addplot [draw=black!40,fill=black!40] coordinates { (0,2.2) (1,1.1) (2,5.1)};
\end{axis}
\end{tikzpicture}
\end{document}

bar chart with y grid on top

Jake
  • 232,450