pgfplotstable 列宽和行高

pgfplotstable 列宽和行高

我正在使用 绘制一个仅包含颜色的大型热图\pgfplotstabletypeset。该表包含 103 列和 111 行,我正试图将其挤进13cm * 16cm为了节省水平空间,我将第一行的标签旋转了 90 度,但列仍然太宽。同时,行又太高。

我想指定表格单元格的宽度和高度。 我并不担心第一行/列中的标签会因此变得非常小。我该怎么做?

下面是我的 MWE。它的灵感来自这个问题。我发现关于 pgfplotstable 中列宽调整的唯一内容可以在第 2.3 节中找到pgfplotstablr 文档。我已经在我的 MWE 中应用了它,但它对我没什么帮助。

\documentclass{standalone}

\listfiles
\usepackage{colortbl}
\usepackage{array}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usetikzlibrary{pgfplots.colormaps}

\pgfplotsset{compat=1.9}
\pgfplotsset{
    colormap={myCustomColorMap}{
        color(0)=(blue);
        color(1)=(red);
    },
}

% https://tex.stackexchange.com/a/158985/31877
\pgfplotstableset{
    /color cells/min/.initial=0,
    /color cells/max/.initial=1000,
    /color cells/textcolor/.initial=,
    /color cells/colormap name/.initial=,% this was added
    %
    % Usage: 'color cells={min=<value which is mapped to lowest color>, 
    %   max = <value which is mapped to largest>}
    color cells/.code={%
        \pgfqkeys{/color cells}{#1}%
        \pgfkeysalso{%
            postproc cell content/.code={%
                \begingroup
                % acquire the value before any number printer changed it:
                \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
                \ifx\value\empty
                    \endgroup
                \else
                \pgfkeys{/pgf/fpu=true}%
                \pgfmathparse{log10(\value)}%
                \pgfmathfloattofixed{\pgfmathresult}%
                \let\value=\pgfmathresult
                \pgfkeys{/pgf/fpu=false}%
                % map that value:
                \pgfplotscolormapaccess
                    [\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
                    {\value}
                    {\pgfkeysvalueof{/color cells/colormap name}}% I CHANGED THIS
                % now, \pgfmathresult contains {<R>,<G>,<B>}
                % 
                % acquire the value AFTER any preprocessor or
                % typesetter (like number printer) worked on it:
                \pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
                \pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
                % tex-expansion control
                \toks0=\expandafter{\empty}%\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
            }%
        }%
    }
}

\pgfmathdeclarefunction{lg10}{1}{%
    \pgfmathparse{ln(#1)/ln(10)}%
}

\begin{filecontents}{data.csv}
item,Xa,Xb,Ya,Yb,Za,Zb
a,2,,2,,783764,783764
b,,2,,,500000,500000
c,,5,,,100000,100000
d,2,,8,,20000,20000
e,,,,,10000,10000
f,,,,3,5000,5000
g,,2,,,1000,1000
h,6,,,4,500,500
i,,,4,,100,100
j,,,,,50,50
k,,2,,,10,10
l,,,7,,5,5
m,,6,,3,2,2
n,,,,,1,1
\end{filecontents}

\begin{document}
\newcolumntype{C}{>{\centering\arraybackslash}p{1mm}}

\pgfplotstabletypeset[
    font=\tiny,
    every head row/.style={
        typeset cell/.code={
        \ifnum\pgfplotstablecol=1
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{&}%
        \else
            \ifnum\pgfplotstablecol=\pgfplotstablecols
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rotatebox{90}{##1}\\}%
            \else
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rotatebox{90}{##1}&}%
            \fi
        \fi
        }
    },
    %max=lg10(783764)
    colorCell/.style={
        column type=C,
        color cells={min=0,max=5.9,colormap name=myCustomColorMap}
    },
    itemCell/.style={
        string type,
        column name={},
    },
    col sep=comma,
    columns={item,Xa,Xb,Ya,Yb,Za,Zb},
    columns/item/.style={itemCell},
    columns/Xa/.style={colorCell},
    columns/Xb/.style={colorCell},
    columns/Ya/.style={colorCell},
    columns/Yb/.style={colorCell},
    columns/Za/.style={colorCell},
    columns/Zb/.style={colorCell},
]{data.csv}

\end{document}

渲染

答案1

最简单的解决方案可能是将 放入命令pgfplotstable\resizebox。从维基百科

该软件包具有可用于指定表格高度和宽度的graphicx命令。\resizebox{width}{height}{object}tabular

\documentclass{standalone}

\listfiles
\usepackage{colortbl}
\usepackage{array}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{graphicx}
\usetikzlibrary{pgfplots.colormaps}

\pgfplotsset{compat=1.9}
\pgfplotsset{
    colormap={myCustomColorMap}{
        color(0)=(blue);
        color(1)=(red);
    },
}

% http://tex.stackexchange.com/a/158985/31877
\pgfplotstableset{
    /color cells/min/.initial=0,
    /color cells/max/.initial=1000,
    /color cells/textcolor/.initial=,
    /color cells/colormap name/.initial=,% this was added
    %
    % Usage: 'color cells={min=<value which is mapped to lowest color>, 
    %   max = <value which is mapped to largest>}
    color cells/.code={%
        \pgfqkeys{/color cells}{#1}%
        \pgfkeysalso{%
            postproc cell content/.code={%
                \begingroup
                % acquire the value before any number printer changed it:
                \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
                \ifx\value\empty
                    \endgroup
                \else
                \pgfkeys{/pgf/fpu=true}%
                \pgfmathparse{log10(\value)}%
                \pgfmathfloattofixed{\pgfmathresult}%
                \let\value=\pgfmathresult
                \pgfkeys{/pgf/fpu=false}%
                % map that value:
                \pgfplotscolormapaccess
                    [\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
                    {\value}
                    {\pgfkeysvalueof{/color cells/colormap name}}% I CHANGED THIS
                % now, \pgfmathresult contains {<R>,<G>,<B>}
                % 
                % acquire the value AFTER any preprocessor or
                % typesetter (like number printer) worked on it:
                \pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
                \pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
                % tex-expansion control
                \toks0=\expandafter{\empty}%\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
            }%
        }%
    }
}

\pgfmathdeclarefunction{lg10}{1}{%
    \pgfmathparse{ln(#1)/ln(10)}%
}

\begin{filecontents}{data.csv}
item,Xa,Xb,Ya,Yb,Za,Zb
a,2,,2,,783764,783764
b,,2,,,500000,500000
c,,5,,,100000,100000
d,2,,8,,20000,20000
e,,,,,10000,10000
f,,,,3,5000,5000
g,,2,,,1000,1000
h,6,,,4,500,500
i,,,4,,100,100
j,,,,,50,50
k,,2,,,10,10
l,,,7,,5,5
m,,6,,3,2,2
n,,,,,1,1
\end{filecontents}

\newcolumntype{C}{>{\centering\arraybackslash}p{1mm}}



\begin{document}
\resizebox{3in}{2in}{\pgfplotstabletypeset[
    font=\tiny,
    every head row/.style={
        typeset cell/.code={
        \ifnum\pgfplotstablecol=1
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{&}%
        \else
            \ifnum\pgfplotstablecol=\pgfplotstablecols
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rotatebox{90}{##1}\\}%
            \else
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\rotatebox{90}{##1}&}%
            \fi
        \fi
        }
    },
    %max=lg10(783764)
    colorCell/.style={
        column type=C,
        color cells={min=0,max=5.9,colormap name=myCustomColorMap}
    },
    itemCell/.style={
        string type,
        column name={},
    },
    col sep=comma,
    columns={item,Xa,Xb,Ya,Yb,Za,Zb},
    columns/item/.style={itemCell},
    columns/Xa/.style={colorCell},
    columns/Xb/.style={colorCell},
    columns/Ya/.style={colorCell},
    columns/Yb/.style={colorCell},
    columns/Za/.style={colorCell},
    columns/Zb/.style={colorCell},
]{data.csv}}
\end{document}

在此处输入图片描述

然而,奇怪的是,尽管我指定了选项width=3in和,height=2in\resizebox由于某种原因,我的输出宽度为 3.01 英寸,高度为 3.9 英寸。我不知道为什么\resizebox乘法因子会导致高度错误。

答案2

根据我在如何使用 TikZ 将值映射到颜色?,我能够通过使用R编程语言预处理输入文件,然后使用 TikZ 和循环绘制每个矩形来完成您想要的操作\foreach。(答案在需要一些帮助来开始使用 TikZ、groupplots 和矩阵的颜色映射使用类似的方法pgfplotstable,其中答题者不使用,而是使用 TikZ 绘制自己的“表格”。不幸的是,我对 TeX 了解不够多,无法在 TeX 中完成所有处理,因此我在 中完成了处理R。(但您可以使用自己喜欢的编程语言代替R)。

首先,我将输入文件保存为文本文件2014-02-09.txt

接下来,我运行一个R脚本来计算以 10 为底的对数,并将输入文件中的条目输出到 LaTeX 列表中。(注意:R是一种广泛使用的统计编程语言。)

temp <- read.table('2014-02-09.txt', sep=',', header = TRUE)

sink('2014-02-09-converted.tex')
prestring <- ''
cat('\\newcommand*{\\ListOfRectangles}{')
cat('%x/y/value\n')
for (i in 1:nrow(temp)) {
  for (j in 2:ncol(temp)) {
    value <- log(temp[i,j]) / log(10)
    if (!is.na(value)) {
      cat(sprintf('%s%d/%d/%.2f', prestring, j, i, value))
      prestring <- ',\n'
    }
  }
}
cat('%\n}')
sink()

输出是一个文件2014-02-09-converted.tex,其内容如下:

\newcommand*{\ListOfRectangles}{%x/y/value
2/1/0.30,
4/1/0.30,
6/1/5.89,
7/1/5.89,
3/2/0.30,
6/2/5.70,
7/2/5.70,
3/3/0.70,
6/3/5.00,
7/3/5.00,
2/4/0.30,
4/4/0.90,
6/4/4.30,
7/4/4.30,
6/5/4.00,
7/5/4.00,
5/6/0.48,
6/6/3.70,
7/6/3.70,
3/7/0.30,
6/7/3.00,
7/7/3.00,
2/8/0.78,
5/8/0.60,
6/8/2.70,
7/8/2.70,
4/9/0.60,
6/9/2.00,
7/9/2.00,
6/10/1.70,
7/10/1.70,
3/11/0.30,
6/11/1.00,
7/11/1.00,
4/12/0.85,
6/12/0.70,
7/12/0.70,
3/13/0.78,
5/13/0.48,
6/13/0.30,
7/13/0.30,
6/14/0.00,
7/14/0.00%
}

最后,我编译了 LaTeX 代码,它\input是之前计算的列表。请注意,通过改变xscaleyscale选项,您可以更改 PDF 输出的大小。

\documentclass{standalone}

\usepackage{tikz}

\input{2014-02-09-converted}

\begin{document}
% to change the output size of the PDF, change the xscale and yscale values.
\begin{tikzpicture}[xscale=0.5,yscale=0.3]
  \colorlet{color min}[rgb]{blue}
  \colorlet{color max}[rgb]{red}

  \def\min{0}
  \def\max{5.89}

  \foreach \sX/\sY/\myvalue in \ListOfRectangles {
    \pgfmathtruncatemacro\lambda{(\max-\myvalue)/(\max-\min)*100}
    \colorlet{my color}[rgb]{color min!\lambda!color max}
    \fill[my color] (\sX, -\sY) rectangle +(1cm,1cm);
  }
\end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

相关内容