如何在 pgfplots 中用变化的宽度填充通道中的颜色条?

如何在 pgfplots 中用变化的宽度填充通道中的颜色条?

假设我有一个包含两个函数和的图f(x)g(x)见下图)。现在我想用颜色图填充这两条曲线之间的通道,使底部为蓝色,顶部为红色。中间部分为黄色,是从蓝色到红色的过渡,它是通道宽度的函数。我可以使用黄色colorbar代替黄色来显示从蓝色到红色的过渡,它是通道宽度的函数吗? 在此处输入图片描述

答案1

感谢您的代码!(将来,请将其添加到问题中,以便其他人不必添加因在评论中添加代码而丢失的换行符。)

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.15} 
\begin{document} 
\begin{tikzpicture} 
\begin{axis}[width=7cm, height=9cm, clip=false, xmin=0,xmax=20, ymin=0,ymax=40,
    declare function={f(\y)=max(5,min(14,9*\y/20+1/2));
    g(\y)=max(10,min(16,6*\y/20+7));},
    view={0}{90},colorbar,
    /tikz/every pin edge/.style={stealth-,thin}] 
 \begin{scope}  
  \clip[variable=\y] plot[samples at={0,10,30,40}] ({f(\y)},\y)
   -- plot[samples at={40,30,10,0}] ({g(\y)},\y) -- cycle;
  \addplot3[surf,shader=interp,point meta={g(y)-f(y)},domain=5:16,
     domain y=0:40] {x};
 \end{scope}
 \draw[variable=\y] plot[samples at={0,10,30,40}] ({f(\y)},\y)
  -- plot[samples at={40,30,10,0}] ({g(\y)},\y) -- cycle;   
 \addplot3[black,thick,samples at={0,10,30,40},samples y=0] ({f(x)},x,0)
  coordinate[pos=0.55,pin=120:{$f(x)$}];
 \addplot3[black,thick,samples at={0,10,30,40},samples y=0] ({g(x)},x,0)
  coordinate[pos=0.45,pin=-60:{$g(x)$}];
\end{axis} 
\end{tikzpicture} 
\end{document}

在此处输入图片描述

这会“反转”颜色条:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.15} 
\begin{document} 
\begin{tikzpicture} 
\begin{axis}[width=7cm, height=9cm, clip=false, xmin=0,xmax=20, ymin=0,ymax=40,
    declare function={f(\y)=max(5,min(14,9*\y/20+1/2));
    g(\y)=max(10,min(16,6*\y/20+7));},
    view={0}{90},colorbar,
    /tikz/every pin edge/.style={stealth-,thin},
    colorbar style={yticklabel={$\pgfmathparse{-1*\tick}%
        \pgfmathprintnumber\pgfmathresult$}}] 
 \begin{scope}  
  \clip[variable=\y] plot[samples at={0,10,30,40}] ({f(\y)},\y)
   -- plot[samples at={40,30,10,0}] ({g(\y)},\y) -- cycle;
  \addplot3[surf,shader=interp,point meta={f(y)-g(y)},domain=5:16,
     domain y=0:40] {x};
 \end{scope}
 \draw[variable=\y] plot[samples at={0,10,30,40}] ({f(\y)},\y)
  -- plot[samples at={40,30,10,0}] ({g(\y)},\y) -- cycle;   
 \addplot3[black,thick,samples at={0,10,30,40},samples y=0] ({f(x)},x,0)
  coordinate[pos=0.55,pin=120:{$f(x)$}];
 \addplot3[black,thick,samples at={0,10,30,40},samples y=0] ({g(x)},x,0)
  coordinate[pos=0.45,pin=-60:{$g(x)$}];
\end{axis} 
\end{tikzpicture} 
\end{document}

在此处输入图片描述

相关内容