Using pgfplots
might be an option, horizontal bars can be made with xbar
, vertical with ybar
. Some key points:
- You just want the histogram, so
hide axis
hides axis lines, ticks, etc.
scale only axis
might on second thought be unnecessary. Without this option the specified width/height will include axis labels and ticklabels.
- You place the axis at the desired place with the
at
key, e.g. at={(5cm,0)}
, which places the bottom left corner of the axis
at (5cm,0)
.
- In order to ensure that there is no space between the bars, and that the full width of the bars are shown, the following steps are taken:
- Set the
width
/height
of the axis
to a specific length, e.g. height=5cm
for the horizontal bars.
- The width of the bars themselves is the axis width/height divided by the number of bars. I have three bars in the example below, so I set
bar width=5cm/3
for the horizontal bars.
- The y-coordinate is at the center of the bars, so to ensure that
pgfplots
extends the axis limits just enough, I add enlargelimits={abs=5cm/6}
, which means that the axis limits will be extended half a bar width beyond the first/last point.

\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (4,5);
% histogram on right
\begin{axis}[
at={(4cm,0)},
xbar,
enlargelimits={abs=5cm/6},
scale only axis,
hide axis,
height=5cm,
bar width=5cm/3]
\addplot coordinates {(3,0)(5,1)(2,2)};
\end{axis}
%histogram on top
\begin{axis}[
at={(0,5cm)},
ybar,
enlargelimits={abs=4cm/6},
scale only axis,
hide axis,
width=4cm,
bar width=4cm/3]
\addplot coordinates {(0,1)(1,4)(2,2)};
\end{axis}
\end{tikzpicture}
\end{document}