You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pgfplots中loglogaxis下fill between的Soft clip功能异常解决方案求助

问题原因

pgfplots的fill between库的soft clip功能默认基于笛卡尔线性坐标系计算裁剪范围,不会自动适配对数坐标轴的坐标变换,所以直接传入原始x值的定义域不会得到预期效果。

解决方案

最稳妥的实现方式是手动指定裁剪路径,使用axis cs坐标系锚定你需要的裁剪范围,替换你原有的填充代码即可:

\addplot [gray!30] fill between[of=C and D, 
    soft clip={
        (axis cs:3.7E4, \pgfkeysvalueof{/pgfplots/ymin}) rectangle 
        (axis cs:5.5E5, \pgfkeysvalueof{/pgfplots/ymax})
    }
];

完整可运行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={
                    (axis cs:3.7E4, \pgfkeysvalueof{/pgfplots/ymin}) rectangle 
                    (axis cs:5.5E5, \pgfkeysvalueof{/pgfplots/ymax})
                }
            ];
        
        \end{loglogaxis}
        
    \end{tikzpicture}%
    
\end{document}

该方案不需要手动计算对数转换值,即使后续修改y轴范围也能自动适配,兼容所有坐标轴类型。

内容的提问来源于stack exchange,提问作者NoSilentRunning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:39:03