在下面的代码中,如何为每个节点(包含三角形、正方形和菱形)定义一次,而不是在每个位置重复它们的定义?
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{tkz-fct}
\usepackage{gensymb}
\usetikzlibrary{arrows, arrows.meta, positioning, calc, intersections, decorations.pathreplacing, decorations.pathmorphing, decorations.markings, shapes, shapes.misc, shapes.symbols, shapes.geometric, patterns, backgrounds, trees, shadows}
\usetikzlibrary{decorations.shapes}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\tikzset{paintt/.style={ draw=#1, fill=#1},decorate with/.style={decorate, decoration={shape backgrounds, shape=#1, shape size=1mm}}}
\tikzset{paints/.style={ draw=#1, fill=#1},decorate with/.style={decorate, decoration={shape backgrounds, shape=#1, shape size=1mm}}}
\tikzset{paintd/.style={ draw=#1, fill=#1},decorate with/.style={decorate, decoration={shape backgrounds, shape=#1, shape size=1mm}}}
\begin{tikzpicture}[scale=.9, transform shape, paint/.style={thin, draw=#1!50!black, fill=#1!50 }]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{.7}
\pgfmathsetmacro{\xlab}{1997}
\pgfmathsetmacro{\xn}{14}
\pgfmathsetmacro{\yn}{6}
\begin{scope}[xshift=0cm, yshift=0cm]
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) node [black, xshift=0cm, yshift=-.68cm, rotate=90] {\footnotesize \aff} -- +(90:\incrmnty*\yn);}
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\foreach \y [evaluate=\y as \aff using int(\y*100)]in {0,...,\yn}{\draw [black!20!white] (10,\incrmnty*\y) node [black, right] {\aff} --+(180:\incrmntx*\xn+.2);}
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn,0);
\draw [thick](\incrmntx*\xn,0) -- (\incrmntx*\xn,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [blue!30!white, ultra thick](0,\incrmnty*2.6) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}% 97
-- ++(\incrmntx*1,.16*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}% 98
-- ++(\incrmntx*1,.1*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}%99
-- ++(\incrmntx*1,.06*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}%0
-- ++(\incrmntx*1,.16*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}%1
-- ++(\incrmntx*1,.18*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}%2
-- ++(\incrmntx*1,.1*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}%3
-- ++(\incrmntx*1,.1*\incrmnty) node [regular polygon,regular polygon sides=3, paintt=blue!30!white, scale=.2]{}%4
;
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [blue!60!black, ultra thick](0,\incrmnty*3.2) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}% 97
-- ++(\incrmntx*1,.2*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}% 98
-- ++(\incrmntx*1,.1*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}%99
-- ++(\incrmntx*1,.06*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}%0
-- ++(\incrmntx*1,.18*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}%1
-- ++(\incrmntx*1,.18*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}%2
-- ++(\incrmntx*1,.08*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}%3
-- ++(\incrmntx*1,.13*\incrmnty) node [regular polygon,regular polygon sides=4, paints=blue!60!black, scale=.2]{}%4
;
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [blue!80!white, ultra thick](0,\incrmnty*3.6) node [diamond, paintd=blue!80!white, scale=.2]{}% 97
-- ++(\incrmntx*1,.2*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}% 98
-- ++(\incrmntx*1,.1*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}%99
-- ++(\incrmntx*1,.06*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}%0
-- ++(\incrmntx*1,.18*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}%1
-- ++(\incrmntx*1,.18*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}%2
-- ++(\incrmntx*1,.08*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}%3
-- ++(\incrmntx*1,.13*\incrmnty) node [diamond, paintd=blue!80!white, scale=.2]{}%4
;
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
这是一个保留了您的(非常好!)符号但将代码缩减为真正变化的规范的提案:每一步中爬升的垂直距离。作为奖励,代码为节点提供了唯一的名称,但这里没有使用这些名称。这通过添加包含步进节点计数器和插入节点的样式的foreach
循环来实现。重复样式被定义为等等。set mark
3gon
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{decorations.shapes,shapes.geometric}
\newcounter{mystep}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\tikzset{paintt/.style={ draw=#1, fill=#1},decorate with/.style={decorate, decoration={shape backgrounds, shape=#1, shape size=1mm}}}
\tikzset{paints/.style={ draw=#1, fill=#1},decorate with/.style={decorate, decoration={shape backgrounds, shape=#1, shape size=1mm}}}
\tikzset{paintd/.style={ draw=#1, fill=#1},decorate with/.style={decorate, decoration={shape backgrounds, shape=#1, shape size=1mm}}}
\begin{tikzpicture}[scale=.9, transform shape, paint/.style={thin,
draw=#1!50!black, fill=#1!50 },set mark/.style={%
/utils/exec=\stepcounter{mystep},
insert path={node [#1] (X-\arabic{mystep}){}}},
3gon/.style={regular polygon,regular polygon sides=3, paintt=blue!30!white,scale=.2},
4gon/.style={regular polygon,regular polygon sides=4, paints=blue!60!black,scale=.2},
dia/.style={diamond, paintd=blue!80!white, scale=.2}
]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{.7}
\pgfmathsetmacro{\xlab}{1997}
\pgfmathsetmacro{\xn}{14}
\pgfmathsetmacro{\yn}{6}
\begin{scope}[xshift=0cm, yshift=0cm]
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) node [black, xshift=0cm, yshift=-.68cm, rotate=90] {\footnotesize \aff} -- +(90:\incrmnty*\yn);}
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\foreach \y [evaluate=\y as \aff using int(\y*100)]in {0,...,\yn}{\draw [black!20!white] (10,\incrmnty*\y) node [black, right] {\aff} --+(180:\incrmntx*\xn+.2);}
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn,0);
\draw [thick](\incrmntx*\xn,0) -- (\incrmntx*\xn,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [blue!30!white, ultra thick]
(0,\incrmnty*2.6) [set mark=3gon] % 97
foreach \X in {.16,.1,.06,.16,.18,.1,.1}
{ -- ++(\incrmntx*1,\X*\incrmnty) [set mark=3gon]};
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [blue!60!black, ultra thick]
(0,\incrmnty*3.2) [set mark=4gon]% 97
foreach \X in {.2,.1,.06,.18,.18,.08,.13}
{ -- ++(\incrmntx*1,\X*\incrmnty) [set mark=4gon]};
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [blue!80!white, ultra thick](0,\incrmnty*3.6) [set mark=dia]% 97
foreach \X in {.2,.1,.06,.18,.18,.08,.13}
{ -- ++(\incrmntx*1,\X*\incrmnty) [set mark=dia]};
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案2
你pgfplots
可以避免你的问题(但你需要离线定义数据):
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{frame}[fragile]
\frametitle{diagram drawn by \texttt{pgfplots}}
\centering
\begin{tikzpicture}
\begin{axis}[
width = 0.9\linewidth, height=0.7\textheight,
grid,
ymin = 0, ymax= 600,
ytick = {0,100,...,600},
xmin = 1997, xmax=2011,
xtick = {1997,1998,...,2011},
xticklabel style = {rotate=90,anchor=east,
/pgf/number format/1000 sep={}},
enlargelimits = false,
every axis plot post/.append style={very thick},
every mark/.append style={drawn=none, minimum size=2pt}
]
\addplot +[mark=diamond*] coordinates {(1997,250) (1998,280) (1999,290)
(2000,300) (2001,310) (2002,330)
(2003,340) (2004,360)};;
\addplot +[mark=triangle*] coordinates {(1997,350) (1998,380) (1999,390)
(2000,400) (2001,410) (2002,430)
(2003,440) (2004,460)};
\addplot +[mark size=1.6pt,
mark=square*] coordinates {(1997,320) (1998,350) (1999,360)
(2000,370) (2001,390) (2002,400)
(2003,410) (2004,430)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
- 我没有费心定义线条的颜色。如果您不喜欢默认选择,您可以在
\addplot
选项中定义自己的颜色:\addplot +[<your color>, ...],
- 我根据提供的图像估计坐标。
- 详细信息请参见pgfplots 软件包手册(或者在此网站上提出新问题 ;-) )
您可以定义包含所有三个坐标集的数据的表格,而不是使用坐标。这样代码会变得更短:
\documentclass{beamer} \beamertemplatenavigationsymbolsempty \usepackage{verbatim} \usepackage{pgfplots} \pgfplotsset{compat=1.16} \begin{document} \begin{frame}[fragile] \frametitle{diagram drawn by \texttt{pgfplots}} \pgfplotstableread{% date y1 y2 y3 1997 250 350 320 1998 280 390 350 1999 290 380 360 2000 300 400 370 2001 310 410 390 2002 330 430 400 2003 340 440 410 2004 360 460 430 }\mydata \centering \begin{tikzpicture} \begin{axis}[ width = 0.9\linewidth, height=0.7\textheight, grid, ymin = 0, ymax= 600, ytick = {0,100,...,600}, xmin = 1997, xmax=2011, xtick = {1997,1998,...,2011}, xticklabel style = {rotate=90,anchor=east, /pgf/number format/1000 sep={}}, enlargelimits = false, every axis plot post/.append style={very thick}, every mark/.append style={drawn=none, minimum size=2pt} ] \addplot +[mark=diamond*] table[x=date, y=y1] {\mydata}; \addplot +[mark=triangle*] table[x=date, y=y2] {\mydata}; \addplot +[mark size=1.6pt, mark=square*] table[x=date, y=y3] {\mydata}; \end{axis} \end{tikzpicture} \end{frame} \end{document}
结果和以前一样。