根据值(某种热图)选择填充颜色

根据值(某种热图)选择填充颜色

我有一组预定义的坐标,\where,以及一个对应的值。每个坐标覆盖一个矩形。我喜欢根据值\value选择选项的颜色。结果是热图 - 但绘制在 之外。fill=...pgfplot

我已经看到pgfplot可以绘制热图,但是我需要在现有图形上绘制地图。尽管,绘图colormap/jetpgfplot接缝确实很好。因此,如果可能的话,我喜欢有一个选择颜色的函数colormap/jet。我还需要一个条形图(颜色,对应值)。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
  %existing figure
    \draw (0,1) circle (1);
    \draw (1,2) circle (.7);
  %heatmap
    \foreach \where/\value in
    { (0,0)/0.5,(1,0)/0.3,(2,0)/0.1, %coordinates and values between 0 and 1
      (0,1)/0.3,(1,1)/0.2,(2,1)/0.05,
      (0,2)/0.1,(1,2)/0.4,(2,2)/0.2%
    }
    { \path [overlay] \where coordinate (A);
      \draw [fill=orange, , opacity=0.2] ($(A)-(.5,.5)$) rectangle ($(A)+(.5,.5)$); } %fill with the correct color
    %add a color bar
  \end{tikzpicture}
\end{document}

答案1

使用该xcolor包,您可以使用语法

<color>!<percent>

获得颜色的阴影:

在此处输入图片描述

代码:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
  %existing figure
    \draw (0,1) circle (1);
    \draw (1,2) circle (.7);
  %heatmap
    \foreach \where/\value in
    { (0,0)/50,(1,0)/30,(2,0)/10, %coordinates and values between 0 and 100
      (0,1)/30,(1,1)/20,(2,1)/5,
      (0,2)/10,(1,2)/40,(2,2)/20%
    }
    { \path [overlay] \where coordinate (A); 
      \draw [fill=red!\value, opacity=0.2] ($(A)-(.5,.5)$) rectangle ($(A)+(.5,.5)$); } %fill with the correct color
    %add a color bar
  \end{tikzpicture}
\end{document}

答案2

您可以计算 rgb 值并相应地为方块着色。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{xcolor}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}

        %heatmap
            \foreach \where/\value in { 
                (0,0)/0.5,(1,0)/0.3,(2,0)/0.1, %coordinates and values between 0 and 1
                (0,1)/0.3,(1,1)/0.2,(2,1)/0.05,
                (0,2)/0.1,(1,2)/0.4,(2,2)/0.2%
            }{ 
                \path [overlay] \where coordinate (A);

                \pgfmathsetmacro \v \value
                \ifthenelse{\lengthtest{\v pt>0.333pt}}{
                    \pgfmathsetmacro \r {1}
                    \pgfmathsetmacro \g {1-(\value-.333)/.667}
                    \pgfmathsetmacro \b {0}
                }{
                    \pgfmathsetmacro \r {\value/.333}
                    \pgfmathsetmacro \g {\value/.333}
                    \pgfmathsetmacro \b {1-\value/.333}
                }
                \definecolor{temp}{rgb}{\r, \g, \b}

                \draw [fill=temp] ($(A)-(.5,.5)$) rectangle ($(A)+(.5,.5)$);
            }

        %colour bar
            \fill[top color=red, bottom color=yellow] (-1.5,.5) rectangle (-2,2.5) node[left] {1};
            \fill[top color=yellow, bottom color=blue] (-2,-.5) node[left] {0} rectangle (-1.5,.5);

        %existing figure
            \draw (0,1) circle (1);
            \draw (1,2) circle (.7);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{xcolor}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}

        %heatmap
            \foreach \where/\value in { (0,0)/0.0,(1,0)/0.1,(2,0)/0.2, (0,1)/0.3,(1,1)/0.4,(2,1)/0.5, (0,2)/0.6,(1,2)/0.7,(2,2)/0.8, (0,3)/0.9,(1,3)/1.0 } { 
                \path [overlay] \where coordinate (A);

                \pgfmathsetmacro \v \value
                \ifthenelse{\lengthtest{\v pt>0.333pt}}{
                    \pgfmathsetmacro \r {1}
                    \pgfmathsetmacro \g {1-(\value-.333)/.667}
                    \pgfmathsetmacro \b {0}
                }{
                    \pgfmathsetmacro \r {\value/.333}
                    \pgfmathsetmacro \g {\value/.333}
                    \pgfmathsetmacro \b {1-\value/.333}
                }
                \definecolor{temp}{rgb}{\r, \g, \b}

                \draw [fill=temp] ($(A)-(.5,.5)$) rectangle ($(A)+(.5,.5)$);
            }

        %colour bar
            \fill[top color=red, bottom color=yellow] (-1.5,1) rectangle (-2,3) node[left] {1};
            \fill[top color=yellow, bottom color=blue] (-2,0) node[left] {0} rectangle (-1.5,1);

        %existing figure
            \draw (0,1) circle (1);
            \draw (1,2) circle (.7);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

