Scatter Plots in PGFPlots
Visualize data points and correlations with scatter plots.
Basic Scatter Plot
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
title={Basic Scatter Plot}
]
\addplot[only marks] coordinates {
(1, 2.1)
(2, 3.8)
(3, 5.2)
(4, 7.1)
(5, 8.9)
(6, 10.5)
};
\end{axis}
\end{tikzpicture}Marker Styles for Scatter Plots
\begin{tikzpicture}
\begin{axis}[
legend pos=north west
]
% Filled circles
\addplot[only marks, mark=*, blue] coordinates {
(0,0) (1,1) (2,1.5) (3,2.2)
};
% Open circles
\addplot[only marks, mark=o, red] coordinates {
(0,0.5) (1,1.5) (2,2) (3,2.7)
};
% Squares
\addplot[only marks, mark=square*, green!60!black] coordinates {
(0,1) (1,2) (2,2.5) (3,3.2)
};
% Triangles
\addplot[only marks, mark=triangle*, orange] coordinates {
(0,1.5) (1,2.5) (2,3) (3,3.7)
};
\legend{Dataset A, Dataset B, Dataset C, Dataset D}
\end{axis}
\end{tikzpicture}Custom Marker Size
\begin{tikzpicture}
\begin{axis}
% Small markers
\addplot[only marks, mark=*, mark size=1pt, blue] coordinates {
(0,0) (1,1) (2,2)
};
% Medium markers (default)
\addplot[only marks, mark=*, mark size=2pt, red] coordinates {
(0,1) (1,2) (2,3)
};
% Large markers
\addplot[only marks, mark=*, mark size=4pt, green] coordinates {
(0,2) (1,3) (2,4)
};
% Extra large markers
\addplot[only marks, mark=*, mark size=6pt, orange] coordinates {
(0,3) (1,4) (2,5)
};
\end{axis}
\end{tikzpicture}Color-Coded Scatter Plot
\begin{tikzpicture}
\begin{axis}[
colorbar,
colormap/viridis,
title={Color by Value}
]
\addplot[
scatter,
only marks,
scatter src=explicit,
mark=*
] coordinates {
(0, 0) [0]
(1, 1) [1]
(2, 4) [2]
(3, 9) [3]
(4, 16) [4]
(5, 25) [5]
};
\end{axis}
\end{tikzpicture}Variable-Size Markers (Bubble Chart)
\begin{tikzpicture}
\begin{axis}[
title={Bubble Chart},
xlabel={X Value},
ylabel={Y Value}
]
\addplot[
scatter,
only marks,
scatter/@pre marker code/.code={
\pgfmathsetmacro{\markersize}{\pgfplotspointmeta/5}
\edef\markersize{\markersize pt}
\tikzset{mark size=\markersize}
},
scatter/@post marker code/.code={},
mark=*,
fill opacity=0.5,
blue
] table[meta=size] {
x y size
1 2 10
2 4 25
3 3 15
4 5 30
5 4 20
};
\end{axis}
\end{tikzpicture}Scatter Plot with Trend Line
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
legend pos=north west
]
% Data points
\addplot[only marks, mark=*, blue] coordinates {
(1, 2.1) (2, 4.2) (3, 5.8) (4, 8.1)
(5, 9.9) (6, 12.2) (7, 14.0) (8, 16.1)
};
% Linear trend line
\addplot[red, thick, domain=0:9] {2*x};
\legend{Data, $y = 2x$}
\end{axis}
\end{tikzpicture}Multiple Data Series
\begin{tikzpicture}
\begin{axis}[
xlabel={Feature 1},
ylabel={Feature 2},
legend pos=outer north east
]
% Class A
\addplot[only marks, mark=*, blue, mark size=3pt] coordinates {
(1,2) (1.5,2.5) (2,3) (2.5,2.8) (1.8,2.2)
};
% Class B
\addplot[only marks, mark=square*, red, mark size=3pt] coordinates {
(4,5) (4.5,5.5) (5,5) (5.5,5.8) (4.8,5.2)
};
% Class C
\addplot[only marks, mark=triangle*, green!60!black, mark size=3pt] coordinates {
(2.5,5) (3,5.5) (3.5,4.8) (3,4.5) (2.8,5.2)
};
\legend{Class A, Class B, Class C}
\end{axis}
\end{tikzpicture}Scatter Plot from File
% Assuming data.csv contains:
% x,y
% 1,2.1
% 2,3.9
% 3,6.2
% ...
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$}
]
\addplot[only marks, mark=*] table[
col sep=comma,
x=x,
y=y
] {data.csv};
\end{axis}
\end{tikzpicture}Transparent Markers
\begin{tikzpicture}
\begin{axis}[
title={Overlapping Points with Transparency}
]
\addplot[
only marks,
mark=*,
mark size=4pt,
blue,
fill opacity=0.3,
draw opacity=0.5
] coordinates {
(1,1) (1.1,1.1) (1.2,0.9) (1.1,1)
(2,2) (2.1,2.1) (2.2,1.9) (2.1,2)
(3,3) (3.1,3.1) (3.2,2.9) (3.1,3)
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Bar Charts - Categorical data visualization
- Error Bars - Add uncertainty to points
- Colors and Styles - Customize appearance
Scatter Plots in PGFPlots
Visualize data points and correlations with scatter plots.
Basic Scatter Plot
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
title={Basic Scatter Plot}
]
\addplot[only marks] coordinates {
(1, 2.1)
(2, 3.8)
(3, 5.2)
(4, 7.1)
(5, 8.9)
(6, 10.5)
};
\end{axis}
\end{tikzpicture}Marker Styles for Scatter Plots
\begin{tikzpicture}
\begin{axis}[
legend pos=north west
]
% Filled circles
\addplot[only marks, mark=*, blue] coordinates {
(0,0) (1,1) (2,1.5) (3,2.2)
};
% Open circles
\addplot[only marks, mark=o, red] coordinates {
(0,0.5) (1,1.5) (2,2) (3,2.7)
};
% Squares
\addplot[only marks, mark=square*, green!60!black] coordinates {
(0,1) (1,2) (2,2.5) (3,3.2)
};
% Triangles
\addplot[only marks, mark=triangle*, orange] coordinates {
(0,1.5) (1,2.5) (2,3) (3,3.7)
};
\legend{Dataset A, Dataset B, Dataset C, Dataset D}
\end{axis}
\end{tikzpicture}Custom Marker Size
\begin{tikzpicture}
\begin{axis}
% Small markers
\addplot[only marks, mark=*, mark size=1pt, blue] coordinates {
(0,0) (1,1) (2,2)
};
% Medium markers (default)
\addplot[only marks, mark=*, mark size=2pt, red] coordinates {
(0,1) (1,2) (2,3)
};
% Large markers
\addplot[only marks, mark=*, mark size=4pt, green] coordinates {
(0,2) (1,3) (2,4)
};
% Extra large markers
\addplot[only marks, mark=*, mark size=6pt, orange] coordinates {
(0,3) (1,4) (2,5)
};
\end{axis}
\end{tikzpicture}Color-Coded Scatter Plot
\begin{tikzpicture}
\begin{axis}[
colorbar,
colormap/viridis,
title={Color by Value}
]
\addplot[
scatter,
only marks,
scatter src=explicit,
mark=*
] coordinates {
(0, 0) [0]
(1, 1) [1]
(2, 4) [2]
(3, 9) [3]
(4, 16) [4]
(5, 25) [5]
};
\end{axis}
\end{tikzpicture}Variable-Size Markers (Bubble Chart)
\begin{tikzpicture}
\begin{axis}[
title={Bubble Chart},
xlabel={X Value},
ylabel={Y Value}
]
\addplot[
scatter,
only marks,
scatter/@pre marker code/.code={
\pgfmathsetmacro{\markersize}{\pgfplotspointmeta/5}
\edef\markersize{\markersize pt}
\tikzset{mark size=\markersize}
},
scatter/@post marker code/.code={},
mark=*,
fill opacity=0.5,
blue
] table[meta=size] {
x y size
1 2 10
2 4 25
3 3 15
4 5 30
5 4 20
};
\end{axis}
\end{tikzpicture}Scatter Plot with Trend Line
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
legend pos=north west
]
% Data points
\addplot[only marks, mark=*, blue] coordinates {
(1, 2.1) (2, 4.2) (3, 5.8) (4, 8.1)
(5, 9.9) (6, 12.2) (7, 14.0) (8, 16.1)
};
% Linear trend line
\addplot[red, thick, domain=0:9] {2*x};
\legend{Data, $y = 2x$}
\end{axis}
\end{tikzpicture}Multiple Data Series
\begin{tikzpicture}
\begin{axis}[
xlabel={Feature 1},
ylabel={Feature 2},
legend pos=outer north east
]
% Class A
\addplot[only marks, mark=*, blue, mark size=3pt] coordinates {
(1,2) (1.5,2.5) (2,3) (2.5,2.8) (1.8,2.2)
};
% Class B
\addplot[only marks, mark=square*, red, mark size=3pt] coordinates {
(4,5) (4.5,5.5) (5,5) (5.5,5.8) (4.8,5.2)
};
% Class C
\addplot[only marks, mark=triangle*, green!60!black, mark size=3pt] coordinates {
(2.5,5) (3,5.5) (3.5,4.8) (3,4.5) (2.8,5.2)
};
\legend{Class A, Class B, Class C}
\end{axis}
\end{tikzpicture}Scatter Plot from File
% Assuming data.csv contains:
% x,y
% 1,2.1
% 2,3.9
% 3,6.2
% ...
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$}
]
\addplot[only marks, mark=*] table[
col sep=comma,
x=x,
y=y
] {data.csv};
\end{axis}
\end{tikzpicture}Transparent Markers
\begin{tikzpicture}
\begin{axis}[
title={Overlapping Points with Transparency}
]
\addplot[
only marks,
mark=*,
mark size=4pt,
blue,
fill opacity=0.3,
draw opacity=0.5
] coordinates {
(1,1) (1.1,1.1) (1.2,0.9) (1.1,1)
(2,2) (2.1,2.1) (2.2,1.9) (2.1,2)
(3,3) (3.1,3.1) (3.2,2.9) (3.1,3)
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Bar Charts - Categorical data visualization
- Error Bars - Add uncertainty to points
- Colors and Styles - Customize appearance