6

I am trying to use pgfplots to add slope triangles to several plots on the same axis which look like the following:

current output from <code>pdflatex</code>

I thought I was in luck to see parts of this process explained specifically in the pgfplots documentation, but as you can see from the output, the right sides of the slope triangles are not aligned. I originally thought this was related to the coordinate [pos=...] command using arc length instead of x (or y) distance, but I don't think this can explain why the middle triangle is the furthest to the right.

Here is the code (some non-essentials removed):

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}       

\begin{document}
\begin{tikzpicture}

\begin{loglogaxis}[%
width=0.962707\textwidth,
height=0.75\textwidth,
xlabel={mesh size, $h^e$},
xmax=0.1,
ylabel={$L_2$ Error}
]
\addplot table[row sep=crcr]{%
0.052   0.08922\\
0.026   0.03718\\
0.013   0.01128\\
0.0065  0.00301\\
0.00325 0.00076\\
}
% add slope triangle
coordinate [pos=0.95] (A)
coordinate [pos=0.75] (B)
;
\draw (A) -| (B)
node [pos=.75,anchor=west]{$2.0$};

\addplot table[row sep=crcr]{%
0.052   0.02903\\
0.026   0.00918\\
0.013   0.00219\\
0.0065  0.00050\\
0.00325 0.00012\\
}
% add slope triangle
coordinate [pos=0.95] (A)
coordinate [pos=0.75] (B)
;
\draw (A) -| (B)
node [pos=.75,anchor=west]{$2.0$};

\end{loglogaxis}
\end{tikzpicture}%
\end{document}

I am aware that the tables are ordered in reverse (right to left), but changing this did not seem to make a difference. Can someone please explain what is going on and how to correct it

Also, if it is possible to compute slope values of the labeled segment automatically, that would be extremely helpful, as I have several of these plots each with unique data.

1 Answers1

3

Note that pos key finds the points by considering the total length of the line. Here since the plots are of different lengths, as pos value increases, the points differ in position.

You can use intersections library, draw two vertical lines and find their intersections with the plots to draw the slope.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}

\begin{loglogaxis}[%
width=0.962707\textwidth,
height=0.75\textwidth,
xlabel={mesh size, $h^e$},
xmax=0.1,
ylabel={$L_2$ Error}
]
\path[name path global = vert1] (rel axis cs: 0.12,   0) -- (rel axis cs: 0.12,   1);
\path[name path global = vert2] (rel axis cs: 0.25,   0) -- (rel axis cs: 0.25,   1);
\addplot+[name path global =plot1] table[row sep=crcr]{%
0.052   0.08922\\
0.026   0.03718\\
0.013   0.01128\\
0.0065  0.00301\\
0.00325 0.00076\\
};
% add slope triangle
\path [name intersections={of = vert1 and plot1, by=a}];
\path [name intersections={of = vert2 and plot1, by=b}];

\draw (a) -| (b)node [pos=.75,anchor=west]{$2.0$};

\addplot+[name path global=plot2] table[row sep=crcr]{%
0.052   0.02903\\
0.026   0.00918\\
0.013   0.00219\\
0.0065  0.00050\\
0.00325 0.00012\\
};
% add slope triangle
\path [name intersections={of = vert1 and plot2, by=a}];
\path [name intersections={of = vert2 and plot2, by=b}];
\draw (a) -| (b)
node [pos=.75,anchor=west]{$2.0$};

\end{loglogaxis}
\end{tikzpicture}%
\end{document}

enter image description here

  • This works great, thank you. Do you have any tips for automatically computing the slope? In the PGFplots manual they use an \xdef\slope{\pgfplotstableregressiona} to save the slope, but I don't have a linear regression. I'm only interested in the slope of a single segment (as pictured). – David McWilliams Mar 24 '15 at 11:42
  • @DavidMcWilliams: Calculation of slopes automatically for non linear graphs is not straight forward and hence deserves to be a separate question. However, you may plot that particular segment separately and use \xdef\slope{\pgfplotstableregressiona} for that segment. I mean use another \addplot for that segment alone. –  Mar 24 '15 at 12:13