淡出图而不影响网格

淡出图而不影响网格

我正在尝试实现绘图曲线向两侧淡出的效果,而不会影响网格。但是,由于我使用的path fading是白色,因此网格也会受到影响。理想情况下,这将通过一个单独的命令来完成,我可以像在我的示例中一样在绘图后添加该命令,但任何实现所需效果的方法都会受到赞赏。一个简单的解决方案是在环境axis on top内使用淡入淡出,axis但我希望线条位于淡入淡出的绘图后面。

梅威瑟: 在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{fadings}
\usepgfplotslibrary{fillbetween}

\begin{document} 
\begin{tikzpicture}
    \begin{axis}[thick,smooth,no markers,grid=both,axis x line=bottom,axis y line = left,]
        \addplot+[name path=A,blue!50] {sqrt(x)};
        \addplot+[name path=B,blue!50] {sqrt(x/2)};

        \addplot[blue!50,opacity=0.5] fill between[of=A and B];
    \end{axis}

 % fading
\fill [path fading=south,white]([yshift=0.1cm]current axis.north west) rectangle ([yshift=-0.5cm]current axis.north east);
\fill [path fading=west,white]  ([xshift=-1.5cm]current axis.south east) rectangle ([xshift=0.1cm]current axis.north east);    
% secondary axis so the x and y lines aren't faded (ideally not required)
 \begin{axis}[%
    grid=none,
    axis x line=bottom,
    axis y line = left,
    minor tick num=0,
    xlabel=, 
    ylabel=,
    grid=none,
    ticks=none]%in m
\end{axis}   
\end{tikzpicture}

\end{document}

感谢您的任何帮助!

答案1

我不知道是否可以把它变成简单的风格。读了你的评论后,一个想法是:

  • 将域分为一个不衰落域和另一个衰落域;
  • 应用east淡入淡出(我担心淡入淡出只是垂直或水平的,所以在狭窄的线上看不到北方的淡入淡出)

像这样:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{fadings}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        thick,smooth,no markers,grid=both,axis x line=bottom,axis y line = left,
        grid, domain=0.5:5
        ]
        \addplot+[domain=0.5:4,name path=A1, blue!50] {sqrt(x)};
        \addplot+[domain=0.5:4,name path=B1, blue!50] {sqrt(x/2)};
        \addplot[blue!50, opacity=0.5,] fill between[of=A1 and B1];
        %
        \addplot+[domain=4:5,name path=A,blue!50, path fading=east] {sqrt(x)};
        \addplot+[domain=4:5,name path=B,blue!50, path fading=east] {sqrt(x/2)};
        \addplot[blue!50, opacity=0.5,path fading=east] fill between[of=A and B];
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

原始答案(简单淡出)

嗯...为什么有两个轴环境和单独的衰落?

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.18}% <<<< don't forget this! (It's in the warnings!)
\usetikzlibrary{fadings}
\usepgfplotslibrary{fillbetween}

\begin{document} 
\begin{tikzpicture}
    \begin{axis}[
        thick,smooth,no markers,grid=both,axis x line=bottom,axis y line = left,
        grid,
        ]
        \addplot+[name path=A,blue!50] {sqrt(x)};
        \addplot+[name path=B,blue!50] {sqrt(x/2)};
        \addplot[blue!50,opacity=0.5, path fading=north] fill between[of=A and B];
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容