简化重复绘图和节点放置

简化重复绘图和节点放置

如何应用以下代码,从答案到这个问题(给出第一张图)绘制第二张和第三张图

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{frame}[t]
\frametitle{}
\hspace {-1cm}
\begin{tikzpicture}[scale=.7, transform shape,linecolor/.style={ultra thick, green!40!black},dotline/.style={thin, black, loosely dotted},
point/.style={circle, draw, red, fill=red, scale=0.4},
cercle/.style={circle,draw,scale=.5, red,thick, fill=white,inner sep=6pt}
]
\pgfmathsetmacro{\incrmnt}{.6}

%draw the axes of the left graph
\draw [thick,-latex](-.3,0) -- (5,0);
\draw [thick,-latex](0,-.3) -- (0,8);

%draw the graph itself
\foreach \y in {1,...,12}{\draw [linecolor] (0,\incrmnt*\y) node (yhl){} +(0:.15cm) -- +(180:.15cm) node [left] {\y/12};
\draw [dotline] (0,\incrmnt*\y) -- +(0:8*\incrmnt);}
\foreach \x in {1,...,7}{\draw [linecolor] (\incrmnt*\x,.15) node [ ]{} -- (\incrmnt*\x,-.15) node [below] {\x} ;}

% left graph
\begin{scope}[every node/.style={circle, draw, solid, red, fill=red, scale=0.4}]
\draw [linecolor] (\incrmnt*1,\incrmnt*1) node{} -- (\incrmnt,.15);
\draw [linecolor] (\incrmnt*2,\incrmnt*2) node{}-- (\incrmnt*2,.15);
\draw [linecolor] (\incrmnt*3,\incrmnt*2) node{} -- (\incrmnt*3,.15);
\draw [linecolor] (\incrmnt*4,\incrmnt*2) node{} -- (\incrmnt*4,.15);
\draw [linecolor] (\incrmnt*5,\incrmnt*2) node{} -- (\incrmnt*5,.15);
\draw [linecolor] (\incrmnt*6,\incrmnt*2) node{} -- (\incrmnt*6,.15);
\draw [linecolor] (\incrmnt*7,\incrmnt*1) node {} -- (\incrmnt*7,.15);
\end{scope}

%do the same with the second right graph, plot the same axis by a simple 8cm translation 
\begin{scope}[xshift=8cm]
\draw [thick,-latex](-2,0)node [below right] {$-\infty$}   -- (6,0);
\draw [thick,-latex](0,-.3)  -- (0,8);
\foreach \y in {1,...,12}{\draw [linecolor] (0,\incrmnt*\y) +(0:.15cm) -- +(180:.15cm) node [left] {\y/12};
\draw [dotline] (0,\incrmnt*\y) -- +(0:10*\incrmnt);}
\foreach \x in {1,...,7}{\draw [linecolor] (\incrmnt*\x,.15) -- (\incrmnt*\x,-.15) node [below] {\x};}

% right graph
\draw [thick, black] (\incrmnt*9,0) node [below] {$\infty$} --+(0,\incrmnt*13);
\foreach \x in {1,...,6}{\draw[ultra thick, blue](\incrmnt*\x,{\incrmnt*(2*\x-1)})node[point]{}--+(\incrmnt,0)node[cercle]{};}
\node[cercle] at(\incrmnt,0){};
\draw[ultra thick, blue](\incrmnt*7,\incrmnt*12)node[point]{}--+(\incrmnt*2,0);
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案1

如果我回答你的问题时就知道旅程会去哪里上一个问题当我写作时我知道什么这个答案,我当时会推荐 pgfplots。但鉴于你似乎不喜欢 pgfplots,我再次为你提供循环。尽管如此,我还是忍不住要求你至少考虑使用 pgfplots。你永远不必用笨拙的单位和比例因子乘以所有内容,也不必处理绘制图表或添加刻度等的循环。(不,遗憾的是我不愿意为了好玩而将以下内容快速翻译成 pgfplots。)

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\tikzset{linecolor/.style={ultra thick, green!40!black},dotline/.style={thin, black, loosely dotted},
point/.style={circle, draw, red, fill=red, scale=0.4},
cercle/.style={circle,draw,scale=.5, red,thick, fill=white,inner sep=6pt}}
\begin{document}
\begin{frame}[t]
\frametitle{First drawing (unchanged)}
\begin{tikzpicture}[scale=.7, transform shape]
\pgfmathsetmacro{\incrmnt}{.6}

\draw [thick,-latex](-.3,0) -- (5,0);
\draw [thick,-latex](0,-.3)  -- (0,8);
\draw [thick,-latex](6.,0)  -- (14.4,0) coordinate (br);
\draw [thick,-latex](8,-.2) -- (8,8) coordinate(tr);
\foreach \X in {1,...,12}
{
\draw [dotline] (0,\X*7cm/12) -- ++ (14,0);
\draw [linecolor] (-\incrmnt/2,\X*7cm/12)node[left]{\X/12} -- ++ (\incrmnt,0)
(8-\incrmnt/2,\X*7cm/12)node[left]{\X/12}-- ++ (\incrmnt,0);
}
\foreach \X in {1,...,7}
{
\draw[linecolor]  (3*\X/5,-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt)
(8+3*\X/5,-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt);
\pgfmathtruncatemacro{\tst}{abs(\X-4)}
\ifnum\tst=3
\draw[linecolor]  (3*\X/5,0)  -- ++ (0,7/12)
node[fill,red,circle,inner sep=2pt]{};
\else
\draw[linecolor]  (3*\X/5,0)  -- ++ (0,14/12)
node[fill,red,circle,inner sep=2pt]{};
\fi
\ifnum\X=7
\draw[blue,thick]   (8+3*\X/5,{7*(2*\X-2)/12})
node[fill,red,circle,inner sep=2pt]{} -- ++ (2*7cm/12,0) coordinate(xr);
\else
\draw[blue,thick]  (8+3*\X/5,{7*(2*\X-1)/12}) node[fill,red,circle,inner sep=2pt]{}
-- ++ (1*7cm/12,0) node[circle,fill=white,draw=red,inner sep=2pt]{};
\fi
}
\draw (tr-|xr) -- (br-|xr) node[below](ir){$\infty$};
\node at (6.5,0 |-ir) {$-\infty$};
\node[circle,fill=white,draw=red,inner sep=2pt,thick] at (8+3/5,0){};
\end{tikzpicture}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[t]
\frametitle{Second drawing: cdf1}
\begin{tikzpicture}[scale=.7, transform shape]
\pgfmathsetmacro{\incrmnt}{.6}

\draw [thick,-latex](-.3,0) -- (5,0);
\draw [thick,-latex](0,-.3)  -- (0,8);
\draw [thick,-latex](6.,0)  -- (14.4,0) coordinate (br);
\draw [thick,-latex](8,-.2) -- (8,8) coordinate(tr);
\foreach \X [evaluate=\X as \Y using {int(3*\X)}]in {2,4,...,12}
{
\draw [dotline] (0,\X*7cm/12) -- ++ (14,0);
\draw [linecolor] (-\incrmnt/2,\X*7cm/12)node[left]{\X/36} -- ++ (\incrmnt,0)
(8-\incrmnt/2,\X*7cm/12)node[left]{\Y/36}-- ++ (\incrmnt,0);
}
\def\hs{{3,5,4,3,2,1}}
\foreach \X in {0,...,5}
{
\draw[linecolor]  ({3*(\X+1)/4},-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt)
({8+3*(\X+1)/4},-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt);
\pgfmathtruncatemacro{\h}{\hs[\X]}
\draw[linecolor]  ({3*(\X+1)/4},0)  -- ++ (0,7*\h/6);
\draw[blue,thick]  ({8+3*(\X+1)/4},{7*(2*\X+2)/12}) node[fill,red,circle,inner sep=2pt]{}
-- ++ (3/4,0) node[circle,fill=white,draw=red,inner sep=2pt] (aux){};
\ifnum\X<5
\draw[cyan,thick] (aux) -- ++ (0,7/6);
\fi
}

\end{tikzpicture}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[t]
\frametitle{Third drawing: cdf2}
\begin{tikzpicture}[scale=.7, transform shape]
\pgfmathsetmacro{\incrmnt}{.6}

\draw [thick,-latex](-.3,0) -- (5,0);
\draw [thick,-latex](0,-.3)  -- (0,8);
\draw [thick,-latex](6.,0)  -- (14.4,0) coordinate (br);
\draw [thick,-latex](8,-.2) -- (8,8) coordinate(tr);
\foreach \X in {1,...,8}
{
\draw [dotline] (0,\X*7/8) -- ++ (14,0);
\ifnum\X=8
\draw [linecolor] (-\incrmnt/2,\X*7/8)node[left]{1} -- ++ (\incrmnt,0)
(8-\incrmnt/2,\X*7/8)node[left]{1}-- ++ (\incrmnt,0);
\else
\draw [linecolor] (-\incrmnt/2,\X*7/8)node[left]{\X/8} -- ++ (\incrmnt,0)
(8-\incrmnt/2,\X*7/8)node[left]{\X/8}-- ++ (\incrmnt,0);
\fi
}
\def\hs{{1,3,3,1}}
\def\sumh{0}
\foreach \X in {0,...,3}
{
\draw[linecolor]  
({\X+1},-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt)
({8+\X+1},-\incrmnt/2) node[below]{\X} -- ++ (0,\incrmnt);
\pgfmathtruncatemacro{\h}{\hs[\X]}
\pgfmathtruncatemacro{\oldsumh}{\sumh}
\pgfmathtruncatemacro{\sumh}{\sumh+\h}
\xdef\sumh{\sumh}
\draw[linecolor]  ({\X+1},0)  -- ++ (0,7*\h/8) 
node[fill,red,circle,inner sep=2pt]{};
\draw[cyan,latex-latex]  ({8+\X+1},{7*\oldsumh/8}) 
node[circle,fill=white,draw=red,inner sep=2pt]{} 
-- ({8+\X+1},{7*\sumh/8})node[fill,red,circle,inner sep=2pt] (aux){}
node[midway,right,black]{$P_{(\X)}$};
\ifnum\X=3
\draw[blue,thick] (aux) -- ++ (14/8,0) coordinate (r);
\else
\draw[blue,thick] (aux) -- ++ (7/8,0);
\fi
}
\draw (r |- 14,8) -- (r|-14,0)node[below]{$\infty$};
\node at (6.5,0 |-ir) {$-\infty$};
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容