5

How can I create a vertical histogram in Tikz?

I am creating a figure that looks like the one below, however its just a conceptual diagram, thus I do not need it to follow real data.

enter image description here

I already have a square with all needed inside but I want to be able to locate a histogram in the Y axis to the left. I though about creating a bunch of boxes, but I though there may be a better way.

9
  • 1
    May be is an stupid idea but what about a rotated horizontal histogram?
    – Ignasi
    Oct 24, 2016 at 10:34
  • @Ignasi yeah, I guess.... I have no idea how to do that. Something like taking a pgf histogram, rotate it and put it inside the rest of the tikz? Oct 24, 2016 at 10:38
  • 1
    Perhaps this answer from the German speaking TeX community will help you: texwelt.de/wissen/fragen/17242/… Oct 24, 2016 at 11:12
  • Some nice examples of this type of chart based on Metapost here: www-math.univ-poitiers.fr/~phan/statsmac.html
    – Thruston
    Oct 24, 2016 at 15:28
  • For really professional quality you might be better using Stata or R to do the heavy statistical lifting here, and include the output as a graphic...
    – Thruston
    Oct 24, 2016 at 15:29

1 Answer 1

5

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:
    1. Set the width/height of the axis to a specific length, e.g. height=5cm for the horizontal bars.
    2. 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.
    3. 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.

enter image description here

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

You must log in to answer this question.

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