# Creating a probability histogram with TikZ

I'm trying to create a probability histogram, and this is its current status

\documentclass[oneside,english]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[    ybar,    ymin=0,    ymax=0.6,    xlabel={$k$},    ylabel={$\mathbb{P} (Z_2=k)$},    xtick={0,...,7},    ytick={0,0.2,...,0.6},    bar width=0.8,    axis
lines=left,    width=8cm,    height=8cm,    enlarge x limits=0.1,    enlarge y
limits=0.1,    ]
\addplot+[ybar interval,mark=no] plot coordinates {(-0.5, 0.5) (0.5, 0.25) (1.5, 0.125)
(2.5, 0.0625) (3.5, 0.03125) (4.5, 0.015625) (5.5, 0.0078125) (6.5, 0.00390625) (7.5,
0.001953125)};
\end{axis}
\end{tikzpicture}
\end{document}


Is there a way to shift the y-axis down so that the bars emanate from the horizontal axis? I'd also like to move the y-axis slightly to the right so that it coincides with the left-side of the first rectangle.

Like this?

You need to set enlarge y limits accordingly:

\documentclass[oneside,english]{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=8cm,    height=8cm,
axis lines=left,
ybar,
bar width=1,
xlabel={$k$},    ylabel={$\mathbb{P}(Z_2=k)$},
xtick={0,...,8},
enlarge x limits=0.1,
enlarge y limits={0.1, upper}  % <----
]
plot coordinates {  (-0.5,0.5)      (0.5,0.25)          (1.5,0.125)
(2.5,0.0625)    (3.5,0.03125)       (4.5,0.015625)
(5.5,0.0078125) (6.5,0.00390625)    (7.5,0.001953125)};
\end{axis}
\end{tikzpicture}
\end{document}


Data for your diagram you can collect in table in which also define, xticks. By this changes diagram code is a wee bit simpler and shorter:

\documentclass[oneside,english]{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=8cm, height=8cm,
axis lines=left,
ybar,
bar width=1,
xlabel={$k$},    ylabel={$\mathbb{P}(Z_2=k)$},
ticklabel style = {font=\footnotesize},
xtick=data,
enlarge x limits=0.1,
ymax=0.55
]
2   0.25
3   0.125
4   0.0625
5   0.03125
6   0.015625
7   0.0078125
8   0.00390625
9   0.001953125
};
\end{axis}
\end{tikzpicture}
\end{document}


Diagram is the same as before.

• @The Substitute, I wrote (for exercise) addendum to answer . See if It may be helpful ;-) Commented Feb 15, 2023 at 2:26
• I combined them to get what I want. For the first code, I had an easier time moving the y-axis to the right (by using enlarge x limits=-0.5,) and from the newer code I took the " ymax=0.55" (the latter helps when comparing different graphs side-by-side since it will not change the scaling of the y-axis). Commented Feb 15, 2023 at 2:36