2

I would like to draw these two pictures of 4 bar charts. enter image description here

and

enter image description here

The numbers in the black rectangles are their areas but I am happy to enter those values manually.

I thought pgfplots might be the right route but I am stuck at the very beginning using this skeleton code which is meant to represent the first of the four bar charts in the top picture.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
%---------------------------------------------------------------%
\begin{tikzpicture}
\begin{axis}[
    area style,
    xticklabels={4, 2, 3, 1},
    yticklabels = {}
    ]
\addplot[ybar interval,mark=no] plot coordinates { (0, 4) (5, 2) (10, 3) (15, 1)};

\end{axis}
\end{tikzpicture}
\end{document}  

To make my question more limited and specific, how I make the bars narrower and put the numbers below the bars as in the images I have posted?

2
  • Have you tried just using plain tikz \drawcommands? In the past, one had to use (axis cs:0, 4) to specify coordinates in the axis coordinate systems, but that was changed, so a simple \draw (0, 4) ... with suitable filling + text node should "just work". See the basic tutorial in tikzmanual for guidance on how to draw basic shapes + text.
    – ojdo
    Nov 3, 2022 at 14:20
  • @ojdo I haven't. I am completely new to tikz and pgfplots.
    – Simd
    Nov 3, 2022 at 15:32

1 Answer 1

3

Here is a macro called \barchart that takes one required argument and one optional argument:

\barchart[<rectangle>]{<bar list>}

The required argument <bar list> is a comma-separated list of rectangle heights. For example, \barchart{4,2,3,1} produces the output:

enter image description here

The optional <rectangle> argument should have the form b1/b2/h to obtain the black rectangle from bar b1 to bar b2 with height h. The area is calculated automatically. For example, \barchart[1/3/2]{4,2,3,1} produces the output:

enter image description here

If you want you can place more than one of these using the syntax [b1/b2/h1,b3/b4/h2,...].

Additionally, there is a macro \myline that draws the vertical lines in your picture. There is a required argument to provide the height.

The global variable \barwidth will adjust the size the bar graphs and the vertical lines.

enter image description here

Here is the code:

\documentclass{article}

\usepackage{tikz}

\newcommand{\barwidth}{.45} % width of each rectangle in cm
\newcommand{\barchart}[2][]{\tikz[baseline]{
  \foreach \y[count=\x] in {#2}{
    \draw[fill=gray!20] (\barwidth*\x,0) node[below, xshift=.5*\barwidth cm]{\small \y} rectangle (\barwidth*\x+\barwidth,\y*\barwidth);
  }
  \foreach \a/\b/\y[evaluate=\y as \A using int((\b-\a+1)*\y)] in {#1}{
    \draw[fill=black](\barwidth*\a,0) rectangle (\barwidth*\b+\barwidth,\barwidth*\y);
    \node[white] at ({(\a/2+\b/2+1/2)*\barwidth},\barwidth*\y/2){\large \A};
  }
}}
\newcommand{\myline}[1]{\hspace{.5em}\tikz[baseline]{\draw[line width=2pt, dash pattern=on 1pt off 1pt](0,0)--(0,#1*\barwidth);}\hspace{.5em}}

\begin{document}
Here are four barcharts.
\[
\barchart{4,2,3,1}\myline{8}
\barchart{2,7,1,8,3,0,5,4}\myline{8}
\barchart{4,3,2,1}\myline{8}
\barchart{1,3,7,4,2}
\]
and
\[
\barchart[1/3/2]{4,2,3,1}\myline{8}
\barchart[7/8/4]{2,7,1,8,3,0,5,4}\myline{8}
\barchart[1/2/3]{4,3,2,1}\myline{8}
\barchart[2/4/3]{1,3,7,4,2}
\]

\end{document}
2
  • 1
    +1 Amazing. Love the minimalism. Dec 8, 2022 at 5:47
  • Just brilliant. Thank you.
    – Simd
    Dec 15, 2022 at 19:07

You must log in to answer this question.

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