使用 pgfplots 在 Latex Heatmap 中填充单元格

使用 pgfplots 在 Latex Heatmap 中填充单元格

这是生成的 pdf 的图像我使用 pgfplots 制作了热图。但单元格中的数字未垂直居中对齐。我如何将它们居中对齐,或者如何在底部添加填充以使它们出现在中心。

\documentclass{article}
\usepackage{colortbl}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{titlesec}
\usepackage[font=small,compatibility=false]{caption}
\usepackage{fancyhdr}
\usepackage[margin=0.5in]{geometry}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\titleformat{\chapter}
  {\normalfont\LARGE\bfseries\color{darkblue}}{\thechapter.}{1em}{}
\pagestyle{fancy}
\fancyhf{}

\lhead{Parent Feedback Report}
\chead{}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{Kaddy.co}

\addtolength{\topmargin}{.55in}
\newcolumntype{C}{>{\centering\arraybackslash}p{2mm}}

\pgfplotstableset{
    /color cells/min/.initial=0,
    /color cells/max/.initial=1000,
    /color cells/textcolor/.initial=,
    color cells/.code={%
        \pgfqkeys{/color cells}{#1}%
        \pgfkeysalso{%
            postproc cell content/.code={%
                \begingroup
                \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
                \ifx\value\empty
                \endgroup
                \else
                \pgfmathfloatparsenumber{\value}%
                \pgfmathfloattofixed{\pgfmathresult}%
                \let\value=\pgfmathresult
                \pgfplotscolormapaccess
                    [\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]%
                    {\value}%
                    {\pgfkeysvalueof{/pgfplots/colormap name}}%
                \pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
                \pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
                \toks0=\expandafter{\typesetvalue}%
                \xdef\temp{%
                    \noexpand\pgfkeysalso{%
                        @cell content={%
                            \noexpand\cellcolor[rgb]{\pgfmathresult}%
                            \noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
                            \ifx\textcolorvalue\empty
                            \else
                                \noexpand\color{\textcolorvalue}%
                            \fi
                            \the\toks0 %
                        }%
                    }%
                }%
                \endgroup
                \temp
                \fi
            }%
        }%
    }
}

\begin{document}
\begin{figure}\caption{HeatMap representation of parent feedback data}
\newcommand\Bstrut{\rule[-0.9ex]{0pt}{0cm}}
\newcolumntype{C}[1]{
 >{\vbox to 3.5ex\bgroup\vfill\centering}
 p{#1}
 <{\egroup}}  
\setlength\extrarowheight{5pt}
\parbox{.30\linewidth}{
\begin{tabular}{| C{9.5cm} |} 
    \hline
    Questions \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.\Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} My child is learning what he/she needs to know to succeed for next grades or after graduating from school.\Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{2em}A clear day with lots of sunshinejA clear day with lotsa as\Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline 
\end{tabular}
}
\hfill
\resizebox{3.5in}{3.94in}{\pgfplotstabletypeset[font=\fontsize{2}{8}\selectfont,
    /pgfplots/colormap={blackwhite}{rgb255=(255,170,0) color=(white) rgb255=(255,170,0)},
    color cells={min=1,max=2323,textcolor=black},
    /pgf/number format/fixed,
    /pgf/number format/precision=1,
    col sep=comma,
    row sep=crcr,
]{
SA,A,N,D,SD\\
1819, 2043, 126, 24, 3\\
2067, 1587, 218, 55, 10\\
986, 2018, 655, 147, 33\\
1257, 2068, 424, 61, 3\\
1722, 1977, 212, 32, 7\\
1322, 2036, 445, 102, 17\\
1264, 2119, 466, 60, 11\\
1205, 1952, 467, 252, 54\\
1855, 1776, 250, 55, 8\\
1638, 2029, 239, 58, 8\\
1953, 1686, 254, 33, 10\\
2020, 1758, 140, 23, 3\\
1835, 1843, 229, 41, 9\\
1371, 2178, 287, 51, 4\\
}}
\end{figure}
\end{document}

答案1

这是比较难的方法。你可以装载adjustbox包裹,将电池内容放入箱子中,然后抬起内容。

\usepackage{adjustbox}

\pgfplotstabletypeset选项中输入

postproc cell content/.append style={
    /pgfplots/table/@cell content/.add={\begin{adjustbox}{raise=1.25ex}}{\end{adjustbox}}},

适当调整1.25ex

代码:

\documentclass{article}
\usepackage{colortbl}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{titlesec}
\usepackage[font=small,compatibility=false]{caption}
\usepackage{fancyhdr,adjustbox}
\usepackage[margin=0.5in]{geometry}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\titleformat{\chapter}
  {\normalfont\LARGE\bfseries\color{darkblue}}{\thechapter.}{1em}{}
\pagestyle{fancy}
\fancyhf{}

\lhead{Parent Feedback Report}
\chead{}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{Kaddy.co}

\addtolength{\topmargin}{.55in}
\newcolumntype{C}{>{\centering\arraybackslash}p{2mm}}

\pgfplotstableset{
    /color cells/min/.initial=0,
    /color cells/max/.initial=1000,
    /color cells/textcolor/.initial=,
    color cells/.code={%
        \pgfqkeys{/color cells}{#1}%
        \pgfkeysalso{%
            postproc cell content/.code={%
                \begingroup
                \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
                \ifx\value\empty
                \endgroup
                \else
                \pgfmathfloatparsenumber{\value}%
                \pgfmathfloattofixed{\pgfmathresult}%
                \let\value=\pgfmathresult
                \pgfplotscolormapaccess
                    [\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]%
                    {\value}%
                    {\pgfkeysvalueof{/pgfplots/colormap name}}%
                \pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
                \pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
                \toks0=\expandafter{\typesetvalue}%
                \xdef\temp{%
                    \noexpand\pgfkeysalso{%
                        @cell content={%
                            \noexpand\cellcolor[rgb]{\pgfmathresult}%
                            \noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
                            \ifx\textcolorvalue\empty
                            \else
                                \noexpand\color{\textcolorvalue}%
                            \fi
                            \the\toks0 %
                        }%
                    }%
                }%
                \endgroup
                \temp
                \fi
            }%
        }%
    }
}

\begin{document}
\begin{figure}\caption{HeatMap representation of parent feedback data}
\newcommand\Bstrut{\rule[-0.9ex]{0pt}{0cm}}
\newcolumntype{C}[1]{
 >{\vbox to 3.5ex\bgroup\vfill\centering}
 p{#1}
 <{\egroup}}
\setlength\extrarowheight{5pt}
\parbox{.30\linewidth}{
\begin{tabular}{| C{9.5cm} |}
    \hline
    Questions \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.\Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} My child is learning what he/she needs to know to succeed for next grades or after graduating from school.\Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{2em}A clear day with lots of sunshinejA clear day with lotsa as\Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
    \rule{0pt}{1.5em} A clear day with lots of sunshine.A clear day with lots of sunshine.
    \Bstrut \tabularnewline \hline
\end{tabular}
}
\hfill
\resizebox{3.5in}{3.94in}{\pgfplotstabletypeset[font=\fontsize{2}{8}\selectfont,
    /pgfplots/colormap={blackwhite}{rgb255=(255,170,0) color=(white) rgb255=(255,170,0)},
    color cells={min=1,max=2323,textcolor=black},
    /pgf/number format/fixed,
    /pgf/number format/precision=1,
    col sep=comma,
    row sep=crcr,
    postproc cell content/.append style={
    /pgfplots/table/@cell content/.add={\begin{adjustbox}{raise=1.25ex}}{\end{adjustbox}}},
]{
SA,A,N,D,SD\\
1819, 2043, 126, 24, 3\\
2067, 1587, 218, 55, 10\\
986, 2018, 655, 147, 33\\
1257, 2068, 424, 61, 3\\
1722, 1977, 212, 32, 7\\
1322, 2036, 445, 102, 17\\
1264, 2119, 466, 60, 11\\
1205, 1952, 467, 252, 54\\
1855, 1776, 250, 55, 8\\
1638, 2029, 239, 58, 8\\
1953, 1686, 254, 33, 10\\
2020, 1758, 140, 23, 3\\
1835, 1843, 229, 41, 9\\
1371, 2178, 287, 51, 4\\
}}
\end{figure}
\end{document}

在此处输入图片描述

相关内容