如何用 tikz 数据可视化找到交点

如何用 tikz 数据可视化找到交点

我试图说明两种付款之间的差异(一种是固定的,在一定年限后结束,另一种则随着时间的推移而膨胀)。

现在,我的问题很尴尬(而且这还不包括我花费了大量时间来弄清楚如何将这两个(非常基本的)图表放在一起)---我无法弄清楚如何与我已经可视化的数据进行交互。

主要是因为我搞不清楚如何实现任何类似名称的东西,也不知道如何将它们引用为路径……而且我在 Google 上找不到任何关于查找交点、两个函数之间的阴影或与库相关的任何内容datavisualization

手册中讨论了如何使用info来维持对坐标系的访问...但我找不到任何地方对其进行计算。

\documentclass[tikz]{standalone}
\usetikzlibrary{datavisualization,datavisualization.formats.functions,positioning}

\def\pOne{200}
\def\endYear{25}
\def\pTwo{150}
\def\inflation{5}

\begin{document}
\begin{tikzpicture}[baseline]
  \datavisualization [scientific axes,
                      visualize as line/.list={payment1,payment2},
                      payment1={pin in data={text={payments end},when=x is 25}},
                      all axes={padding=.5em},
                      x axis={label={years},
                              length=10cm,
                              ticks={step=5,
                                     minor steps between steps=4}},
                      y axis={label={monthly payment},
                              attribute=y,
                              scaling=min at 0cm,
                              length=10cm,
                              ticks={tick prefix=\$,
                                     step=50,
                                     minor steps between steps=4}},
                      ]
    data [set=payment1] {
      x, y
      0, 200
      25, 200
      25, 0
      35, 0
    } % why can't I use \pOne (or a comment) here?
    data [format=function,set=payment2] {
      var x : interval [0:35]; % is there a way to make this (non-)inclusive?
      func y = \pTwo*(1+(\inflation/100))^\value x);
    }
    info{
      % intersection
      % distances
      % areas between
      % etc.
    };
\end{tikzpicture}


\end{document}

结果

答案1

我不确定这个库的内部工作原理。至少使用类似的东西payment1={style={name path global=path1}}似乎不会产生可访问的命名路径,以便以后可以与一起使用name intersections

我想到两件事。首先,你可以在相关坐标处添加一个标签(在= 200),您可以命名并稍后访问。但是,这要求您以某种方式设置样本大小,使得坐标或多或​​少恰好位于右侧X值并不完全是微不足道的:

\documentclass[tikz]{standalone}
\usetikzlibrary{datavisualization, datavisualization.formats.functions}

\def\pOne{200}
\def\endYear{25}
\def\pTwo{150}
\def\inflation{5}

\begin{document}
\begin{tikzpicture}[baseline]
  \datavisualization [
    scientific axes,
    visualize as line/.list={payment1, payment2},
    payment1={
        pin in data={text={payments end}, when=x is \endYear},
    },
    payment2={
        label in data={when=y is \pOne, node style={anchor=center, name=intersection}},
    },
    all axes={padding=.5em},
    x axis={
        label={years},
        length=10cm,
        ticks={
            step=5, 
            minor steps between steps=4}
        },
    y axis={
        label={monthly payment},
        attribute=y,
        scaling=min at 0cm,
        length=10cm,
        ticks={
            tick prefix=\$, 
            step=50, 
            minor steps between steps=4}
        },
    ]
    data [set=payment1] {
      x, y
      0, \pOne
      \endYear, \pOne
      \endYear, 0
      35, 0
    } 
    data [format=function, set=payment2] {
      var x : interval [0:35] samples 101; 
      func y = \pTwo*(1+(\inflation/100))^\value x);
    }
    info{
      \draw[red] (intersection) circle[radius=2pt];
    };
\end{tikzpicture}
\end{document}

在此处输入图片描述

第二个更精确的解决方案是转换绘制曲线的公式,然后计算出X为了= 200(因为在这种情况下您知道这个值并且它存储在中\pOne):

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{datavisualization, datavisualization.formats.functions}

\def\pOne{200}
\def\endYear{25}
\def\pTwo{150}
\def\inflation{5}

\begin{document}
\begin{tikzpicture}[baseline]
  \datavisualization [
    scientific axes,
    visualize as line/.list={payment1, payment2},
    payment1={
        pin in data={text={payments end}, when=x is \endYear},
    },
    all axes={padding=.5em},
    x axis={
        label={years},
        length=10cm,
        ticks={
            step=5, 
            minor steps between steps=4}
        },
    y axis={
        label={monthly payment},
        attribute=y,
        scaling=min at 0cm,
        length=10cm,
        ticks={
            tick prefix=\$, 
            step=50, 
            minor steps between steps=4}
        },
    ]
    data [set=payment1] {
      x, y
      0, \pOne
      \endYear, \pOne
      \endYear, 0
      35, 0
    } 
    data [format=function, set=payment2] {
      var x : interval [0:35]; 
      func y = \pTwo*(1+(\inflation/100))^\value x);
    }
    info{
        \pgfmathsetmacro{\ix}{ln(\pOne/\pTwo)/ln((1+(\inflation/100)))} 
        \draw[red] (visualization cs: x={\ix}, y={\pOne}) circle[radius=2pt];
    };
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容