如何在具有對數 x 軸刻度的圖表上新增連接兩個座標的線?

如何在具有對數 x 軸刻度的圖表上新增連接兩個座標的線?

我正在嘗試添加穿過點 (1000,-8.5) 和 (10000,-27.79) 的線。這是我目前擁有的乳膠。

\begin{figure}[h!]
\begin{center}
\begin{tikzpicture}
\begin{axis}[
    xmode=log,
    xlabel={Frequency (Hz)},
    ylabel={Gain (db)},
    xmin=0, xmax=20000,
    ymin=-30, ymax=10,
    xtick={0,100,1000,10000},
    ytick={10,0,-10,-20,-30},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
]

\addplot[
    color=blue,
    mark=square,
    only marks,
    ]
    coordinates {
    (100,-0.18)(1000,-8.5)(2000,-14.15)(4000,-19.83)(10000,-27.79)
    };

% add plot of equation going through points (1000,-8.5) and (10000,-27.79) here

\end{axis}
\end{tikzpicture}
\end{center}
\end{figure}

我猜測通過這兩個點的所有方程式都沒有正確顯示在圖上,因為對數刻度。我嘗試過 49.37-19.29*x 但不起作用。在計算連接兩點的線之前應用 log(x) 可得出方程式。如何得到一條穿過這些點的線?

答案1

怎麼樣

\documentclass[tikz,border=3.14mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmode=log,
    xlabel={Frequency (Hz)},
    ylabel={Gain (db)},
    xmin=0, xmax=20000,
    ymin=-30, ymax=10,
    xtick={0,100,1000,10000},
    ytick={10,0,-10,-20,-30},
    legend pos=north west,
    ymajorgrids=true,
    grid style=dashed,
    legend pos=north east
]

\addplot[
    color=blue,
    mark=square,
    only marks,
    ]
    coordinates {
    (100,-0.18)(1000,-8.5)(2000,-14.15)(4000,-19.83)(10000,-27.79)
    };
\addlegendentry{data}

\addplot[color=orange,no marks] coordinates {(1000,-8.5) (10000,-27.79)};
\addlegendentry{fit}
\end{axis}
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容