Bar Charts in PGFPlots
Create vertical, horizontal, grouped, and stacked bar charts for categorical data.
Basic Vertical Bar Chart
\begin{tikzpicture}
\begin{axis}[
ybar,
xlabel={Category},
ylabel={Value},
symbolic x coords={A, B, C, D, E},
xtick=data
]
\addplot coordinates {
(A, 10)
(B, 25)
(C, 15)
(D, 30)
(E, 20)
};
\end{axis}
\end{tikzpicture}Horizontal Bar Chart
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={Value},
ylabel={Category},
symbolic y coords={E, D, C, B, A},
ytick=data,
nodes near coords,
nodes near coords align={horizontal}
]
\addplot coordinates {
(10, A)
(25, B)
(15, C)
(30, D)
(20, E)
};
\end{axis}
\end{tikzpicture}Bar Styling
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt, % Width of bars
symbolic x coords={A, B, C, D},
xtick=data
]
% Colored and styled bars
\addplot[
fill=blue!60,
draw=blue!80,
postaction={
pattern=north east lines,
pattern color=blue!30
}
] coordinates {(A,10) (B,20) (C,15) (D,25)};
\end{axis}
\end{tikzpicture}Grouped Bar Chart
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=10pt,
symbolic x coords={2020, 2021, 2022, 2023},
xtick=data,
ylabel={Sales (M\$)},
legend pos=north west,
ymin=0
]
\addplot[fill=blue!60] coordinates {
(2020, 50) (2021, 60) (2022, 55) (2023, 70)
};
\addplot[fill=red!60] coordinates {
(2020, 45) (2021, 55) (2022, 65) (2023, 60)
};
\addplot[fill=green!60] coordinates {
(2020, 30) (2021, 40) (2022, 50) (2023, 55)
};
\legend{Product A, Product B, Product C}
\end{axis}
\end{tikzpicture}Stacked Bar Chart
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=20pt,
symbolic x coords={Q1, Q2, Q3, Q4},
xtick=data,
ylabel={Revenue (\$)},
legend style={at={(0.5,-0.15)}, anchor=north, legend columns=3},
ymin=0
]
\addplot[fill=blue!60] coordinates {
(Q1, 100) (Q2, 120) (Q3, 130) (Q4, 150)
};
\addplot[fill=red!60] coordinates {
(Q1, 80) (Q2, 90) (Q3, 100) (Q4, 110)
};
\addplot[fill=green!60] coordinates {
(Q1, 50) (Q2, 60) (Q3, 70) (Q4, 80)
};
\legend{Domestic, International, Online}
\end{axis}
\end{tikzpicture}Bar Chart with Value Labels
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=20pt,
symbolic x coords={A, B, C, D},
xtick=data,
nodes near coords, % Show values
nodes near coords style={
font=\footnotesize,
/pgf/number format/fixed
},
ymin=0
]
\addplot[fill=blue!60] coordinates {
(A, 12.5) (B, 28.3) (C, 19.7) (D, 35.1)
};
\end{axis}
\end{tikzpicture}Negative Values
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt,
symbolic x coords={Jan, Feb, Mar, Apr, May},
xtick=data,
ylabel={Profit/Loss (\$)},
ymin=-50,
ymax=50
]
\addplot[
fill=blue!60,
postaction={
pattern=north east lines,
pattern color=blue!30
}
] coordinates {
(Jan, 20) (Feb, -15) (Mar, 35) (Apr, -30) (May, 25)
};
\end{axis}
\end{tikzpicture}Custom Bar Colors
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=20pt,
symbolic x coords={A, B, C, D, E},
xtick=data,
ymin=0
]
\addplot[
point meta=explicit,
fill=blue!60,
visualization depends on={\thisrow{color} \as \mycolor},
every node near coord/.append style={color=\mycolor}
] table[meta=color] {
x y color
A 10 blue
B 25 red
C 15 green
D 30 orange
E 20 purple
};
\end{axis}
\end{tikzpicture}
% Or simpler approach with separate addplot commands:
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=20pt,
symbolic x coords={A, B, C, D},
xtick=data,
ymin=0
]
\addplot[fill=red!60] coordinates {(A, 10)};
\addplot[fill=blue!60] coordinates {(B, 25)};
\addplot[fill=green!60] coordinates {(C, 15)};
\addplot[fill=orange!60] coordinates {(D, 30)};
\end{axis}
\end{tikzpicture}Error Bars on Bar Charts
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt,
symbolic x coords={Control, Treatment A, Treatment B},
xtick=data,
ylabel={Response},
ymin=0
]
\addplot[
fill=blue!60,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(Control, 50) +- (0, 5)
(Treatment A, 75) +- (0, 8)
(Treatment B, 65) +- (0, 6)
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Histograms - Frequency distributions
- Error Bars - Uncertainty visualization
- Axis Customization - Advanced axis options
Bar Charts in PGFPlots
Create vertical, horizontal, grouped, and stacked bar charts for categorical data.
Basic Vertical Bar Chart
\begin{tikzpicture}
\begin{axis}[
ybar,
xlabel={Category},
ylabel={Value},
symbolic x coords={A, B, C, D, E},
xtick=data
]
\addplot coordinates {
(A, 10)
(B, 25)
(C, 15)
(D, 30)
(E, 20)
};
\end{axis}
\end{tikzpicture}Horizontal Bar Chart
\begin{tikzpicture}
\begin{axis}[
xbar,
xlabel={Value},
ylabel={Category},
symbolic y coords={E, D, C, B, A},
ytick=data,
nodes near coords,
nodes near coords align={horizontal}
]
\addplot coordinates {
(10, A)
(25, B)
(15, C)
(30, D)
(20, E)
};
\end{axis}
\end{tikzpicture}Bar Styling
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt, % Width of bars
symbolic x coords={A, B, C, D},
xtick=data
]
% Colored and styled bars
\addplot[
fill=blue!60,
draw=blue!80,
postaction={
pattern=north east lines,
pattern color=blue!30
}
] coordinates {(A,10) (B,20) (C,15) (D,25)};
\end{axis}
\end{tikzpicture}Grouped Bar Chart
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=10pt,
symbolic x coords={2020, 2021, 2022, 2023},
xtick=data,
ylabel={Sales (M\$)},
legend pos=north west,
ymin=0
]
\addplot[fill=blue!60] coordinates {
(2020, 50) (2021, 60) (2022, 55) (2023, 70)
};
\addplot[fill=red!60] coordinates {
(2020, 45) (2021, 55) (2022, 65) (2023, 60)
};
\addplot[fill=green!60] coordinates {
(2020, 30) (2021, 40) (2022, 50) (2023, 55)
};
\legend{Product A, Product B, Product C}
\end{axis}
\end{tikzpicture}Stacked Bar Chart
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=20pt,
symbolic x coords={Q1, Q2, Q3, Q4},
xtick=data,
ylabel={Revenue (\$)},
legend style={at={(0.5,-0.15)}, anchor=north, legend columns=3},
ymin=0
]
\addplot[fill=blue!60] coordinates {
(Q1, 100) (Q2, 120) (Q3, 130) (Q4, 150)
};
\addplot[fill=red!60] coordinates {
(Q1, 80) (Q2, 90) (Q3, 100) (Q4, 110)
};
\addplot[fill=green!60] coordinates {
(Q1, 50) (Q2, 60) (Q3, 70) (Q4, 80)
};
\legend{Domestic, International, Online}
\end{axis}
\end{tikzpicture}Bar Chart with Value Labels
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=20pt,
symbolic x coords={A, B, C, D},
xtick=data,
nodes near coords, % Show values
nodes near coords style={
font=\footnotesize,
/pgf/number format/fixed
},
ymin=0
]
\addplot[fill=blue!60] coordinates {
(A, 12.5) (B, 28.3) (C, 19.7) (D, 35.1)
};
\end{axis}
\end{tikzpicture}Negative Values
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt,
symbolic x coords={Jan, Feb, Mar, Apr, May},
xtick=data,
ylabel={Profit/Loss (\$)},
ymin=-50,
ymax=50
]
\addplot[
fill=blue!60,
postaction={
pattern=north east lines,
pattern color=blue!30
}
] coordinates {
(Jan, 20) (Feb, -15) (Mar, 35) (Apr, -30) (May, 25)
};
\end{axis}
\end{tikzpicture}Custom Bar Colors
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=20pt,
symbolic x coords={A, B, C, D, E},
xtick=data,
ymin=0
]
\addplot[
point meta=explicit,
fill=blue!60,
visualization depends on={\thisrow{color} \as \mycolor},
every node near coord/.append style={color=\mycolor}
] table[meta=color] {
x y color
A 10 blue
B 25 red
C 15 green
D 30 orange
E 20 purple
};
\end{axis}
\end{tikzpicture}
% Or simpler approach with separate addplot commands:
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=20pt,
symbolic x coords={A, B, C, D},
xtick=data,
ymin=0
]
\addplot[fill=red!60] coordinates {(A, 10)};
\addplot[fill=blue!60] coordinates {(B, 25)};
\addplot[fill=green!60] coordinates {(C, 15)};
\addplot[fill=orange!60] coordinates {(D, 30)};
\end{axis}
\end{tikzpicture}Error Bars on Bar Charts
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=15pt,
symbolic x coords={Control, Treatment A, Treatment B},
xtick=data,
ylabel={Response},
ymin=0
]
\addplot[
fill=blue!60,
error bars/.cd,
y dir=both,
y explicit
] coordinates {
(Control, 50) +- (0, 5)
(Treatment A, 75) +- (0, 8)
(Treatment B, 65) +- (0, 6)
};
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- Histograms - Frequency distributions
- Error Bars - Uncertainty visualization
- Axis Customization - Advanced axis options