How to plot blurred curves with PGFPlots?
This is basically the same as Is there an easy way of using line thickness as error indicator in a plot?, only with functions instead of tabulated data. The trick is to use stack plots=y
together with \closedcycle
to create the bands.
I've defined a new command, \addplotwitherrorbands[<optional styles>]{<function>}{<positive error>}{<negative error>}
that can be used as follows:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\tikzset{
error band/.style={fill=orange},
error band style/.style={
error band/.append style=#1
}
}
\newcommand{\addplotwitherrorband}[4][]{
\addplot [#1, draw=none, stack plots=y, forget plot] {#2-(#3)};
\addplot +[#1, draw=none, stack plots=y, error band] {(#3)+(#4)} \closedcycle;
\addplot [#1, draw=none, stack plots=y, forget plot] {-(#2)-(#3)};
\addplot [#1, forget plot] {#2};
}
\begin{tikzpicture}[
declare function={f(\x)=rad(\x)-sin(\x);}
]
\begin{axis}[domain=0:360, enlarge x limits=false,
cycle list={
error band style=orange!20\\
error band style=orange!40\\
error band style=orange!60\\
error band style=orange!80\\
error band style=orange!100\\
}]
\pgfplotsinvokeforeach{1,0.5,0.25,0.125, 0.0625} {
\addplotwitherrorband [] {f(x)}{#1}{#1}
}
\end{axis}
\end{tikzpicture}
\end{document}
Check out if this MWE
with Asymptote
does what you need.
For demonstration in this example it uses three paths (guide
s)
gtop
,gbot
and gmid
to define functions f(x)
for mean
and s(x)
for deviation, use the proper definitions
of f(x)
and s(x)
instead.
Array pen[] clrs
defines colors, array real[] dh
defines
fractions of the total interval to cover with the corresponding color.
Then for every color the top (gt
), bottom (gb
) guides
are defined and joined in a region g
fo fill with i
-th color.
% blurred.tex:
%
\documentclass{article}
\usepackage{textgreek}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{figure}
\begin{asy}
size(200);
import graph;
pair[] botP={(0,0.09),(0.252,0.196),(0.383,0.429),(0.479,0.588),
(0.574,0.668),(0.733,0.726),(0.883,0.747),(1,0.747),};
pair[] topP={(0,0.341),(0.252,0.451),(0.383,0.677),(0.479,0.841),
(0.574,0.92),(0.733,0.977),(0.883,0.993),(1,1),};
pair[] midP=0.5*(topP+botP);
guide gtop=graph(topP,operator..);
guide gbot=graph(botP,operator..);
guide gmid=graph(midP,operator..);
real f(real x){
real t=times(gmid,x)[0];
return point(gmid,t).y;
};
real s(real x){
real tt=times(gtop,x)[0];
real tb=times(gbot,x)[0];
return point(gtop,tt).y-point(gbot,tb).y;
};
real xmin=0, xmax=1;
pen[] clrs={
rgb(0.988,0.847,0.796),
rgb(0.969,0.592,0.502),
rgb(0.953,0.365,0.29),
rgb(0.933,0.188,0.165),
rgb(0.933,0.114,0.137),
};
real[] dh={1,0.5,0.25,0.125,0.0625};
guide gt, gb,g;
for(int i=0;i<clrs.length;++i){
gt=graph(new real(real x){return f(x)+0.5dh[i]*s(x);},xmin,xmax);
gb=graph(new real(real x){return f(x)-0.5dh[i]*s(x);},xmin,xmax);
g=gb--reverse(gt)--cycle;
fill(g,clrs[i]);
}
real ymax=1.1;
pen axisPen=darkblue+1.3bp;
xaxis(xmin,xmax,axisPen);
xaxis(YEquals(ymax),xmin,xmax,axisPen);
label("\textbf{\straighttheta(d$|$m)}",(0.6,0.5));
label("\textbf{m}",(xmax,0),NW);
label("\textbf{d}",(0,1),SE);
shipout(bbox(Fill(paleyellow)));
\end{asy}
\end{figure}
\end{document}
%
% Process:
%
% pdflatex blurred.tex
% asy blurred-*.asy
% pdflatex blurred.tex
Inspired by g.kov answer, this is the TikZ solution:
\usetikzlibrary{backgrounds}
\begin{tikzpicture}[
show background rectangle,
declare function={
f(\x) = \x - sin(deg(\x));
s(\x) = 0.8;
xmin = 0;
xmax = 2*pi;
}]
\foreach \dh/\color in {1/20, 0.5/40, 0.25/60, 0.125/80, 0.0625/100} {
\fill[smooth,color=red!\color,domain=xmin:xmax] plot (\x,{f(\x)+\dh*s(\x)}) --
plot[domain=xmax:xmin] (\x,{f(\x)-\dh*s(\x)}) -- cycle;
}
\node at (current bounding box.south east) {$m$};
\node at (current bounding box.north west) {$d$};
\end{tikzpicture}
Unfortunately, PGFPlots doesn't allow cycle f(x)+s(x)
and f(x)-s(x)
with \addplot
as I did with the TikZ \draw
command. Improvements are welcome.