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:
1 Answers
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}
\usetikzlibrary{fadings, fit}
\begin{document}
\begin{tikzpicture}
\begin{axis}[tick style={transparent!100}]
\addplot[
transparent!0,
line width=2pt
] coordinates {(0,0) (1,1) (2,0.5) (3,1.5) (4,0)};
\end{axis}
\begin{tikzfadingfrompicture}[name=plot fading]
\begin{scope}[local bounding box=plot bbox]
\begin{axis}[tick style={transparent!100}]
\addplot[
transparent!0,
line width=2pt
] coordinates {(0,0) (1,1) (2,0.5) (3,1.5) (4,0)};
\end{axis}
\end{scope}
\end{tikzfadingfrompicture}
\fill[top color=blue, bottom color=cyan, path fading=plot fading, fit fading=false, fading transform={shift={(plot bbox.center)}}]
(plot bbox.north west) rectangle (plot bbox.south east);
\end{tikzpicture}
\end{document}
- 48,848


tikzfadingfrompicture, but this is probably a bit too complicated here. – Jasper Habicht Nov 28 '22 at 21:14