4

I'm still dabbling in the use of pgfplots but I'm trying to draw arrows on this plot to show an inverse correlation between the variation in the arc length along the curve and the changes in x.

Given that a picture is worth a thousand words, I'll show you my code, the corresponding output and an image of the desired output, so that you can compare and notice the changes I'd like to get.

Also, you might notice my code is based on the information I collected from these posts: Draw Arrows outside of content-area of TikZ, Tikzpicture - Arrow next to/under plot label and Pgfplots: plot text along function. However, I just haven't been able to make it work together.

I've been struggling with this all night long, so sny help is highly appreciated. In fact, if you could add some explanations on how and why your solution works, so that I can learn from it and do better in the future I'd be forever in debt with you :')

This is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings,decorations.text,arrows.meta,arrows}
\pgfplotsset{compat=1.16}
\begin{document}
    
\begin{figure}[htb]
\centering 
\begin{tikzpicture} 
\begin{axis}[
name=MyAxis,
xlabel={$x$},
ylabel={$y$},
xmin=0,
xmax=25,
ymin=0,
ymax=21,
axis y line=center, 
axis x line=center,
ytick=\empty,
xtick=\empty,
]
\addplot[black,domain=0:21,samples=200,very thick]{(-5/4)*21^(3/5)*x^(2/5)+5*x^(8/5)/(16*21^(3/5))+315/16};
\addplot[draw=none,
          postaction={decorate,decoration={text effects along path,
              text={Increasing values of s},raise=2ex,
              text align=center,
              text effects/.cd, 
              text along path,
              every character/.style={fill=white,text=blue, yshift=-0.5ex}
              }},
              domain=0:21,
    ] {(-5/4)*21^(3/5)*x^(2/5)+5*x^(8/5)/(16*21^(3/5))+350/16};
\addplot+[mark=none,blue,
      domain=2:15,latex-,samples=150
    ] {-(5/4)*21^(3/5)*(x-1.5)^(2/5)+5*(x-1.5)^(8/5)/(16*21^(3/5))+(350/16)};
\end{axis}
\def\myShift{-1cm}
  \draw [-latex] ([yshift=-4ex]MyAxis.outer south west) --node[midway, fill=white, yshift=1pt]{Increasing values of $x$} ([yshift=-4ex]MyAxis.outer south east);
\end{tikzpicture}
\caption{Path travelled by dog after $t$ seconds}
\label{ref:my7graph}
\end{figure}
\end{document}

This is the output I get:

enter image description here

This is what I'd like to get: enter image description here

Notice the changes:

  • Shorter horizontal arrow below the x axis.
  • Zero at the origin.
  • White background for the text along the curved arrow.
  • If possible, I'd like to type the "s" in mathmode along the curved arrow.

Edit

These days I read part of the TikZ manual, as well as this and this manual, and found a rudimentary solution. Hopefully someone might come up with a better idea.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings,decorations.text,arrows.meta,arrows}
\pgfplotsset{compat=1.16}
\begin{document}
    
\begin{figure}[htb]
\centering 
\begin{tikzpicture} 
\begin{axis}[
name=MyAxis,
xlabel={$x$},
ylabel={$y$},
xmin=0,
xmax=25,
ymin=0,
ymax=21,
axis y line=center, 
axis x line=center,
ytick=\empty,
xtick=\empty,
legend entries={$y(x)$},
legend pos=north east]
\addlegendimage{no marks,dashed,very thick,gray}
]
\addplot[gray,dashed,domain=0:21,samples=200,very thick]{(-5/4)*21^(3/5)*x^(2/5)+5*x^(8/5)/(16*21^(3/5))+315/16};
\addplot[draw=none,
          postaction={decorate,decoration={text effects along path,
              text={Increasing values of s},raise=-0.5ex,
              text align=center,
              text effects/.cd, 
              text along path,
              every character/.style={text=blue}
              }},
              domain=0:17.5,
    ] {(-5/4)*21^(3/5)* (x-1.31684)^(2/5)+(5*(x-1.31684)^(8/5))/(16*21^(3/5))+(315/16)+1.15223};
\addplot[stealth-,blue,very thick,samples=200,domain=2:3]{(-5/4)*21^(3/5)* (x-1.31684)^(2/5)+(5*(x-1.31684)^(8/5))/(16*21^(3/5))+(315/16)+1.15223};
\addplot[blue,very thick,samples=200,domain=11.75:14]{(-5/4)*21^(3/5)* (x-1.31684)^(2/5)+(5*(x-1.31684)^(8/5))/(16*21^(3/5))+(315/16)+1.15223};
\end{axis}
\draw (0,0) node [below left] {$0$};
\draw [anchor=base, very thick] (4ex,-3ex)--(10ex,-3ex) node [right] {Increasing values of $x$} [-stealth, xshift=28ex](6ex,-3ex)--(12ex,-3ex);
\end{tikzpicture}
\caption{Inverse correlation between $s$ and $x$}
\label{ref:fig2}
\end{figure}

This is the output: enter image description here

As you might see, I found a way to add a label for the origin and shorten the arrows, but haven't been able to type the blue 's' in mathmode. Again, any help is highly appreciated :)

8
  • 2
    The yshift accounts for the lopsidedness. Try preaction instead. Commented May 31, 2021 at 19:18
  • 2
    Did you check this answer?
    – SebGlav
    Commented Jun 1, 2021 at 19:07
  • Do you need the construction in this case only? Or do you plan to use it in more complicated cases?
    – Daniel N
    Commented Jun 4, 2021 at 6:38
  • @JohnKormylo I'm sorry, I don't quite know how to do that. Could you give me more details or recommend me some documentation to learn, please?
    – achaverra
    Commented Jun 4, 2021 at 20:29
  • 1
    Replies on this site are only for answers. If you want to ask your question in another way, you would ask another one (and maybe delete this one).
    – SebGlav
    Commented Jun 4, 2021 at 20:47

