修改代码,以便从特定的 x 轴值绘制到线交点

修改代码,以便从特定的 x 轴值绘制到线交点

使用以下代码从答案这个问题

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape, -|/.style args={of #1 at #2}{insert path={(intersection cs:first line={#1}, second line={(#2,0)--(#2,10)}) coordinate (aux) (#2,0) |- (0,0|-aux) }}, vert/.style args={of #1 at #2}{insert path={(#2,0) -- (intersection cs:first line={#1}, second line={(#2,0)--(#2,10)}) }}]% this is used when drawing 2 lines to intersect with
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{2.}
\pgfmathsetmacro{\xlab}{2006}
\pgfmathsetmacro{\xn}{5}
\pgfmathsetmacro{\yn}{5}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn+\incrmntx*1,0);
\node at (\incrmntx*\xn+\incrmntx*1,\incrmnty*\yn-.4) (endx){};
\draw [thick](0,0) -- (0,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=0cm, yshift=0cm]
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))] in {0,...,\xn}{\draw [black!40!white] (\incrmntx*\x+\incrmntx*.4,0) node [black, xshift=0cm, yshift=-.68cm, font=\footnotesize] {\aff} -- +(-90:.2);}
\end{scope}
\draw[blue!40!black, densely dashed, line width=.4mm] (-.1*\incrmntx,1.56*\incrmnty) node [black, left, yshift=-.06cm] {Intercept} -- (endx.center);
\draw[blue!80!white, thick, dashed, -|={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn+\incrmntx*.4}}] node [black, left, yshift=.06cm] {2011 value};
\draw[blue!80!white, thick, dashed, -|={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn-2+\incrmntx*.4}}] node [black, left, yshift=-.06cm] {2010 value};
\draw[blue!80!white, thick, dashed, vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn-4+\incrmntx*.4}}];
\draw[red!80!white, thick] (\incrmntx*\xn-7+\incrmntx*.4,2.58*\incrmnty) -- (\incrmntx*\xn-10+\incrmntx*.4,2.58*\incrmnty) node [black, xshift=.6cm, yshift=.3cm] {Slope} -- +(-90:.82cm);
\end{tikzpicture}
\end{frame}
\end{document}

如何进行以下修改:

  1. 修改样式以仅绘制一条斜线,而不是两条

  2. 从指定位置与斜线的交点(2007年和2008年之间的某点)绘制红线,到最终指定位置(2006年)

在此处输入图片描述

答案1

这是解决第二点的样式hat。遗憾的是,第一点我不明白。您说的“只画一条斜线,而不是两条”是什么意思?也就是说,您指的是哪两条线?

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape, 
  -|/.style args={of #1 at #2}{insert path={(intersection cs:first line={#1}, second line={(#2,0)--(#2,10)}) coordinate (aux) (#2,0) |- (0,0|-aux) }}, 
  vert/.style args={of #1 at #2}{insert path={(#2,0) -- (intersection cs:first
  line={#1}, second line={(#2,0)--(#2,10)}) }},
  hat/.style args={of #1 at #2 and #3}{insert path={%
  (intersection cs:first line={#1}, second line={(#2,0)--(#2,10)}) |-
  (intersection cs:first line={#1}, second line={(#3,0)--(#3,10)}) 
  }}
 ]% this is used when drawing 2 lines to intersect with
 \pgfmathsetmacro{\incrmnty}{1.}
 \pgfmathsetmacro{\incrmntx}{2.}
 \pgfmathsetmacro{\xlab}{2006}
 \pgfmathsetmacro{\xn}{5}
 \pgfmathsetmacro{\yn}{5}
 \begin{scope}[xshift=0cm, yshift=0cm]
  \draw [thick](0,0) -- (\incrmntx*\xn+\incrmntx*1,0);
  \node at (\incrmntx*\xn+\incrmntx*1,\incrmnty*\yn-.4) (endx){};
  \draw [thick](0,0) -- (0,\incrmnty*\yn);
 \end{scope}
 \begin{scope}[xshift=0cm, yshift=0cm]
  \foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))] in {0,...,\xn}{\draw [black!40!white] (\incrmntx*\x+\incrmntx*.4,0) node [black, xshift=0cm, yshift=-.68cm, font=\footnotesize] {\aff} -- +(-90:.2);}
 \end{scope}
  \draw[blue!40!black, densely dashed, line width=.4mm] (-.1*\incrmntx,1.56*\incrmnty) node [black, left, yshift=-.06cm] {Intercept} -- (endx.center);
  \draw[blue!80!white, thick, dashed, -|={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn+\incrmntx*.4}}] node [black, left, yshift=.06cm] {2011 value};
  \draw[blue!80!white, thick, dashed, -|={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn-2+\incrmntx*.4}}] node [black, left, yshift=-.06cm] {2010 value};
  \draw[blue!80!white, thick, dashed, vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn-4+\incrmntx*.4}}];
  \draw[red!80!white, thick,hat={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)}
   at {\incrmntx*\xn-10+\incrmntx*.4} and {\incrmntx*\xn-7+\incrmntx*.4}}]
   node[black,pos=0.55,above right]{Slope};
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容