Search code examples
latexfillcliptikzpgfplots

pgfplots "Soft clip" of "Fill between" not working with loglogaxis


I am struggling with soft clipping a fill between when using a loglogaxis. The soft-clip domain seems to be completely off; i.e. setting soft clip={domain=1:1} yields the same result as no domain at all.

clipping with no effect

Yet a domain soft clip={domain=0:10000000000} results in a horizontal clip:

weird horizontal clip

The pgfplots manual doesn't give an example for clipping on a loglogaxis, might it be it's simply not supported? If it is, can anyone point me on how to clip to soft clip={domain=3.7E4:5.5E5} ?

My MWE:

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
    
    \begin{tikzpicture}
        \def\xmin{1E4}
        \def\xmax{1E7}
    
        \begin{loglogaxis}[
            xmin=\xmin,xmax=\xmax,
            ymin=1E-2,ymax=1000,
            xlabel={Re [-]},
            small,
            height=4cm,
            width=15cm,
            xtick={1E4,1E5,1E6,1E7},
            extra x ticks={3.7E4,5.5E5},
            extra x tick labels ={3.7E4,5.5E5},
            extra x tick style={
                tickwidth=\pgfkeysvalueof{/pgfplots/minor tick length},
                tick label style={yshift=-0.5mm}
            },
            ytick={1E-2,1E-1,1,10},
            yticklabels={,,1},
            extra y ticks={1E-2,1E-1},
            grid=major,
            domain=1E4:1E7,
            ]
            
            \def\D{1.4}     
            
            \def\yplus{30}
            \addplot[name path = C,thick,domain=1E4:1E7] {\yplus/((1/2*(2*log10(x)-0.65)^(-2.3)*(x/\D)^2)^(1/2)/1000)};
            
            \def\yplus{300}
            \addplot[name path = D,thick,domain=1E4:1E7] {\yplus/((1/2*(2*log10(x)-0.65)^(-2.3)*(x/\D)^2)^(1/2)/1000)};
                
            \addplot [gray!30] fill between[of=C and D, soft clip={domain=3.7E4:5.5E5}];
        
        \end{loglogaxis}
        
    \end{tikzpicture}%
    
\end{document}


Solution

  • You could use the following technique to clip it:

    \documentclass[tikz]{standalone}
    
    \usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{compat=1.18}
    
    \begin{document}
        
        \begin{tikzpicture}
            \def\xmin{1E4}
            \def\xmax{1E7}
        
            \begin{loglogaxis}[
                xmin=\xmin,xmax=\xmax,
                ymin=1E-2,ymax=1000,
                xlabel={Re [-]},
                small,
                height=4cm,
                width=15cm,
                xtick={1E4,1E5,1E6,1E7},
                extra x ticks={3.7E4,5.5E5},
                extra x tick labels ={3.7E4,5.5E5},
                extra x tick style={
                    tickwidth=\pgfkeysvalueof{/pgfplots/minor tick length},
                    tick label style={yshift=-0.5mm}
                },
                ytick={1E-2,1E-1,1,10},
                yticklabels={,,1},
                extra y ticks={1E-2,1E-1},
                grid=major,
                domain=1E4:1E7,
                ]
                
                \def\D{1.4}     
                
                \def\yplus{30}
                \addplot[name path = C,thick,domain=1E4:1E7] {\yplus/((1/2*(2*log10(x)-0.65)^(-2.3)*(x/\D)^2)^(1/2)/1000)};
                
                \def\yplus{300}
                \addplot[name path = D,thick,domain=1E4:1E7] {\yplus/((1/2*(2*log10(x)-0.65)^(-2.3)*(x/\D)^2)^(1/2)/1000)};
                    
                \draw [
                help lines,
                name path=clippath]
                        (3.7E4,1E-2) rectangle (5.5E5,1E3);
                    
                \addplot [gray!30] fill between[of=C and D,soft clip={clippath}];
            
            \end{loglogaxis}
            
        \end{tikzpicture}%
        
    \end{document}
    

    enter image description here