用 pgfplots 繪製折線圖
在 axis 中為每條線加上一個 \addplot;用 red、dashed、thick 或 mark=square* 等選項設定各自的樣式,或讓循環列表替你選擇樣式。
一個座標軸中的多條線
每個 \addplot 都會加入一個系列。沒有選項時,各系列會依序從循環列表取得樣式。使用 \addplot+[...],就能保留該樣式並只修改其中一部分。
使用預設循環列表的三個系列
\begin{tikzpicture}
\begin{axis}[xlabel=Week, ylabel=Orders, legend pos=north west]
\addplot coordinates {(1,12) (2,18) (3,25) (4,31) (5,44)};
\addplot coordinates {(1,10) (2,14) (3,15) (4,21) (5,24)};
\addplot+[dashed] coordinates {(1,5) (2,9) (3,8) (4,12) (5,13)};
\legend{Erika, Hans, John}
\end{axis}
\end{tikzpicture}線條樣式與標記
\addplot 後面的選項設定顏色、線條和標記。你可以自由組合。
| 選項 | 值 | 在 Scissor LaTeX 中 |
|---|---|---|
| 顏色 | red、blue!60!black、color=teal,任何 xcolor 顏色名稱或混色 | 可以使用 |
| 線寬 | ultra thin、very thin、thin、semithick、thick、very thick、ultra thick、line width=1.5pt | 可以使用 |
| 虛線樣式 | solid、dashed、dotted、densely dashed、loosely dotted、dashdotted、dash pattern=on 4pt off 2pt | 可以使用 |
mark= | *、o、square*、square、triangle*、triangle、diamond*、diamond、x、+、star、pentagon*、asterisk、otimes*、|、-、none | 可以使用 |
mark size=3pt | 標記的半徑 | 可以使用 |
mark options={fill=white} | 只設定標記的樣式 | 可以使用 |
mark repeat=2 | 每隔一個點加一個標記 | 可以使用 |
no marks、only marks | 只有線條、只有標記 | 可以使用 |
顏色、虛線樣式和標記
\begin{tikzpicture}
\begin{axis}[domain=0:4, samples=9, legend pos=outer north east]
\addplot[blue, thick, mark=*] {x};
\addplot[red, dashed, mark=square*, mark options={fill=white}] {x^1.5/2};
\addplot[teal, densely dotted, very thick] {sqrt(x)};
\addplot[orange, mark=triangle*, mark size=3pt, only marks] {4 - x};
\legend{linear, power, root, falling}
\end{axis}
\end{tikzpicture}和 pgfplots 一樣,即使系列是虛線,標記也一律以實線繪製。
選擇循環列表
循環列表是不加選項的 \addplot 指令依序使用的樣式序列。用 cycle list name 選擇內建的列表,或用 cycle list 自己撰寫。
black white 循環列表,以及自訂的列表
\begin{tikzpicture}
\begin{axis}[cycle list name=black white, width=6.5cm, height=5cm, title={black white}]
\addplot coordinates {(0,0) (1,1) (2,4)};
\addplot coordinates {(0,1) (1,2) (2,2)};
\addplot coordinates {(0,2) (1,0) (2,1)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[cycle list={{red, thick}, {blue, dashed}}, width=6.5cm, height=5cm, title={custom}]
\addplot coordinates {(0,0) (1,1) (2,4)};
\addplot coordinates {(0,1) (1,2) (2,2)};
\end{axis}
\end{tikzpicture}可以使用的內建名稱有:color(預設)、exotic、black white、mark list 和 linestyles。在導言區使用 \pgfplotscreateplotcyclelist{name}{...},可以定義一個能重複使用的列表。
平滑曲線與階梯
smooth 會畫出通過各點的曲線,而不是直線段;tension(預設 0.55)控制曲線的圓滑程度。const plot 會畫出階梯函數,每個值一直維持到下一個點。
smooth 和 const plot
\begin{tikzpicture}
\begin{axis}[ymin=0, legend pos=north west]
\addplot+[smooth] coordinates {(0,1) (1,2.5) (2,1.8) (3,3.2) (4,2.4)};
\addplot+[const plot] coordinates {(0,0.5) (1,1) (2,1.5) (3,1.2) (4,2)};
\legend{smooth, const plot}
\end{axis}
\end{tikzpicture}以度和弧度表示的函數
公式使用 pgfmath:+ - * / ^、括號、sqrt、exp、ln、abs、min、max、sin、cos、tan 等,常數有 pi 和 e。角度以度為單位,所以請用 deg(x) 把弧度換算成度。
一個週期內的 sin 和 cos
\begin{tikzpicture}
\begin{axis}[domain=0:2*pi, samples=80, xlabel=$t$,
xtick={0,1.5708,3.1416,4.7124,6.2832},
xticklabels={$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$},
legend pos=south west]
\addplot[blue, thick] {sin(deg(x))};
\addplot[red, thick, dashed] {cos(deg(x))};
\legend{$\sin t$, $\cos t$}
\end{axis}
\end{tikzpicture}參數曲線
要繪製由 x(t) 和 y(t) 定義的曲線,請把兩個公式寫在括號中、以逗號分隔,並設定 variable=t。
利薩茹曲線
\begin{tikzpicture}
\begin{axis}[axis lines=middle, xmin=-1.3, xmax=1.3, ymin=-1.3, ymax=1.3]
\addplot[purple, thick, domain=0:360, samples=120, variable=t]
({cos(t)}, {sin(2*t)});
\end{axis}
\end{tikzpicture}曲線中的斷點
像 1/x 這樣的函數會衝向無限大。restrict y to domain 會捨棄範圍之外的點,unbounded coords=jump 則會在那裡把線斷開,而不是把兩個分支連起來。請把兩者都設定在 \addplot 上。
在零處斷開的 1/x
\begin{tikzpicture}
\begin{axis}[ymin=-10, ymax=10, axis lines=middle]
\addplot[blue, thick, domain=-3:3, samples=200,
restrict y to domain=-10:10, unbounded coords=jump] {1/x};
\end{axis}
\end{tikzpicture}與 TeX 安裝環境的差異
CTAN 上的 pgfplots 手冊說明了每個選項:ctan.org/pkg/pgfplots。只有點而沒有線的圖表,請參閱散佈圖。
另請參閱 pgfplots 基礎、對數座標軸、群組圖和從 CSV 讀取資料。
常見問題
怎麼在 LaTeX 中繪製函數?
在 axis 中寫 \addplot[domain=a:b, samples=n] {formula};,以 x 作為變數,例如 {x^2 - 1}。
怎麼移除線上的標記?
在 \addplot 的選項中加上 no marks,或使用不加 + 的選項(例如 \addplot[blue]),這樣會畫出一條單純的線。
為什麼 sin(x) 看起來幾乎是平的?
pgfplots 以度讀取角度,所以 0 到 6 的 sin(x) 只涵蓋 6 度。請寫 sin(deg(x))。
本頁的每個範例都在建置網站時由 Scissor LaTeX 排版。 最後更新:2026-11-07