# Is there a way to make a gradient in a tikz plot that is y dependent?

I am using pgfplots and tikz (with \begin(axis) ... \end(axis) ) to make a plot from somo data and I was wondering if it is possible to make a gradient in the plot that is dependent with the y axis, just like this:

• Since the average gradiient is zero, you probably want a new y axis to overlap the ranges. You can overlap two plots with one y axis on the left and one on the right. See tex.stackexchange.com/questions/538789/… for example. Nov 28, 2022 at 16:39
• With a continuous plot, this would be simple. But you are using a discrete plot that consists of lines that connect coordinates. I only can think of using tikzfadingfrompicture, but this is probably a bit too complicated here. Nov 28, 2022 at 21:14
• One could probably add more coordinates so that the single segments become reasonably small. I think this would be easier than to apply a gradient to every segment. Nov 29, 2022 at 8:08

There are probably easier ways to achieve this, but you could create a fading from the plot and apply it to a rectangle filled with the gradient:

\documentclass[border=10pt]{standalone}

\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}

\begin{axis}[tick style={transparent!100}]

transparent!0,
line width=2pt
] coordinates {(0,0) (1,1) (2,0.5) (3,1.5) (4,0)};

\end{axis}

\begin{scope}[local bounding box=plot bbox]
\begin{axis}[tick style={transparent!100}]

transparent!0,
line width=2pt
] coordinates {(0,0) (1,1) (2,0.5) (3,1.5) (4,0)};

\end{axis}
\end{scope}

(plot bbox.north west) rectangle (plot bbox.south east);

\end{tikzpicture}

\end{document}