Area Plots in PGFPlots
Create filled area charts, stacked areas, and fill between curves.
Basic Area Plot
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
area style
]
\addplot[fill=blue!30, draw=blue] coordinates {
(0, 0)
(1, 2)
(2, 3)
(3, 4)
(4, 3)
(5, 2)
} \closedcycle;
\end{axis}
\end{tikzpicture}Fill to Axis
\begin{tikzpicture}
\begin{axis}[
domain=0:4,
samples=100,
xlabel={$x$},
ylabel={$f(x)$}
]
% Fill from curve to x-axis
\addplot[
fill=green!30,
draw=green!60!black,
thick
] {sin(deg(x))*x} \closedcycle;
\end{axis}
\end{tikzpicture}Stacked Area Plot
\begin{tikzpicture}
\begin{axis}[
stack plots=y,
area style,
xlabel={Year},
ylabel={Value},
legend pos=outer north east
]
\addplot[fill=blue!40] coordinates {
(2018, 10) (2019, 12) (2020, 15) (2021, 18) (2022, 20)
} \closedcycle;
\addplot[fill=red!40] coordinates {
(2018, 8) (2019, 10) (2020, 12) (2021, 14) (2022, 16)
} \closedcycle;
\addplot[fill=green!40] coordinates {
(2018, 5) (2019, 6) (2020, 8) (2021, 10) (2022, 12)
} \closedcycle;
\legend{Product A, Product B, Product C}
\end{axis}
\end{tikzpicture}Fill Between Two Curves
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[
domain=0:3,
samples=100
]
% Upper curve
\addplot[name path=upper, blue, thick] {x^2 + 1};
% Lower curve
\addplot[name path=lower, red, thick] {x};
% Fill between
\addplot[fill=yellow, fill opacity=0.3]
fill between[of=upper and lower];
\end{axis}
\end{tikzpicture}Confidence Interval
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
legend pos=north west
]
% Mean line
\addplot[name path=mean, blue, thick] coordinates {
(0, 1) (1, 2) (2, 2.5) (3, 3) (4, 3.2)
};
% Upper bound
\addplot[name path=upper, draw=none] coordinates {
(0, 1.5) (1, 2.8) (2, 3.3) (3, 3.8) (4, 4.0)
};
% Lower bound
\addplot[name path=lower, draw=none] coordinates {
(0, 0.5) (1, 1.2) (2, 1.7) (3, 2.2) (4, 2.4)
};
% Fill confidence interval
\addplot[fill=blue, fill opacity=0.2]
fill between[of=upper and lower];
\legend{Mean, 95\% CI}
\end{axis}
\end{tikzpicture}Partial Fill
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[
domain=0:4,
samples=100
]
\addplot[name path=f, blue, thick] {sin(deg(x*180/pi))};
\addplot[name path=axis, draw=none] {0};
% Fill only positive part
\addplot[fill=green!30]
fill between[of=f and axis, soft clip={domain=0:1}];
% Fill only negative part
\addplot[fill=red!30]
fill between[of=f and axis, soft clip={domain=1:2}];
\end{axis}
\end{tikzpicture}Gradient Fill
\begin{tikzpicture}
\begin{axis}[
domain=0:5,
samples=100
]
\addplot[
thick,
blue,
fill=blue,
fill opacity=0.3,
area legend
] {exp(-x/2)*sin(deg(x*2))} \closedcycle;
\end{axis}
\end{tikzpicture}100% Stacked Area
\begin{tikzpicture}
\begin{axis}[
stack plots=y,
area style,
ymin=0, ymax=100,
xlabel={Quarter},
ylabel={Percentage (\%)},
symbolic x coords={Q1, Q2, Q3, Q4},
xtick=data,
legend style={at={(0.5,-0.15)}, anchor=north, legend columns=3}
]
\addplot[fill=blue!50] coordinates {
(Q1, 40) (Q2, 35) (Q3, 30) (Q4, 35)
} \closedcycle;
\addplot[fill=red!50] coordinates {
(Q1, 35) (Q2, 40) (Q3, 45) (Q4, 40)
} \closedcycle;
\addplot[fill=green!50] coordinates {
(Q1, 25) (Q2, 25) (Q3, 25) (Q4, 25)
} \closedcycle;
\legend{Category A, Category B, Category C}
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- 3D Plots - Three-dimensional surfaces
- Error Bars - Add uncertainty bands
- Multiple Plots - Combine multiple charts
Area Plots in PGFPlots
Create filled area charts, stacked areas, and fill between curves.
Basic Area Plot
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
area style
]
\addplot[fill=blue!30, draw=blue] coordinates {
(0, 0)
(1, 2)
(2, 3)
(3, 4)
(4, 3)
(5, 2)
} \closedcycle;
\end{axis}
\end{tikzpicture}Fill to Axis
\begin{tikzpicture}
\begin{axis}[
domain=0:4,
samples=100,
xlabel={$x$},
ylabel={$f(x)$}
]
% Fill from curve to x-axis
\addplot[
fill=green!30,
draw=green!60!black,
thick
] {sin(deg(x))*x} \closedcycle;
\end{axis}
\end{tikzpicture}Stacked Area Plot
\begin{tikzpicture}
\begin{axis}[
stack plots=y,
area style,
xlabel={Year},
ylabel={Value},
legend pos=outer north east
]
\addplot[fill=blue!40] coordinates {
(2018, 10) (2019, 12) (2020, 15) (2021, 18) (2022, 20)
} \closedcycle;
\addplot[fill=red!40] coordinates {
(2018, 8) (2019, 10) (2020, 12) (2021, 14) (2022, 16)
} \closedcycle;
\addplot[fill=green!40] coordinates {
(2018, 5) (2019, 6) (2020, 8) (2021, 10) (2022, 12)
} \closedcycle;
\legend{Product A, Product B, Product C}
\end{axis}
\end{tikzpicture}Fill Between Two Curves
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[
domain=0:3,
samples=100
]
% Upper curve
\addplot[name path=upper, blue, thick] {x^2 + 1};
% Lower curve
\addplot[name path=lower, red, thick] {x};
% Fill between
\addplot[fill=yellow, fill opacity=0.3]
fill between[of=upper and lower];
\end{axis}
\end{tikzpicture}Confidence Interval
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
legend pos=north west
]
% Mean line
\addplot[name path=mean, blue, thick] coordinates {
(0, 1) (1, 2) (2, 2.5) (3, 3) (4, 3.2)
};
% Upper bound
\addplot[name path=upper, draw=none] coordinates {
(0, 1.5) (1, 2.8) (2, 3.3) (3, 3.8) (4, 4.0)
};
% Lower bound
\addplot[name path=lower, draw=none] coordinates {
(0, 0.5) (1, 1.2) (2, 1.7) (3, 2.2) (4, 2.4)
};
% Fill confidence interval
\addplot[fill=blue, fill opacity=0.2]
fill between[of=upper and lower];
\legend{Mean, 95\% CI}
\end{axis}
\end{tikzpicture}Partial Fill
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[
domain=0:4,
samples=100
]
\addplot[name path=f, blue, thick] {sin(deg(x*180/pi))};
\addplot[name path=axis, draw=none] {0};
% Fill only positive part
\addplot[fill=green!30]
fill between[of=f and axis, soft clip={domain=0:1}];
% Fill only negative part
\addplot[fill=red!30]
fill between[of=f and axis, soft clip={domain=1:2}];
\end{axis}
\end{tikzpicture}Gradient Fill
\begin{tikzpicture}
\begin{axis}[
domain=0:5,
samples=100
]
\addplot[
thick,
blue,
fill=blue,
fill opacity=0.3,
area legend
] {exp(-x/2)*sin(deg(x*2))} \closedcycle;
\end{axis}
\end{tikzpicture}100% Stacked Area
\begin{tikzpicture}
\begin{axis}[
stack plots=y,
area style,
ymin=0, ymax=100,
xlabel={Quarter},
ylabel={Percentage (\%)},
symbolic x coords={Q1, Q2, Q3, Q4},
xtick=data,
legend style={at={(0.5,-0.15)}, anchor=north, legend columns=3}
]
\addplot[fill=blue!50] coordinates {
(Q1, 40) (Q2, 35) (Q3, 30) (Q4, 35)
} \closedcycle;
\addplot[fill=red!50] coordinates {
(Q1, 35) (Q2, 40) (Q3, 45) (Q4, 40)
} \closedcycle;
\addplot[fill=green!50] coordinates {
(Q1, 25) (Q2, 25) (Q3, 25) (Q4, 25)
} \closedcycle;
\legend{Category A, Category B, Category C}
\end{axis}
\end{tikzpicture}Next Steps
Continue learning PGFPlots:
- 3D Plots - Three-dimensional surfaces
- Error Bars - Add uncertainty bands
- Multiple Plots - Combine multiple charts