使用 \foreach 绘制条形图

使用 \foreach 绘制条形图

使用以下代码从答案这个问题, 如何绘制以下条形图。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\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}

在此处输入图片描述

在此处输入图片描述

答案1

Tikz 允许您将图形构建为条形图(如示例中的条形图)或通过操作构建为曲线plot

这里,为了从一个坐标到另一个坐标绘制棍子,可以选择ybar interval在两个连续坐标之间的间隔内绘制棍子。

正如在您的轴上,纵坐标(垂直轴的数量)乘以二,将纵坐标除以二就足够了。

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

(1,1) (2,4)画一根棍子,即一个矩形,其左上角位于点(1,1),右下角位于横坐标点,2即坐标点(2,0)

(1,1) (2,4) (3,5)同样的做法,添加第二个矩形,其左上角为坐标点(2,4),右下角为横坐标点3

ETC。

第一张图:

图形

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\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}

第二张图:

对于第二张图,画一个矩形棒就足够了。例如:

图二

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\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}

使用 www.DeepL.com/Translator 翻译

相关内容