使用 pgfplots 在对数对数图中插入斜率

使用 pgfplots 在对数对数图中插入斜率

我想在对数对数图表中插入斜率pgfplots。我检查了pgfplots文档和网络,但没有找到有关此问题的有用信息。

以下是 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}]
     ]
     \addplot
     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}

答案1

您可以使用绘图选项中的键计算一组点的线性回归y={create col/linear regression={y=<column name>}}。这允许您绘制一条直线作为斜率,并且coordinate [pos=<fraction>] (<name>)您可以使用该直线保存可用于绘制斜率三角形的点。

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

\pgfplotstableread{
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
     \addplot [black]
        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 segment
        node [pos=0.75, anchor=west] {\pgfmathprintnumber{\slope}}  % label the vertical segment with the slope of the regression line
     ;
     \end{loglogaxis}
\end{tikzpicture}
\end{document}

相关内容