![有没有办法在 tikz 图中制作依赖于 y 的渐变?](https://linux22.com/image/457988/%E6%9C%89%E6%B2%A1%E6%9C%89%E5%8A%9E%E6%B3%95%E5%9C%A8%20tikz%20%E5%9B%BE%E4%B8%AD%E5%88%B6%E4%BD%9C%E4%BE%9D%E8%B5%96%E4%BA%8E%20y%20%E7%9A%84%E6%B8%90%E5%8F%98%EF%BC%9F.png)
答案1
可能有更简单的方法来实现这一点,但你可以从图中创建一个淡入淡出并将其应用于填充渐变的矩形:
\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}