10

I'm trying to achieve a fade-out effect of my plot curve to the sides without affecting the grid. However, as I'm using path fading with white, the grid is also affected. Ideally this would be done by a separate command that I can just add after the plot like in my example, but any way to achieve the desired effect would be appreciated. A simple solution would be the use of axis on top with the fading inside the axis environment but I'd like to have the lines behind the fading plot.

MWE: enter image description here

\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}

Thank you for any help!

Dwarfworld
  • 103
  • 5

1 Answers1

8

I do not know if it's possible to concot this into a simple style. One idea, after reading your comments, is:

  • split the domain in one un-fading and other fading;
  • apply the east fading (the fadings are only vertical or horizontal, I fear, so a north one is invisible on a narrow line)

like this:

\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}

enter image description here

Original answer (simple fading)

Hmmm... why two axis environments and the separate fading?

\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}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • Thanks! Can you update the solution so that the complete plot is affected by the fade to both corners over a small area like in the MWE? The fading outside of the plot is done as I have a document with multiple plots and I'd like to define (and change) the fading in one central definition and just add a command for each plot document. But I would accept an answer that fulfils the rest :-) – Dwarfworld Mar 23 '23 at 10:46
  • @Dwarfworld it is, although it's not easy to see (remove the opacity=0.5 to see it better). I am no expert on path fading option, you can try looking at https://tikz.dev/tikz-actions#sec-15.9; let's see if someone else can add a better answer! – Rmano Mar 23 '23 at 10:51
  • ...hmmm, really fadings are little documented... better look also at https://tikz.dev/base-shadings – Rmano Mar 23 '23 at 11:03
  • I mean it doesn't fade in the x-direction and path fading=north doesn't seem to have any effect on the line plots when I add it to them. Unfortunately I ran into the same problems when I tried it with shading. – Dwarfworld Mar 23 '23 at 11:37
  • 1
    Thanks! I think I can work with the second example. Combining it with this answer, the fading can be set without the domain split. However, I can then use the domain split for examples where the plot starts from the top/ north and ends on the right/ east to have both, a vertical and horizontal fading. – Dwarfworld Mar 23 '23 at 15:08
  • 1
    There might be cases where this doesn't work (wide area in the top right corner), however this answer might help in that case. As I can work with your example I mark it as solved. – Dwarfworld Mar 23 '23 at 15:09
  • 1
    Very nice links, thanks. – Rmano Mar 23 '23 at 15:50