Histograms in PGFPlots
Create histograms for frequency distributions and statistical analysis.
Setup for Histograms
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.18}Basic Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Value},
ylabel={Frequency},
ymin=0
]
\addplot[
hist={bins=10, data min=0, data max=100}
] table[y index=0] {
data
15
23
45
67
34
56
78
89
43
21
54
76
32
65
87
};
\end{axis}
\end{tikzpicture}Pre-computed Histogram
If you already have bin counts, use ybar interval directly:
\begin{tikzpicture}
\begin{axis}[
ybar interval=0.9,
xlabel={Range},
ylabel={Count},
xticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=0
},
ymin=0
]
\addplot[fill=blue!40] coordinates {
(0, 5)
(10, 12)
(20, 25)
(30, 18)
(40, 10)
(50, 3)
(60, 0)
};
\end{axis}
\end{tikzpicture}Customizing Bins
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Score},
ylabel={Students}
]
\addplot[
hist={
bins=5, % Number of bins
data min=0, % Minimum data value
data max=100 % Maximum data value
},
fill=blue!50
] table[y index=0] {
scores
45 67 78 89 56 34 72 81 93 55
62 74 88 91 43 58 77 85 96 51
};
\end{axis}
\end{tikzpicture}Density Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Value},
ylabel={Density},
ymin=0
]
\addplot[
hist={
bins=10,
density % Normalize to density
},
fill=green!50
] table[y index=0] {
data
1.2 2.3 1.8 2.1 1.9 2.5 2.2 1.7 2.0 2.4
1.5 2.6 1.6 2.8 1.4 2.7 1.3 2.9 1.1 3.0
};
\end{axis}
\end{tikzpicture}Cumulative Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Value},
ylabel={Cumulative Count}
]
\addplot[
hist={
bins=10,
cumulative % Cumulative histogram
},
fill=orange!50
] table[y index=0] {
data
10 20 30 40 50 15 25 35 45 55
12 22 32 42 52 18 28 38 48 58
};
\end{axis}
\end{tikzpicture}Multiple Histograms
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Score},
ylabel={Count},
legend pos=north west,
ymin=0
]
% Group A
\addplot[
hist={bins=5, data min=0, data max=100},
fill=blue!40,
fill opacity=0.7
] table[y index=0] {
A
65 72 78 82 69 74 80 85 71 76
};
% Group B
\addplot[
hist={bins=5, data min=0, data max=100},
fill=red!40,
fill opacity=0.7
] table[y index=0] {
B
55 62 68 73 58 64 70 75 60 66
};
\legend{Group A, Group B}
\end{axis}
\end{tikzpicture}Styled Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval=0.8,
xlabel={Measurement},
ylabel={Frequency},
title={Distribution of Measurements},
grid=major,
ymin=0,
xtick={0,20,40,60,80,100},
minor y tick num=1
]
\addplot[
fill=blue!60,
draw=blue!80,
line width=0.5pt
] coordinates {
(0, 2)
(20, 8)
(40, 15)
(60, 12)
(80, 5)
(100, 0)
};
\end{axis}
\end{tikzpicture}Histogram with Normal Curve
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={$x$},
ylabel={Density},
ymin=0,
domain=0:10
]
% Histogram
\addplot[
hist={
bins=10,
density
},
fill=blue!30
] table[y index=0] {
data
4.5 5.2 4.8 5.1 4.9 5.3 4.7 5.0 4.6 5.4
5.1 4.9 5.2 4.8 5.0 5.3 4.7 5.1 4.9 5.2
};
% Normal distribution overlay
\addplot[red, thick, smooth, samples=100] {
1/(0.3*sqrt(2*pi)) * exp(-(x-5)^2/(2*0.3^2))
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Box Plots - Statistical summaries
- Error Bars - Uncertainty visualization
- Area Plots - Filled regions
Histograms in PGFPlots
Create histograms for frequency distributions and statistical analysis.
Setup for Histograms
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.18}Basic Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Value},
ylabel={Frequency},
ymin=0
]
\addplot[
hist={bins=10, data min=0, data max=100}
] table[y index=0] {
data
15
23
45
67
34
56
78
89
43
21
54
76
32
65
87
};
\end{axis}
\end{tikzpicture}Pre-computed Histogram
If you already have bin counts, use ybar interval directly:
\begin{tikzpicture}
\begin{axis}[
ybar interval=0.9,
xlabel={Range},
ylabel={Count},
xticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=0
},
ymin=0
]
\addplot[fill=blue!40] coordinates {
(0, 5)
(10, 12)
(20, 25)
(30, 18)
(40, 10)
(50, 3)
(60, 0)
};
\end{axis}
\end{tikzpicture}Customizing Bins
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Score},
ylabel={Students}
]
\addplot[
hist={
bins=5, % Number of bins
data min=0, % Minimum data value
data max=100 % Maximum data value
},
fill=blue!50
] table[y index=0] {
scores
45 67 78 89 56 34 72 81 93 55
62 74 88 91 43 58 77 85 96 51
};
\end{axis}
\end{tikzpicture}Density Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Value},
ylabel={Density},
ymin=0
]
\addplot[
hist={
bins=10,
density % Normalize to density
},
fill=green!50
] table[y index=0] {
data
1.2 2.3 1.8 2.1 1.9 2.5 2.2 1.7 2.0 2.4
1.5 2.6 1.6 2.8 1.4 2.7 1.3 2.9 1.1 3.0
};
\end{axis}
\end{tikzpicture}Cumulative Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Value},
ylabel={Cumulative Count}
]
\addplot[
hist={
bins=10,
cumulative % Cumulative histogram
},
fill=orange!50
] table[y index=0] {
data
10 20 30 40 50 15 25 35 45 55
12 22 32 42 52 18 28 38 48 58
};
\end{axis}
\end{tikzpicture}Multiple Histograms
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={Score},
ylabel={Count},
legend pos=north west,
ymin=0
]
% Group A
\addplot[
hist={bins=5, data min=0, data max=100},
fill=blue!40,
fill opacity=0.7
] table[y index=0] {
A
65 72 78 82 69 74 80 85 71 76
};
% Group B
\addplot[
hist={bins=5, data min=0, data max=100},
fill=red!40,
fill opacity=0.7
] table[y index=0] {
B
55 62 68 73 58 64 70 75 60 66
};
\legend{Group A, Group B}
\end{axis}
\end{tikzpicture}Styled Histogram
\begin{tikzpicture}
\begin{axis}[
ybar interval=0.8,
xlabel={Measurement},
ylabel={Frequency},
title={Distribution of Measurements},
grid=major,
ymin=0,
xtick={0,20,40,60,80,100},
minor y tick num=1
]
\addplot[
fill=blue!60,
draw=blue!80,
line width=0.5pt
] coordinates {
(0, 2)
(20, 8)
(40, 15)
(60, 12)
(80, 5)
(100, 0)
};
\end{axis}
\end{tikzpicture}Histogram with Normal Curve
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xlabel={$x$},
ylabel={Density},
ymin=0,
domain=0:10
]
% Histogram
\addplot[
hist={
bins=10,
density
},
fill=blue!30
] table[y index=0] {
data
4.5 5.2 4.8 5.1 4.9 5.3 4.7 5.0 4.6 5.4
5.1 4.9 5.2 4.8 5.0 5.3 4.7 5.1 4.9 5.2
};
% Normal distribution overlay
\addplot[red, thick, smooth, samples=100] {
1/(0.3*sqrt(2*pi)) * exp(-(x-5)^2/(2*0.3^2))
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Box Plots - Statistical summaries
- Error Bars - Uncertainty visualization
- Area Plots - Filled regions