ヒストグラムのビン幅を変更する方法

ヒストグラムのビン幅を変更する方法

幅 10 の 8 つのビンのヒストグラムを描画しようとしています。これが現在私が持っているもので、ビンの幅は 10 単位より大幅に小さくなっています。

    \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}

ここに画像の説明を入力してください

基本的にバー同士が接触する必要があります。何をすればよいか、何かアイデアはありますか?

答え1

これを行う1つの方法は、ybar interval(x軸の目盛りの位置のわずかな違いに注意してください)を使用することです。

\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}

ここに画像の説明を入力してください

答え2

x 軸の単位幅 (例x=0.1cm) とバーの幅 (bar width=1cmステップ サイズが 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}

ここに画像の説明を入力してください

関連情報