更改条形图的方向

更改条形图的方向

以下代码中的条形图如何从 y 轴绘制?

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

成为

在此处输入图片描述

代替

在此处输入图片描述

答案1

改为,我ybarxbar你主要会得到你所设想的(我认为):

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

得出:

在此处输入图片描述

答案2

与 Raaja 的精彩回答类似,但还包括 pgfplots 替代方案。

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

在此处输入图片描述

相关内容