3

I would like to have a simple way of drawing the following red lines, given a certain function (here f(x) = x^2).

what I want to draw (the red polylines)

Here is one of the ideas I have had so far (that mostly feel silly) :

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}

\pgfplotstableread{
  1
  2.5
  3
} \myTable % -- I can't use this method with the `expr` syntax ? 

\begin{tikzpicture}
  \begin{axis}
    [
      xtick = {1,2.5,3}, 
      ytick = {1,6.25,9}, 
      domain = 0:4,
      axis lines = middle,
    ]
    \addplot [thick, blue] {x^2} node [near end, anchor = east] {$y = x^2$};
    \addplot [xcomb, red] table[x index = 0,y expr=x^2] {
        1
      2.5
      3
    };
    \addplot [ycomb, red] table[x index = 0,y expr=x^2] {
        1
      2.5
      3
    };
    %\addplot table[x index=0,y expr=x^2] {\myTable}; % does not work ?!
  \end{axis}
\end{tikzpicture}
\end{document}

Another way of doing things I came up with is something like

draw [myStyle] (axis cs: 1, 0) |- (axis cs: 0, 1) ; 
draw [myStyle] (axis cs: 2.5, 0) |- (axis cs: 0, 6.25) ; 

repeated for each point I want to place,

but I suspect there might be some much more clever way to do this.

Any ideas ?

1 Answer 1

6

Ok I found a better idea with a for loop :

(I changed the function to showcase that it works with named functions, like sqrt(x), and not only x^2)

enter image description here

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
      xtick = {1,6.25,9},
      ytick = {1,2.5,3},
      domain = 0:10, samples=100,
      axis lines = middle,
    ]
    \addplot [thick, blue] {sqrt(x)} node [pos=.4, anchor = south east, inner sep=1pt] {$y = \sqrt{x}$};

    \pgfplotsinvokeforeach {1,6.25,9}
      {
        \draw [red] (axis cs: #1, 0) |- (axis cs: 0, {sqrt(#1)}) ;
      }
%    % variant : 
%    \pgfplotsextra
%    {
%      \foreach \x in {1,6.25,9}
%      {
%        \draw [red] (axis cs: \x, 0) |- (axis cs: 0, {sqrt(\x)}) ;
%      }
%    }
  \end{axis}
\end{tikzpicture}
\end{document}

Cheers,

Edit :

  • added support for named points (instead of numbered points) and automatically generating tick list.

  • One could also automatically generate a TickLabelList...

here we go :

\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\usepackage{pgfplots}
\begin{document}

\gdef \xTicks {0}  
\gdef \yTicks {0}  
%\gdef\symbols{{"a","b","myPointWithAnIntricateName"}} %------- variant with a list 

\tikzmath
{
  integer \i ; 
  function f(\x)
  {
    return sqrt(\x);
  };
  let \symbols{0} = a; %the hash for point names --------comment for the list variant
  let \symbols{1} = b;
  let \symbols{2} = myPointWithAnIntricateName;
  \t{a} = 1 ; % here \t stands for the abscissa
  \t{b} = 6.25 ;
  \t{myPointWithAnIntricateName} = 9 ;
  for \i in {0,...,2}% 
  {
    let \theSymbol =\symbols{\i}; % ---------------------comment for the list variant
    %\theSymbol=array(\symbols,\i); % -----------------uncomment for the list variant
    \y{\theSymbol} = f(\t{\theSymbol}) ; %showcase one way to define the hash for y coordinates
    {
      \xdef\xTicks{\xTicks, \t{\theSymbol}} % append value to ticks list
      \xdef\yTicks{\yTicks, \y{\theSymbol}}
    };
  };
}

\begin{tikzpicture}
  \begin{axis}
    [
      xtick = \xTicks,
      ytick = \yTicks,
      domain = 0:10, samples=100,
      axis lines = middle,
    ]
    \addplot [thick, blue] {f(x)} node [pos=.4, anchor = south east, inner sep=1pt] {$y = f(x)$};

    \pgfplotsextra
    {
      \foreach \theSymbol in {a,b,myPointWithAnIntricateName}%
      {
        \draw [red] (axis cs: {\t{\theSymbol}}, 0) |- (axis cs: 0, {\y{\theSymbol}}) ;
      }
    }
  \end{axis}
\end{tikzpicture}
\end{document}

Cheers,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.