# Drawing bar graphs using \foreach

Using the following code from the answer to this question, How to draw the following bar graphs.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.4, transform shape,linecolor/.style={thick,blue}]
\pgfmathsetmacro{\incrmnt}{1.}
\pgfmathsetmacro{\inc}{5}
\draw [thick,-latex](0,0) -- (8,0) node [black, xshift=.35cm, yshift=0cm] {Age};
\draw [thick,-latex](0,0) -- (0,6) node [black, xshift=0cm, yshift=.3cm] {Fr.};
\foreach \y [evaluate=\y as \aff using int(\y*2)]in {1,...,5}{
\draw [linecolor] (.15,\incrmnt*\y) -- +(0:-.3) node [left] {\aff};
}

\foreach \x [evaluate= \x as \aff using int(\inc*\x+20)]in {1,...,6}{
\draw [linecolor] (\incrmnt*\x,.15) -- +(-90:.3) node [below] {\aff};
}
\end{tikzpicture}
\end{frame}
\end{document}  Tikz allows you to build graphs as a bar graph like the one in your example or as a curve with the plot operation.

Here, to draw the sticks from one coordinate to another, there is the option ybar interval which draws the sticks in the interval between two consecutive coordinates.

As on your axes, the ordinates (number of the vertical axis) are multiplied by two, it is enough to divide by two the ordinates.

\draw[ybar interval,pattern=north west lines]
plot coordinates{(1,1) (2,4) (3,5) (4,2) (5,0)};


(1,1) (2,4) draws a stick, i. e. a rectangle whose upper left corner is located at the point (1,1) and whose lower right corner is at point of abscissa 2 i. e. at the point of coordinates (2,0).

(1,1) (2,4) (3,5) does the same by adding a second rectangle whose upper left corner is located at the coordinate point (2,4) and the lower right corner is the abscissa point 3.

Etc.

## First graph: \documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.4, transform shape,linecolor/.style={thick,blue}]
\pgfmathsetmacro{\incrmnt}{1.}
\pgfmathsetmacro{\inc}{5}
\draw [thick,-latex](0,0) -- (8,0) node [black, xshift=.35cm, yshift=0cm] {Age};
\draw [thick,-latex](0,0) -- (0,6) node [black, xshift=0cm, yshift=.3cm] {Fr.};
\foreach \y [evaluate=\y as \aff using int(\y*2)]in {1,...,5}{
\draw [linecolor] (.15,\incrmnt*\y) -- +(0:-.3) node [left] {\aff};
}

\foreach \x [evaluate= \x as \aff using int(\inc*\x+20)]in {1,...,6}{
\draw [linecolor] (\incrmnt*\x,.15) -- +(-90:.3) node [below] {\aff};
}

\draw[ybar interval,pattern=north west lines]
plot coordinates{(1,1) (2,4) (3,5) (4,2) (5,0)};
\end{tikzpicture}

\end{frame}
\end{document}


## Second graph:

For the second graph, it is enough to draw a rectangular stick. For example: \documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.4, transform shape,linecolor/.style={thick,blue}]
\pgfmathsetmacro{\incrmnt}{1.}
\pgfmathsetmacro{\inc}{5}
\draw [thick,-latex](0,0) -- (14,0);
\draw [thick,-latex](0,0) -- (0,6);
\foreach \y [evaluate=\y as \aff using int(\y*2)]in {1,...,5}{
\draw [] (.15,\incrmnt*\y) -- +(0:-.3) node [left] {\aff};
}

\draw[ybar,bar width=2cm,pattern=north west lines
]
plot coordinates{(2,3.5)  (7,5.5) (12,2)
};
\node[below] at (2,0) {Pass};
\node[below] at (7,0) {Good};
\node[below] at (12,0) {V. Good};
\end{tikzpicture}

\end{frame}
\end{document}


Translated with www.DeepL.com/Translator

• Thank you very very much for your answer. Just one more request please, can the bars have the top part coloured (as if it is a stacked graph) as in the drawing included in my question.
– Hany
Oct 30 '18 at 10:26
• @Hany Ask a new question so that we can do this with your data. Oct 30 '18 at 10:28