2

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:

enter image description here

3
  • 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

1 Answer 1

1

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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .