使用由三种颜色组成的颜色图根据值分配颜色

使用由三种颜色组成的颜色图根据值分配颜色

我正在尝试根据一些数值为线条分配颜色。

我想要从中采样颜色的颜色图包含三种颜色:紫色、红色和黄色(如下部颜色条所示)。

但是,我只能弄清楚如何使用跨越两种颜色的颜色图来分配颜色\colorlet

这是我的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\newcommand{\tension}{0.5}
\newcommand{\uppery}{18}


\centering
\begin{tikzpicture}

    \colorlet{color min hsb}[hsb]{red}
    \colorlet{color max hsb}[hsb]{yellow}
    
    \def\min{10}
    \def\max{0}

  \foreach \x/\y/\z in {0.01/3.26/4.78,7.63/0.90/1.56,0.25/3.44/9.06,2.22/0.95/0.63,0.73/2.47/9.09,2.22/0.86/5.12,2.46/3.56/9.41,8.98/2.08/5.07,4.97/1.08/2.75,4.50/9.64/9.35}{

    \pgfmathtruncatemacro\lambda{(\z-\min)/(\max-\min)*100}
    \colorlet{my color hsb}[rgb]{color min hsb!\lambda!color max hsb}
    
    \draw [line width=0.75mm, my color hsb] plot [smooth, tension=\tension] coordinates { (\x,\uppery)    (\y,0.5*\uppery) (\z,0) };
  }
    
    \pgfplotscolorbardrawstandalone[ 
        colorbar horizontal,
        colormap={gb}{color=(violet) color=(red) color=(yellow)},
        point meta min=0.84,
        point meta max=0.91,
        colorbar style={
            at={(0,0)},
            anchor=south west,
            width=10cm,
            }]

\end{tikzpicture}


\end{document}

我的代码的结果

答案1

您可以使用选项从颜色图中color of colormap选择颜色。请查看Choosing a Colormap Entry as Normal Colorpgfplots部分。您还可以查看如何在 TikZ 中自动定义离散颜色图了解有关此选项的更多信息。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\newcommand{\tension}{0.5}
\newcommand{\uppery}{18}


\centering
\begin{tikzpicture}

    \colorlet{color min hsb}[hsb]{violet}
    \colorlet{color central hsb}[hsb]{red}
    \colorlet{color max hsb}[hsb]{yellow}
    
    \pgfplotsset{colormap={mycolormap}{color(0)=(violet) color(50)=(red) color(100)=(yellow)}}
    
    
    \def\min{10}
    \def\max{0}

  \foreach \x/\y/\z in {0.01/3.26/4.78, 7.63/0.90/1.56, 
  0.25/3.44/9.06, 2.22/0.95/0.63, 0.73/2.47/9.09,
  2.22/0.86/5.12, 2.46/3.56/9.41, 8.98/2.08/5.07, 
  4.97/1.08/2.75, 4.50/9.64/9.35}{

%    \pgfmathtruncatemacro\lambda{int((\z-\min)/(\max-\min)*1000)}
    
    \draw [line width=0.75mm, color of colormap={\z*100 of mycolormap}, draw=.] plot [smooth, tension=\tension] coordinates { (\x,\uppery)    (\y,0.5*\uppery) (\z,0) };
  }
    
    \pgfplotscolorbardrawstandalone[ 
        colorbar horizontal,
        colormap={gb}{color=(violet) color=(red) color=(yellow)},
        point meta min=0.84,
        point meta max=0.91,
        colorbar style={
            at={(0,0)},
            anchor=south west,
            width=10cm,
            }]

\end{tikzpicture}


\end{document}

在此处输入图片描述

相关内容