1 Answer 1

4

enter image description here

Updated answer

I propose a construction of the graph based on plot rather than addplot from pgfplots.

  • The function must be written differently, using pow(x, y) for x at the power y.

  • I put the text above the arrow on the left and in the arrow on the right (the update). The decorations are slightly different: the first is based on text along path and the second on text effects along path (as in your work). The math mode for s (in fact \itshape) is achieved through the option character <number>.

  • There is a second argument for both decorations; for the former, the second argument sets the color; for the latter, it sets the character which appears in italic.

  • (Side remark) Looking only at the image, it is not clear what is meant by s. You should say it at least in the figure's caption.

The code

\documentclass[11pt, margin=.7cm]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{decorations.text, arrows.meta}
\begin{document}

\tikzset{%
  fancy text/.style 2 args={%
    postaction={
      decorate, decoration={%
        text along path,
        text={|\small|#1},
        text align=center,
        text color=#2,
        raise=.7ex,
      }
    }
  },
  over/.style 2 args={%
    postaction={%
      decorate, decoration={%
        text effects along path,
        text={#1},
        text align=center,
        text effects/.cd, 
        text along path,
        characters={fill=white, yshift=-.5ex},
        character #2/.style={font=\itshape}
      }
    }
  }
}
\begin{tikzpicture}[scale=.3, every node/.style={scale=.8}]
  % axes
  \draw[->] (0, 0) -- (22, 0) node[below=1ex] {$x$};
  \draw[->] (0, 0) -- (0, 21) node[left=1ex] {$y$};
  \path (0, 0) node[below left] {$O$};

  % curve
  \draw[blue!70!black, very thick, variable=\t, domain=0:21, samples=200]
  plot (\t, {
    (-5/4)*pow(21, 3/5)*pow(\t, 2/5)
    +5/16*pow(\t, 8/5)/pow(21, 3/5)
    +315/16
  });

  % arrows for s and x
  \draw[blue, thin, variable=\t, domain=2:14, samples=200,
  arrows={Latex-},
  fancy text={increasing values of |\scriptsize|{$s$}}{blue}]
  plot (\t, {
    (-5/4)*pow(21, 3/5)*pow(\t -1.3, 2/5)
    +5/16*pow(\t -1.3, 8/5)/pow(21, 3/5)
    +315/16 +1.15
  });

  \draw[blue, thin, arrows={-Latex},
  fancy text={increasing values of |\scriptsize|{$x$}}{blue}]
  (2, -3) -- ++(13, 0);

  % label
  \draw[blue!70!black, very thick] (17.5, 18) -- ++(3, 0)
  node[right] {$y(x)$};
  \draw[very thin] (17, 17) rectangle ++(6.25, 2);
\end{tikzpicture}
\quad
\begin{tikzpicture}[scale=.3, every node/.style={scale=.8}]
  % axes
  \draw[->] (0, 0) -- (22, 0) node[below=1ex] {$x$};
  \draw[->] (0, 0) -- (0, 21) node[left=1ex] {$y$};
  \path (0, 0) node[below left] {$O$};

  % curve
  \draw[blue!70!black, very thick, variable=\t, domain=0:21, samples=100]
  plot (\t, {
    (-5/4)*pow(21, 3/5)*pow(\t, 2/5)
    +5/16*pow(\t, 8/5)/pow(21, 3/5)
    +315/16
  });

  % arrows for s and x
  \draw[blue, thin, variable=\t, domain=2:14, samples=100,
  arrows={Latex-},
  over={~increasing values of s~}{23}]
  plot (\t, {
    (-5/4)*pow(21, 3/5)*pow(\t -1.3, 2/5)
    +5/16*pow(\t -1.3, 8/5)/pow(21, 3/5)
    +315/16 +1.15
  });

  \draw[blue, thin, arrows={-Latex},
  over={~increasing values of x~}{23}]
  (1, -3) -- ++(15, 0);

  % label
  \draw[blue!70!black, very thick] (17.5, 18) -- ++(3, 0)
  node[right] {$y(x)$};
  \draw[very thin] (17, 17) rectangle ++(6.25, 2);
\end{tikzpicture}
\quad
\begin{tikzpicture}[scale=.3, every node/.style={scale=.8}]
  % axes
  \draw[->] (0, 0) -- (22, 0) node[below=1ex] {$x$};
  \draw[->] (0, 0) -- (0, 21) node[left=1ex] {$y$};
  \path (0, 0) node[below left] {$O$};

  % curve
  \draw[blue!70!black, very thick, variable=\t, domain=0:21, samples=100]
  plot (\t, {
    (-5/4)*pow(21, 3/5)*pow(\t, 2/5)
    +5/16*pow(\t, 8/5)/pow(21, 3/5)
    +315/16
  });

  % arrows for s and x
  \draw[blue, thin, variable=\t, domain=2:14, samples=100,
  arrows={Latex-},
  over={~increasing values of s~}{23}]
  plot (\t, {
    (-5/4)*pow(21, 3/5)*pow(\t -1.3, 2/5)
    +5/16*pow(\t -1.3, 8/5)/pow(21, 3/5)
    +315/16 +1.15
  });

  \draw[blue, thin, arrows={-Latex},
  over={~increasing values of x~}{23}]
  (1, -3) -- ++(15, 0);

  % label
  \draw[blue!70!black, very thick] (17.5, 18) -- ++(3, 0)
  node[right] {$y(x)$};
  \draw[very thin] (17, 17) rectangle ++(6.25, 2);
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .