定义自定义颜色图

定义自定义颜色图

使用自定义颜色图这样就可以了:

\begin{axis}[
    mesh/interior colormap name=hot,
    colormap={slategraywhite}{rgb255=(112,128,144) rgb255=(255,159,101)},
 ]

下面第一个 MWE 得出:

在此处输入图片描述

但是,如果我在另一个图中需要这个,那么我必须复制颜色规范。因此,我更愿意定义一个自定义颜色图,名称​​为

\pgfplotsset{%
    colormap={slategraywhite}{rgb255=(112,128,144) rgb255=(255,159,101)}%
}%    

并重新使用它。但是,我无法让使用命名颜色图的第二个 MWE 工作。

参考:


代码:Manually Specified Colormap

\documentclass[border=2pt]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    hide axis,
    xlabel=$x$,ylabel=$y$,
    mesh/interior colormap name=hot,
    colormap={slategraywhite}{rgb255=(112,128,144) rgb255=(255,159,101)},% <--- This works fine.
 ]
  \addplot3 [domain=-1.5:1.5,surf, shader=faceted] {-exp(-x^2-y^2)};
\end{axis}
\end{tikzpicture}%
\end{document}

代码:Named Colormap

\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}%

\pgfplotsset{%
    colormap={slategraywhite}{rgb255=(112,128,144) rgb255=(255,159,101)}%
}%    
\pgfplotsset{colormap/slategraywhite}% <-- activate colormap
\pgfplotsset{colormap/blackwhite}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    hide axis,
    xlabel=$x$,ylabel=$y$,
    mesh/interior colormap name=hot,
    colormap/slategraywhite,%         <--- How do I get this working.
 ]
  \addplot3 [domain=-1.5:1.5,surf, shader=faceted] {-exp(-x^2-y^2)};
\end{axis}
\end{tikzpicture}%
\end{document}

答案1

这些colormap/<colormap name>东西是预定义的样式,即它们(i)要么定义颜色图本身,从而激活它们,要么(ii)通过调用它们的名称来激活它们

\pgfplotsset{
    % (i) define the colormap and activate it
    colormap={<colormap name>}{ ... },
    % (ii) call an already defined colormap to activate it
    colormap/<colormap name>/.style={
        colormap name=<colormap name>,
    },
}

当然,您没有对自定义颜色图执行此操作。因此,请参阅以下代码中的注释,了解如何激活它以全局或本地使用它。

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{colormaps}
\pgfplotsset{
    % this *defines* a custom colormap ...
    colormap={slategraywhite}{
        rgb255=(112,128,144)
        rgb255=(255,159,101)
    },
%    % ... but this command does not *activate* a custom colormap ...
%    colormap/slategraywhite,        % <-- activate colormap
%    % this could either be done here (globally), which makes it the default
%    % used colormap, by specifying ...
%    colormap name=slategraywhite,
}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        hide axis,
        mesh/interior colormap name=hot,
        % ... or you activate it here (locally)
        colormap name=slategraywhite,
    ]
        \addplot3 [domain=-1.5:1.5,surf, shader=faceted] {-exp(-x^2-y^2)};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案2

让我们看看包作者是如何完成这个工作的:

pgfplots.code.tex第 36 行输入pgfplotscolormap.code.tex。在后面的文件第 2372 行

\pgfplotscreatecolormap{hot}{color(0cm)=(blue); color(1cm)=(yellow); color(2cm)=(orange); color(3cm)=(red)}

pgfplots.code.tex4019-47行

/pgfplots/colormap/hot/.style={
  % attention: copied from pgfplots.colormap.code.tex:
  /pgfplots/colormap={hot}{color(0cm)=(blue); color(1cm)=(yellow); color(2cm)=(orange); color(3cm)=(red)}
},
/pgfplots/colormap/viridis/.style={%
  /pgfplots/colormap={viridis}{%
      rgb=(0.267,0.00487,0.32942)
      rgb=(0.28192,0.08966,0.41241)
      rgb=(0.28026,0.1657,0.4765)
      rgb=(0.26366,0.23763,0.51877)
      rgb=(0.23744,0.3052,0.54192)
      rgb=(0.20862,0.36775,0.55267)
      rgb=(0.18225,0.42618,0.55711)
      rgb=(0.1592,0.48224,0.55807)
      rgb=(0.13777,0.53749,0.5549)
      rgb=(0.12115,0.59274,0.54465)
      rgb=(0.12808,0.64775,0.5235)
      rgb=(0.18065,0.7014,0.48819)
      rgb=(0.27415,0.75198,0.4366)
      rgb=(0.39517,0.79747,0.36775)
      rgb=(0.53561,0.83578,0.2819)
      rgb=(0.68895,0.86545,0.18272)
      rgb=(0.84557,0.88733,0.0997)
      rgb=(0.99324,0.90616,0.14394)
  }%
},
% instantiate viridis such that it is in memory by default:
/pgfplots/colormap/viridis,
% ... but reuse hot since it is used to be the default since the
% beginning:
/pgfplots/colormap name=hot,
%
/pgfplots/colormap/hot2/.style={
  /pgfplots/colormap={hot2}{[1cm]rgb255(0cm)=(0,0,0) rgb255(3cm)=(255,0,0) rgb255(6cm)=(255,255,0)
  rgb255(8cm)=(255,255,255)}
},
/pgfplots/colormap/bluered/.style={
  /pgfplots/colormap={bluered}{rgb255(0cm)=(0,0,180); rgb255(1cm)=(0,255,255); rgb255(2cm)=(100,255,0);
  rgb255(3cm)=(255,255,0); rgb255(4cm)=(255,0,0); rgb255(5cm)=(128,0,0)}
},
/pgfplots/colormap/cool/.style={
  /pgfplots/colormap={cool}{rgb255(0cm)=(255,255,255); rgb255(1cm)=(0,128,255); rgb255(2cm)=(255,0,255)}
},
/pgfplots/colormap/greenyellow/.style={
  /pgfplots/colormap={greenyellow}{rgb255(0cm)=(0,128,0); rgb255(1cm)=(255,255,0)}
},
/pgfplots/colormap/redyellow/.style={
  /pgfplots/colormap={redyellow}{rgb255(0cm)=(255,0,0); rgb255(1cm)=(255,255,0)}
},
/pgfplots/colormap/blackwhite/.style={
  /pgfplots/colormap={blackwhite}{gray(0cm)=(0); gray(1cm)=(1)}
},
/pgfplots/colormap/violet/.style={
  /pgfplots/colormap={violet}{rgb255=(25,25,122) color=(white) rgb255=(238,140,238)}
},
/pgfplots/colormap/jet/.style={
  /pgfplots/colormap={jet}{rgb255(0cm)=(0,0,128) rgb255(1cm)=(0,0,255) rgb255(3cm)=(0,255,255) rgb255(5cm)=(255,255,0)
  rgb255(7cm)=(255,0,0) rgb255(8cm)=(128,0,0)}
},

在英语中,该包执行以下操作

  • 激活hot,使用hot。(仅限此内容pgfplotscolormap.code.tex
  • 定义hot
  • 定义viridis
  • 激活viridis,使用viridis而不是hot
  • 使用hot而不是viridis
  • 定义hot2
  • 定义bluered
  • 定义cool
  • 定义greenyellow
  • 定义redyellow
  • 定义blackwhite
  • 定义violet
  • 定义jet

因此状态是

  • hot已定义、激活并使用。(绿灯)

相关内容