I want to draw a bar graph with the bars touching, like in a histogram, using pgfplots. This is basically what I'm aiming for:
That plot is generated with this code:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar]
\addplot[fill=gray, bar width=41] plot coordinates
{(1,160) (2,-50) (3,-98) (4,95) (5,270)};
\end{axis}
\end{tikzpicture}
\end{document}
The problem with my code is that the value for the bar width
key was found by trial and error, doesn't place the bars exactly right, and needs to be changed if I want to change the size of the graph.
I was expecting to be able to do something like this:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar, width=\textwidth,]
\addplot[fill=gray, bar width=0.2\textwidth] plot coordinates
{(1,160) (2,-50) (3,-98) (4,95) (5,270)};
\end{axis}
\end{tikzpicture}
\end{document}
but that makes the bars too wide:
Ideally I'd like a solution that scales with changes in the size of the graph.
ybar
tryybar interval
. Is that a reasonable result for you?xmajorgrids=false
, but I still get ticks between the numbers on the x-axis, and I can't figure out how to get rid of these without losing the numbers too.