如何生成热图?

如何生成热图?

我最近正在阅读一篇论文,在论文中看到了这张图表: 在此处输入图片描述

它基本上显示了近似算法在 x 和 y 变化时的精度。有人知道这是什么样的包吗?

答案1

它的想法如下:

您必须创建一些椭圆(它们有两个中心)并计算每个椭圆中心距离的总和。如果任何点(一般来说)的距离小于给定值,则用颜色为该点对应的框着色(红色表示较近的点,橙色表示较远的点等)

执行此类操作的代码如下:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usetikzlibrary{calc}

\xdef\YintSquares{70}
\xdef\XintSquares{70}
\xdef\factor{20}
\xdef\numEclipse{3}
\xdef\numColors{5}
\xdef\DefCol{blue}

\def\AddFCenter#1#2#3{
\xdef\tempFX{#2}
\global\expandafter\let\csname CenterFX#1\endcsname\tempFX
\xdef\tempFY{#3}
\global\expandafter\let\csname CenterFY#1\endcsname\tempFY}

\def\AddSCenter#1#2#3{
\xdef\tempFX{#2}
\global\expandafter\let\csname CenterSX#1\endcsname\tempFX
\xdef\tempFY{#3}
\global\expandafter\let\csname CenterSY#1\endcsname\tempFY}

\AddFCenter{1}{15}{24}
\AddSCenter{1}{16}{41}
\AddFCenter{2}{47}{53}
\AddSCenter{2}{50}{59}
\AddFCenter{3}{50}{30}
\AddSCenter{3}{44}{28}

\def\AddColorDistance#1#2#3{
\xdef\MyDist{#2}
\xdef\MyColor{#3}
\global\expandafter\let\csname UntilDistance#1\endcsname\MyDist
\global\expandafter\let\csname ColorAtDistance#1\endcsname\MyColor
}

\AddColorDistance{1}{17}{red}
\AddColorDistance{2}{19}{red!70!orange}
\AddColorDistance{3}{23}{orange}
\AddColorDistance{4}{27}{orange!70!yellow}
\AddColorDistance{5}{30}{yellow}

\xdef\DrawLW{0.01mm}




\pgfmathsetmacro\circleRarious{1/\factor/sqrt(2)/2}


\begin{document}

\begin{tikzpicture}
   \foreach \x in {0,...,\XintSquares}
   {
      \foreach \y in {0,...,\YintSquares}{
         \foreach \n in {1,...,\numEclipse}{
            \pgfmathsetmacro\distF{int(sqrt((\y-\csname CenterFY\n\endcsname)^2+(\x-\csname CenterFX\n\endcsname)^2))}
            \pgfmathsetmacro\distS{int(sqrt((\y-\csname CenterSY\n\endcsname)^2+(\x-\csname CenterSX\n\endcsname)^2))}
            \pgfmathsetmacro\dist{int(sqrt((\y-\csname CenterFY\n\endcsname)^2+(\x-\csname CenterFX\n\endcsname)^2)
            +sqrt((\y-\csname CenterSY\n\endcsname)^2+(\x-\csname CenterSX\n\endcsname)^2))}
            \ifcsname ValueOf\x And \y\endcsname
               \pgfmathsetmacro\tempbef{\csname ValueOf\x And \y\endcsname}
               \ifnum\tempbef<\dist
               \relax
               \else
               \global\expandafter\let\csname ValueOf\x And \y\endcsname\dist
               \fi
           \else
               \global\expandafter\let\csname ValueOf\x And \y\endcsname\dist
            \fi
            \ifnum\distF=0
            \global\expandafter\let\csname TrueCenterX\x Y\y\endcsname\distF
            \else
               \ifnum\distS=0
               \global\expandafter\let\csname TrueCenterX\x Y\y\endcsname\distS
               \else
               \relax
               \fi
            \fi
         }
         \xdef\curVal{\csname ValueOf\x And \y\endcsname}
         \ifcsname TrueCenterX\x Y\y\endcsname
            \xdef\OnCenter{1}
         \else
            \xdef\OnCenter{0}
         \fi
           \ifnum\OnCenter=1
              \draw[line width=\DrawLW,fill=red] ($(\x/\factor,\y/\factor)$)--($({(\x+1)/\factor},\y/\factor)$)--($({(\x+1)/\factor},{(\y+1)/\factor})$)--($(\x/\factor,{(\y+1)/\factor})$)--cycle;
              \draw[,fill=black] ($({(\x/\factor+(\x+1)/\factor)/2},{(\y/\factor+(\y+1)/\factor)/2})$) circle (\circleRarious);
            \else
              \xdef\BoolFillDefault{1}
              \foreach \col in {1,...,\numColors}{
              \xdef\valforCol{\csname UntilDistance\col\endcsname}
              \ifnum\curVal<\valforCol

                 \xdef\Col{\csname ColorAtDistance\col\endcsname}
                 \draw[line width=\DrawLW,fill=\Col] ($(\x/\factor,\y/\factor)$)--($({(\x+1)/\factor},\y/\factor)$)--($({(\x+1)/\factor},{(\y+1)/\factor})$)--($(\x/\factor,{(\y+1)/\factor})$)--cycle;
                 \xdef\BoolFillDefault{0}
                 \breakforeach              
              \fi
              }
             \ifnum\BoolFillDefault=1
                \draw[line width=\DrawLW,fill=\DefCol] ($(\x/\factor,\y/\factor)$)--($({(\x+1)/\factor},\y/\factor)$)--($({(\x+1)/\factor},{(\y+1)/\factor})$)--($(\x/\factor,{(\y+1)/\factor})$)--cycle;
             \fi
          \fi
      }
   }
\end{tikzpicture}


\end{document}

结果是:

在此处输入图片描述

我使用的颜色距离对于所有椭圆来说都是相同的,这意味着您必须根据中心距离绘制重叠的椭圆。

相关内容