跳到主要內容

用 pgfplots 繪製誤差線

在 \addplot 的選項中加上 error bars/.cd, y dir=both, y explicit,並在每個點後面以 +- (0, error) 寫出誤差,或用 y error= 從表格欄位讀取誤差。

隨資料點寫出的誤差

誤差線是針對每個 \addplot 開啟的。error bars/.cd 後面的鍵指定要往哪個方向畫,以及數值從哪裡來。使用 y explicit 時,每個點在 +- 後面帶有自己的誤差。

來自座標的對稱 y 誤差

LaTeX導言區: \usepackage{pgfplots}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \begin{axis}[xlabel={Length (m)}, ylabel={Period (s)}]
    \addplot+[error bars/.cd, y dir=both, y explicit]
      coordinates {
        (0.2,0.90) +- (0,0.05)
        (0.4,1.27) +- (0,0.04)
        (0.6,1.55) +- (0,0.06)
        (0.8,1.80) +- (0,0.05)
        (1.0,2.01) +- (0,0.07)
      };
  \end{axis}
\end{tikzpicture}

在 +- (ex, ey) 中,第一個數字是 x 誤差,第二個是 y 誤差。只有你開啟的方向才會繪製。

來自表格欄位的誤差

資料是表格時,用 y error= 指定誤差欄位。檔案也一樣,例如 table[x=t, y=v, y error=dv, col sep=comma] {data.csv}。

從欄位讀取的 y 誤差

LaTeX導言區: \usepackage{pgfplots}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \begin{axis}[xlabel={Dose}, ylabel={Response}, ymin=0]
    \addplot+[only marks, error bars/.cd, y dir=both, y explicit]
      table[x=dose, y=mean, y error=sd] {
        dose mean sd
        1    2.1  0.3
        2    3.4  0.4
        4    5.0  0.6
        8    6.2  0.5
      };
  \end{axis}
\end{tikzpicture}

單側誤差與固定誤差

y dir=plus 或 y dir=minus 只會畫出上半部或下半部。除了明確寫出數值之外,y fixed=0.2 會讓每個點都有相同的誤差,y fixed relative=0.1 則讓誤差為該點數值的 10%。

只有上方誤差,以及相對誤差

LaTeX導言區: \usepackage{pgfplots}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \begin{axis}[ymin=0, legend pos=north west]
    \addplot+[error bars/.cd, y dir=plus, y fixed=0.4]
      coordinates {(1,2) (2,3) (3,3.5) (4,4.4)};
    \addplot+[error bars/.cd, y dir=both, y fixed relative=0.1]
      coordinates {(1,1) (2,1.6) (3,2.2) (4,2.5)};
    \legend{fixed $\pm 0.4$ (upper), relative 10\%}
  \end{axis}
\end{tikzpicture}
由 Scissor LaTeX 排版: 只有上方誤差,以及相對誤差

上下不同的誤差

用 += (0, up) 寫出上方誤差,用 -= (0, down) 寫出下方誤差。

不對稱誤差

LaTeX導言區: \usepackage{pgfplots}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \begin{axis}[width=7cm, height=5cm, xmin=0.5, xmax=3.5]
    \addplot+[only marks, error bars/.cd, y dir=both, y explicit]
      coordinates {
        (1,2.0) += (0,0.5) -= (0,0.1)
        (2,3.0) += (0,0.2) -= (0,0.6)
        (3,2.6) += (0,0.3) -= (0,0.3)
      };
  \end{axis}
\end{tikzpicture}

x 和 y 方向的誤差

用 x dir 和 y dir 開啟兩個方向。error bar style 設定誤差線的樣式。

兩個方向的誤差線

LaTeX導言區: \usepackage{pgfplots}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \begin{axis}[xlabel=$x$, ylabel=$y$]
    \addplot+[only marks, mark=square*,
              error bars/.cd,
                x dir=both, x explicit,
                y dir=both, y explicit,
                error bar style={red, thick}]
      coordinates {
        (1.0,1.2) +- (0.15,0.20)
        (2.0,2.1) +- (0.20,0.15)
        (3.0,2.8) +- (0.10,0.30)
      };
  \end{axis}
\end{tikzpicture}

用 fill between 繪製誤差帶

對於連續的不確定度,可以為上下兩條曲線之間的區域加上陰影。載入 fillbetween 函式庫,用 name path 為兩條曲線命名,再填滿它們之間的區域。

曲線周圍的陰影帶

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[domain=0:4, samples=40, xlabel=$t$, ylabel=$y$]
    \addplot[name path=upper, draw=none] {exp(-0.5*x) + 0.1 + 0.05*x};
    \addplot[name path=lower, draw=none] {exp(-0.5*x) - 0.1 - 0.05*x};
    \addplot[blue!20] fill between[of=upper and lower];
    \addplot[blue, thick] {exp(-0.5*x)};
  \end{axis}
\end{tikzpicture}
\end{document}

誤差線選項

選項作用在 Scissor LaTeX 中
y dir=both、plus、minus、none要畫出誤差線的哪一半(也有 x dir)可以使用
y explicit、x explicit誤差來自座標中的 +- 或表格欄位可以使用
y error=col、x error=col、y error index=n含有誤差的表格欄位可以使用
+= (ex,ey)、-= (ex,ey)在座標中分別寫出上方和下方誤差可以使用
y fixed=v、y fixed relative=r每個點使用相同的誤差可以使用
error bar style={..}誤差線的顏色和線寬可以使用
error mark=..、error mark options={..}兩端的端帽可以接受,但端帽一律是短線

與 TeX 安裝環境的差異

pgfplots 手冊在誤差線一節中說明了誤差線:ctan.org/pkg/pgfplots。

另請參閱長條圖、散佈圖和從 CSV 讀取資料。

常見問題

怎麼在 pgfplots 中加上誤差線?

寫 \addplot+[error bars/.cd, y dir=both, y explicit] coordinates {(1,2) +- (0,0.3) ...};。+- 後面的第二個數字是 y 誤差。

怎麼從 CSV 檔案讀取誤差線?

在表格選項中加上 y error=column,並在誤差線選項中加上 y explicit:table[x=a, y=b, y error=c, col sep=comma] {file.csv}。

誤差線可以上下不同嗎?

可以。在點的後面寫 += (0, up) 和 -= (0, down),取代 +-,例如 (1,2) += (0,0.5) -= (0,0.1)。

本頁的每個範例都在建置網站時由 Scissor LaTeX 排版。 最後更新:2026-11-07