对于所示示例,我并不认为您需要在 PGFPlotsaxis环境之外绘制“现有图形”。但即使确实如此,您也可以将 PGFPlots 坐标系与 tikz 坐标系匹配然后使用 PGFPlots 提供的所有可能性来绘制热图。

我还想提一下,我会反过来画。所以首先画热图,然后在上面画“现有图形”。然后颜色就与颜色条中的颜色真正匹配了,颜色条没有任何透明度。(我不认为有一种简单的方法可以只让颜色条透明——没有框架、刻度和标签——这样它就会与情节相匹配。如果我错了,我很乐意在这里告诉你。)

请查看代码中的注释以了解更多详细信息。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.11}
\begin{document}
    \begin{tikzpicture}

        % if you really need to draw the figure outside the axis environment
        % of the PGFPlots package ...
        % -----------------------
        % existing figure
        \begin{scope}[
            thick,
            red,
        ]
            \draw (0,1) circle (1);
            \draw (1,2) circle (.7);
        \end{scope}
        % -----------------------

        \begin{axis}[
            % -----------------------------------------------------------------
            % in case you really want matching coordinate systems between
            % `tikz' and `pgfplots'
            % (taken from PGFPlots manual v1.13 section 4.26)
            % -----------------------------------------------------------------
            % tell pgfplots to "grab" the axis at its
            % internal (0,0) coord:
            anchor=origin,
            % tell pgfplots to place its anchor at (0,0):
            % (This is actually the default and can
            % be omitted)
            at={(0pt,0pt)},
            % tell pgfplots to use the "natural" dimensions:
            disabledatascaling,
            % tell pgfplots to use the same unit vectors
            % as tikz:
            x=1cm,y=1cm,
% -----
% not needed here, because the axes themselves are not needed (see below)
%            % range... try it out.
%            xmin=-1,xmax=3,
%            ymin=-1,ymax=3,
% -----
            % -----------------------------------------------------------------
            %
            % choose a colormap that fits your need and draw the colorbar
            % (to change position and style of the colorbar please have a look
            % at the PGFPlots manual section 4.9.13 (in v1.13))
            colormap/viridis,
            colorbar,
            %
            % set min and max vales for `meta' values (and therefore the colorbar)
            point meta min=0,
            point meta max=0.5,   % <-- here a lower value to better show the
                                  %     the differences of the given values
            %
            % use opacity, if you really want to draw the other figure below
            % the "heat map"
            matrix plot*/.append style={
                opacity=0.5,
            },
            % because the axes are not needed -> hide them
            hide axis,
            % and because without drawing the axes we do not need to specify
            % axis limits. But to ensure that all "non-\addplot" stuff is fully
            % drawn, disable clipping
            clip=false,
        ]

            % I think it is not a problem to draw your figure also inside
            % of the axis environment
            % -----------------------
            % existing figure
            \draw (0,1) circle (1);
            \draw (1,2) circle (.7);
            % -----------------------

            \addplot [
                matrix plot*,
                point meta=explicit,
%                draw=black,     % <-- change me, if the default doesn't fit your needs
            ]
            table [x=x,y=y,meta=z] {
                x   y   z
                0   0   0.5
                1   0   0.3
                2   0   0.1

                0   1   0.3
                1   1   0.2
                2   1   0.05

                0   2   0.1
                1   2   0.4
                2   2   0.2
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

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

相关内容