1

I'm attempting to draw a histogram of 8 bins of width 10. This is what I currently have, where my bin width is notably much less than 10 units.

    \begin{tikzpicture}
        \begin{axis} [ybar,xmin=70,xmax=160,ymin=0,ymax=20, xlabel = IQ Scores, ylabel = Frequency, xtick={70, 80, 90, 100, 110, 120, 130, 140, 150}]
            \addplot [draw = black, fill = gray] coordinates {
                (75,2)
                (85,3) 
                (95, 10)
                (105, 16)
                (115, 13)
                (125, 10)
                (135, 5)
                (145,1)
            }; 
        \end{axis}
    \end{tikzpicture}

enter image description here

I basically need the bars to touch each other. Any ideas what I need to do?

2 Answers 2

1

One way to do so is to use ybar interval (notice the slight difference in tick position in x axis)

\begin{tikzpicture}
    \begin{axis} [ybar interval,
    grid=none,
    xmin=70,xmax=160,ymin=0,ymax=20, xlabel = IQ Scores, ylabel = Frequency, xtick={70, 80, 90, 100, 110, 120, 130, 140, 150}]
        \addplot [draw = black, fill = gray] coordinates {
            (75,2)
            (85,3) 
            (95, 10)
            (105, 16)
            (115, 13)
            (125, 10)
            (135, 5)
            (145,1)
            (155,0)
        }; 
    \end{axis}
\end{tikzpicture}

enter image description here

0

You can set the unit width of the x-axis (e.g. x=0.1cm) and the bar width (bar width=1cm since your step size is 10).

\documentclass{article}

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

\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        ybar,
        x=0.1cm,
        bar width=1cm,
        xmin=70,
        xmax=160,
        ymin=0,
        ymax=20,
        xlabel=IQ Scores,
        ylabel=Frequency,
        xtick={70, 80, 90, 100, 110, 120, 130, 140, 150}
    ]
        \addplot [draw = black, fill = gray] coordinates {
            (75,2)
            (85,3) 
            (95, 10)
            (105, 16)
            (115, 13)
            (125, 10)
            (135, 5)
            (145,1)
        }; 
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .