Line Plots in PGFPlots
Create line plots from functions, data points, and external files.
Plotting Functions
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
domain=-2:2
]
% Simple polynomial
\addplot {x^2};
% Explicit domain
\addplot[domain=-3:3] {x^3};
% Trigonometric (use deg() for degree input)
\addplot[domain=-pi:pi] {sin(deg(x))};
% Exponential
\addplot {exp(x)};
% Logarithmic
\addplot[domain=0.1:10] {ln(x)};
\end{axis}
\end{tikzpicture}Plotting Data Points
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Value}
]
% Inline coordinates
\addplot coordinates {
(0, 1)
(1, 2.5)
(2, 3.2)
(3, 4.1)
(4, 5.8)
(5, 6.2)
};
% Table format
\addplot table {
x y
0 0
1 1
2 4
3 9
4 16
};
\end{axis}
\end{tikzpicture}Line Styles
\begin{tikzpicture}
\begin{axis}[domain=0:4]
% Solid line (default)
\addplot[solid, blue] {x};
% Dashed line
\addplot[dashed, red] {x + 1};
% Dotted line
\addplot[dotted, green] {x + 2};
% Dash-dot
\addplot[dashdotted, orange] {x + 3};
% Custom dash pattern
\addplot[dash pattern=on 4pt off 2pt on 1pt off 2pt, purple] {x + 4};
\end{axis}
\end{tikzpicture}Line Thickness
\begin{tikzpicture}
\begin{axis}[domain=0:2]
\addplot[ultra thin] {x};
\addplot[very thin] {x + 0.5};
\addplot[thin] {x + 1};
\addplot[semithick] {x + 1.5};
\addplot[thick] {x + 2};
\addplot[very thick] {x + 2.5};
\addplot[ultra thick] {x + 3};
% Custom width
\addplot[line width=2pt] {x + 3.5};
\end{axis}
\end{tikzpicture}Adding Markers
\begin{tikzpicture}
\begin{axis}
% Circle markers
\addplot[mark=o] coordinates {(0,0) (1,1) (2,4)};
% Filled circles
\addplot[mark=*] coordinates {(0,1) (1,2) (2,5)};
% Square markers
\addplot[mark=square] coordinates {(0,2) (1,3) (2,6)};
% Triangle markers
\addplot[mark=triangle] coordinates {(0,3) (1,4) (2,7)};
% Diamond markers
\addplot[mark=diamond] coordinates {(0,4) (1,5) (2,8)};
% Plus markers
\addplot[mark=+] coordinates {(0,5) (1,6) (2,9)};
% Cross markers
\addplot[mark=x] coordinates {(0,6) (1,7) (2,10)};
\end{axis}
\end{tikzpicture}Marker Customization
\begin{tikzpicture}
\begin{axis}
% Marker size
\addplot[mark=*, mark size=4pt] coordinates {(0,0) (1,1) (2,4)};
% Marker color different from line
\addplot[blue, mark=*, mark options={fill=red}]
coordinates {(0,1) (1,2) (2,5)};
% Markers only at some points
\addplot[mark=o, mark repeat=2] {x^2};
% Markers every nth point
\addplot[mark=square, mark phase=1, mark repeat=3] {x^2 + 2};
% Only markers, no line
\addplot[only marks, mark=*] coordinates {(0.5,3) (1.5,4) (2,6)};
\end{axis}
\end{tikzpicture}Smooth Lines
\begin{tikzpicture}
\begin{axis}
% Sharp corners (default)
\addplot coordinates {(0,0) (1,2) (2,1) (3,3) (4,2)};
% Smoothed curve
\addplot[smooth] coordinates {(0,1) (1,3) (2,2) (3,4) (4,3)};
% Smoothed with tension control
\addplot[smooth, tension=0.5] coordinates {(0,2) (1,4) (2,3) (3,5) (4,4)};
\end{axis}
\end{tikzpicture}Multiple Line Plots
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
legend pos=north west,
grid=major
]
\addplot[blue, thick] {x};
\addplot[red, thick, dashed] {x^2};
\addplot[green!60!black, thick, dotted] {sqrt(x)};
\addplot[orange, thick, mark=*] coordinates {
(0,0) (1,1) (2,1.5) (3,2) (4,2.2)
};
\legend{Linear, Quadratic, Square Root, Experimental}
\end{axis}
\end{tikzpicture}Samples and Resolution
\begin{tikzpicture}
\begin{axis}[domain=0:2*pi]
% Low resolution (jagged)
\addplot[samples=10, blue] {sin(deg(x))};
% Medium resolution
\addplot[samples=50, red] {sin(deg(x)) + 2};
% High resolution (smooth)
\addplot[samples=200, green] {sin(deg(x)) + 4};
\end{axis}
\end{tikzpicture}Parametric Plots
\begin{tikzpicture}
\begin{axis}[
axis equal,
xlabel={$x$},
ylabel={$y$}
]
% Circle (parametric)
\addplot[domain=0:360, samples=100, blue, thick]
({cos(x)}, {sin(x)});
% Ellipse
\addplot[domain=0:360, samples=100, red, thick]
({2*cos(x)}, {sin(x)});
% Lissajous curve
\addplot[domain=0:360, samples=200, green!60!black, thick]
({sin(2*x)}, {sin(3*x)});
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Scatter Plots - Point data visualization
- Bar Charts - Categorical data
- Legends - Customize legends
Line Plots in PGFPlots
Create line plots from functions, data points, and external files.
Plotting Functions
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
domain=-2:2
]
% Simple polynomial
\addplot {x^2};
% Explicit domain
\addplot[domain=-3:3] {x^3};
% Trigonometric (use deg() for degree input)
\addplot[domain=-pi:pi] {sin(deg(x))};
% Exponential
\addplot {exp(x)};
% Logarithmic
\addplot[domain=0.1:10] {ln(x)};
\end{axis}
\end{tikzpicture}Plotting Data Points
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Value}
]
% Inline coordinates
\addplot coordinates {
(0, 1)
(1, 2.5)
(2, 3.2)
(3, 4.1)
(4, 5.8)
(5, 6.2)
};
% Table format
\addplot table {
x y
0 0
1 1
2 4
3 9
4 16
};
\end{axis}
\end{tikzpicture}Line Styles
\begin{tikzpicture}
\begin{axis}[domain=0:4]
% Solid line (default)
\addplot[solid, blue] {x};
% Dashed line
\addplot[dashed, red] {x + 1};
% Dotted line
\addplot[dotted, green] {x + 2};
% Dash-dot
\addplot[dashdotted, orange] {x + 3};
% Custom dash pattern
\addplot[dash pattern=on 4pt off 2pt on 1pt off 2pt, purple] {x + 4};
\end{axis}
\end{tikzpicture}Line Thickness
\begin{tikzpicture}
\begin{axis}[domain=0:2]
\addplot[ultra thin] {x};
\addplot[very thin] {x + 0.5};
\addplot[thin] {x + 1};
\addplot[semithick] {x + 1.5};
\addplot[thick] {x + 2};
\addplot[very thick] {x + 2.5};
\addplot[ultra thick] {x + 3};
% Custom width
\addplot[line width=2pt] {x + 3.5};
\end{axis}
\end{tikzpicture}Adding Markers
\begin{tikzpicture}
\begin{axis}
% Circle markers
\addplot[mark=o] coordinates {(0,0) (1,1) (2,4)};
% Filled circles
\addplot[mark=*] coordinates {(0,1) (1,2) (2,5)};
% Square markers
\addplot[mark=square] coordinates {(0,2) (1,3) (2,6)};
% Triangle markers
\addplot[mark=triangle] coordinates {(0,3) (1,4) (2,7)};
% Diamond markers
\addplot[mark=diamond] coordinates {(0,4) (1,5) (2,8)};
% Plus markers
\addplot[mark=+] coordinates {(0,5) (1,6) (2,9)};
% Cross markers
\addplot[mark=x] coordinates {(0,6) (1,7) (2,10)};
\end{axis}
\end{tikzpicture}Marker Customization
\begin{tikzpicture}
\begin{axis}
% Marker size
\addplot[mark=*, mark size=4pt] coordinates {(0,0) (1,1) (2,4)};
% Marker color different from line
\addplot[blue, mark=*, mark options={fill=red}]
coordinates {(0,1) (1,2) (2,5)};
% Markers only at some points
\addplot[mark=o, mark repeat=2] {x^2};
% Markers every nth point
\addplot[mark=square, mark phase=1, mark repeat=3] {x^2 + 2};
% Only markers, no line
\addplot[only marks, mark=*] coordinates {(0.5,3) (1.5,4) (2,6)};
\end{axis}
\end{tikzpicture}Smooth Lines
\begin{tikzpicture}
\begin{axis}
% Sharp corners (default)
\addplot coordinates {(0,0) (1,2) (2,1) (3,3) (4,2)};
% Smoothed curve
\addplot[smooth] coordinates {(0,1) (1,3) (2,2) (3,4) (4,3)};
% Smoothed with tension control
\addplot[smooth, tension=0.5] coordinates {(0,2) (1,4) (2,3) (3,5) (4,4)};
\end{axis}
\end{tikzpicture}Multiple Line Plots
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
legend pos=north west,
grid=major
]
\addplot[blue, thick] {x};
\addplot[red, thick, dashed] {x^2};
\addplot[green!60!black, thick, dotted] {sqrt(x)};
\addplot[orange, thick, mark=*] coordinates {
(0,0) (1,1) (2,1.5) (3,2) (4,2.2)
};
\legend{Linear, Quadratic, Square Root, Experimental}
\end{axis}
\end{tikzpicture}Samples and Resolution
\begin{tikzpicture}
\begin{axis}[domain=0:2*pi]
% Low resolution (jagged)
\addplot[samples=10, blue] {sin(deg(x))};
% Medium resolution
\addplot[samples=50, red] {sin(deg(x)) + 2};
% High resolution (smooth)
\addplot[samples=200, green] {sin(deg(x)) + 4};
\end{axis}
\end{tikzpicture}Parametric Plots
\begin{tikzpicture}
\begin{axis}[
axis equal,
xlabel={$x$},
ylabel={$y$}
]
% Circle (parametric)
\addplot[domain=0:360, samples=100, blue, thick]
({cos(x)}, {sin(x)});
% Ellipse
\addplot[domain=0:360, samples=100, red, thick]
({2*cos(x)}, {sin(x)});
% Lissajous curve
\addplot[domain=0:360, samples=200, green!60!black, thick]
({sin(2*x)}, {sin(3*x)});
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Scatter Plots - Point data visualization
- Bar Charts - Categorical data
- Legends - Customize legends