1

How can the bar charts from the following code be drawn from the y-axis?

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.76, transform shape]
\pgfmathsetmacro{\incrmntx}{1.2}
\pgfmathsetmacro{\incrmnty}{1}
\pgfmathsetmacro{\inc}{100}
\draw [thick](0,0) -- (\incrmntx*8,0);
\draw [thick](0,0) -- (0,\incrmnty*10);
\foreach \y [evaluate=\y as \aff using int(\y*1)]in {1,...,10}{\draw [black] (.15,\incrmnty*\y) -- +(0:-.3);}
\foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {1,...,8}{\draw [black!20!white] (\incrmntx*\x,0) -- +(90:10);}
\foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {0,...,8}{\draw [black] (\incrmntx*\x,.15) -- +(-90:.3) node [below, black] {\aff};}
\draw[blue, ybar, bar width=.4cm, fill=blue] plot coordinates{(1,1.5) (2,2.5) (3,3.5) };
\node[below] at (1,-.1) {x};
\node[below] at (2,-.1) {y};
\node[below] at (3,-.1) {v};
\end{tikzpicture}
\end{frame}
\end{document}

to be

enter image description here

instead of

enter image description here

3

Change ybar to xbar, I think you will get mainly what you envisage (I think):

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
    \begin{frame}[t]
    \frametitle{}
    \begin{tikzpicture}[scale=.76, transform shape]
    \pgfmathsetmacro{\incrmntx}{1.2}
    \pgfmathsetmacro{\incrmnty}{1}
    \pgfmathsetmacro{\inc}{100}
    \draw [thick](0,0) -- (\incrmntx*8,0);
    \draw [thick](0,0) -- (0,\incrmnty*10);
    \foreach \y [evaluate=\y as \aff using int(\y*1)]in {1,...,10}{\draw [black] (.15,\incrmnty*\y) -- +(0:-.3);}
    \foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {1,...,8}{\draw [black!20!white] (\incrmntx*\x,0) -- +(90:10);}
    \foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {0,...,8}{\draw [black] (\incrmntx*\x,.15) -- +(-90:.3) node [below, black] {\aff};}
    \draw[blue, xbar, bar width=.4cm, fill=blue] plot coordinates{(1,1.5) (2,2.5) (3,3.5) };
    \node[below] at (1,-.1) {x};
    \node[below] at (2,-.1) {y};
    \node[below] at (3,-.1) {v};
    \end{tikzpicture}
\end{frame}
\end{document}

this gives:

enter image description here

  • Thank you. Can y labels be added to the bars instead of defining them separately as nodes? – Hany Feb 8 at 14:43
  • @Hany Since you already have the values in hand, you can use the bar plots of pgfplots with the data-set being supplied. That makes your life more easier. But, yes, it can be added. I will try to add that part as well ;) – Raaja Feb 8 at 14:46
2

Similar to Raaja's nice answer but also including a pgfplots alternative.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.76, transform shape,%x={(0,1)},y={(1,0)}
]
\pgfmathsetmacro{\incrmntx}{1.2}
\pgfmathsetmacro{\incrmnty}{1}
\pgfmathsetmacro{\inc}{100}
\draw [thick](0,0) -- (\incrmntx*8,0);
\draw [thick](0,0) -- (0,\incrmnty*10);
\foreach \y [evaluate=\y as \aff using int(\y*1)]in {1,...,8}
{\draw [black] (.15,\incrmnty*\y) -- +(0:-.3);}
\foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {1,...,10}
{\draw [black!20!white] (\incrmntx*\x,0) -- +(90:10);}
\foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {0,...,8}
{\draw [black] (\incrmntx*\x,.15) -- +(-90:.3) node [below, black] {\aff};}
\draw[blue, xbar, bar width=.4cm, fill=blue] 
plot coordinates{(1.5,1) (2.5,2) (3.5,3) };
\node[left] at (-.1,1) {x};
\node[left] at (-.1,2) {y};
\node[left] at (-.1,3) {v};
\end{tikzpicture}
\end{frame}

\begin{frame}
\frametitle{A pgfplots alternative}
\begin{tikzpicture}
 \begin{axis}[xbar,ytick={1,2,3},yticklabels={x,y,v},ytick={1,2,...,8},
 xmajorgrids,
 ymax=8,xmax=800]
  \addplot coordinates{(150,1) (250,2) (350,3) };
 \end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

  • @marmotThank you for your answer. But how to adjust the alternative method to look like your original code, regarding the axes ticks, and bar spacings. – Hany Feb 12 at 5:07
  • 1
    @Hany This is rather easy. All you need to do is to add is ytick={1,2,3},ytick={1,2,...,8}, xmajorgrids, ymax=8,xmax=800 to the axis options. I updated the answer accordingly. – marmot Feb 12 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.