I am trying to recreate the following figure using pgfplots: enter image description here

I tried the following code, but it results in 'fill between' misbehaving, especially the blue color as shown at the bottom Figure.

% define gaussian pdf and cdf
    \pgfmathparse{1/(1+exp(-0.07056*((#1-#2)/#3)^3 - 1.5976*(#1-#2)/#3))}%



\begin{tikzpicture}[transform shape, scale=0.5]
    width=\textwidth, height=0.5\textwidth,
    enlarge x limits=false,
    y axis line style={opacity=0},
    x axis line style={opacity=0},
    %   xlabel=$t$,ylabel=$i_{PIN}$,
    every axis y label/.style={at={(ticklabel* cs:1.025)},anchor=south},  
    every axis x label/.style={at={(ticklabel* cs:1.025)},anchor=west}, 
    axis x line =bottom,
    axis y line =left,
    \foreach \bit[remember=\bit as \lastbit (initially 0), count=\xi, evaluate=\xi as \xj using int(\xi-1) ] in \data{
        \ifthenelse{\NOT \lastbit=\bit}
        {% True
            \ifthenelse{\NOT \bit=0}
                \addplot[no markers, smooth, samples=50,domain=\xi-\del:\xi+\del] {-((\xi-x)/(2*\del))+0.5};% remap -0.1 to 0 and 0.1 to 1
                \addplot[no markers, smooth, samples=50,domain=\xi-\del:\xi+\del] {((\xi-x)/(2*\del))+0.5};% remap -0.1 to 1 and 0.1 to 0
        {% False
        \addplot [no markers, smooth, samples=100,domain=\xi:\xi+1] {(\bit<1) ? sin(pi)+rand*0.2 : 1+sin(pi)+rand*0.2};
            \noexpand \draw (axis cs:\xi+0.5,1.5)node[above](l\xi){\mya};
    \draw[dashed, name path=line2] (axis cs:0.25,1)node(U){} (axis cs:0.5,1) to(axis cs:\xend,1)node(up){};
    \draw[dashed, name path=line0] (axis cs:0.25,0)node(V){} (axis cs:0.5,0)to(axis cs:\xend,0)node(btm){};
    \draw[dashed, name path=line1] (axis cs:0.5,0.5)to(axis cs:\xend,0.5)node[right]{$ V_{DTH} $} (axis cs:10,0.5)node(mid){};
    \draw[dashed, name path=line3] (axis cs:8,1.1)to(axis cs:8.5,1.1);
    \path[name path=vline] (axis cs:8.25,0)to(axis cs:8.25,1.5);
    \path [name intersections={of=vline and line2,by=A}];
    \path [name intersections={of=vline and line3,by=B}];
    \draw (axis cs:8.25,0.75)node(C){}  (axis cs:8.25,1.35)node(D){};           
    \draw[stealth-](B.center)to(B|-D)node[right]{\Large $ v^{rms}_{n} $};
    \draw[stealth-stealth](U.center)to node[left]{\Large $ v_{s}^{pp} $}(V.center);


    every axis plot post/.append style={mark=none,domain={-0.05*(\xmax)}:{1.08*\xmax},samples=100,smooth},  
    xmin={-0.1*(\xmax)}, xmax=\xmax,
    ymin=\ymin, ymax={1.05*cdf(\xmax,\B,\Bs)},
    axis lines=middle,
    axis line style=thick,
    enlargelimits=upper, % extend the axes a bit to the right and top
    y axis line style={opacity=0},
    anchor= south,
    xshift= -150mm,
    yshift= 32.5mm,%32.5mm: -88mm
    rotate=-90,% To rotate the plot
    every axis x label/.style={at={(ticklabel* cs:1.025)},anchor=west},
    every axis y label/.style={at={(ticklabel* cs:1.025)},anchor=north},
    width=\textwidth, height=0.5*\textwidth,
    transform shape, scale=0.97,
    clip=false, % prevent labels falling off

    \addplot[blue, name path=B,thick] {gauss(x,\B,\Bs)};
    \addplot[red,  name path=S,thick] {gauss(x,\S,\Ss)};
    % FILL
    \path[name path=xaxis](axis cs:0,0) to (axis cs:\xmax,0); 
    \addplot[white!50!blue] fill between[of=xaxis and B, soft clip={domain=\q:\xmax}];
    \addplot[white!50!red]  fill between[of=xaxis and S, soft clip={domain=0:\q}];      


enter image description here

Also, it's getting hard to position the second axis to align with center line (mid), when changing the scale. Is there's any easier way to accomplish it.


1 Answer 1


Just swapping the domain range, has resolved the above problem.

\addplot[white!50!blue] fill between[of=xaxis and B, soft clip={domain=\q:\xmax}];


\addplot[white!50!blue] fill between[of=xaxis and B, soft clip={domain=\xmax:\q}];

But still positioning and aligning the axises are tricky.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .