# Inserting the slope inside a log-log chart with pgfplots

I would like to insert the slope inside a log-log chart with pgfplots. I checked both the pgfplots documentation and the net, but I didn't find something helpful about this problem.

Here is the MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\usepackage{amsmath,amssymb}
\begin{document}

\begin{tikzpicture}
\begin{loglogaxis}[
xlabel={x},
ylabel={y},
xmin=0.01, xmax=0.10,
ymin=200, ymax=750,
width=.8\columnwidth,
/pgfplots/log ticks with fixed point,
/pgfplots/ytick={250,300,400,...,700}]
]
coordinates{
(0.0090,   704.11)
(0.0300,   476.05)
(0.0600,   379.49)
(0.0900,   332.26)
(0.1005,   320.46)
};
\end{loglogaxis}
\end{tikzpicture}
\end{document}

-
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Apr 2 '12 at 22:28

## migrated from stackoverflow.comApr 2 '12 at 22:11

This question came from our site for professional and enthusiast programmers.

You can calculate the linear regression to a set of points using the key y={create col/linear regression={y=<column name>}} in the plot options. This allows you to plot a straight line for the slope, and using coordinate [pos=<fraction>] (<name>) you can save points on this line that can be used for drawing a slope triangle.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.5.1}
\usepackage{amsmath,amssymb}
\begin{document}

0.0090  704.11
0.0300  476.05
0.0600  359.49
0.0900  332.26
0.1005  320.46
}\datatable

\begin{tikzpicture}
\begin{loglogaxis}[
xlabel={x},
ylabel={y},
xmin=0.01, xmax=0.10,
ymin=200, ymax=750,
width=.8\columnwidth,
/pgfplots/log ticks with fixed point,
/pgfplots/ytick={200,300,...,700}]
]
\addplot [only marks, red] table {\datatable}; % plot the data
table [y={create col/linear regression={y=1}}] {\datatable} % calculate and plot the regression line
coordinate [pos=0.25] (A) % save two points on the regression line for drawing the slope triangle
coordinate [pos=0.4] (B)
;
\xdef\slope{\pgfplotstableregressiona} % save the slope parameter
\draw (A) -| (B)  % draw the opposite and adjacent sides of the triangle
node [pos=0.25, anchor=south] {1} % label the horizontal line
node [pos=0.75,anchor=west] {\pgfmathprintnumber{\slope}} label the vertical line
;
\end{loglogaxis}
\end{tikzpicture}
\end{document}

-
Thank you very much Jake, that's exactly what I was looking for. I just made some minor changes to your code, to adapt it better to my situation: \addplot [only marks,fill=gray] table {\datatable}; \addplot [black] table [y={create col/linear regression={y=1}}] {\datatable} coordinate [pos=0.425] (A) coordinate [pos=0.575] (B) ; \xdef\slope{\pgfplotstableregressiona} \draw (A) |- (B) node at (axis cs:0.027,395) {\pgfmathprintnumber{\slope}} node at (axis cs:0.051,340) {$1$} –  mp87 Apr 3 '12 at 10:31