![\pgfplotstabletypeset 热图:对数尺度](https://linux22.com/image/283370/%5Cpgfplotstabletypeset%20%E7%83%AD%E5%9B%BE%EF%BC%9A%E5%AF%B9%E6%95%B0%E5%B0%BA%E5%BA%A6.png)
我正在尝试创作自己的演绎这个热图。我的值范围从1
到783764
。我想将它们绘制为对数(以 10 为底)缩放的颜色图上的颜色。因此,值的对数表示范围应从0
到~5.894
。
当我设置最大值时color cells={min=1,max=783764}
,出现以下错误
!尺寸太大。
<待重读>
\relax
l.104 ]{data.csv}
通过实验,我发现这16383
似乎是最大值不是给我一个错误。由于我不知道如何完成我预期的最大783764
工作量,我尝试进行一些预处理(请参阅pgfplotstable 文档第 3.3 节),并通过我自己的lg10
函数提取我的所有值:preproc/expr={lg10(##1)}
但是,那然后又给了我另一个错误:
!软件包 PGF 数学错误:抱歉,浮点单元的内部例程收到格式错误的浮点数“1”。不可读部分位于“1”附近。
首先,这很奇怪,因为1
我尝试过的数据表中没有值。其次,我尝试附加.0
到所有(整数)值,但没有任何区别。
我怎样才能让它工作?
这是我的最小非工作示例:
\documentclass{standalone}
\listfiles
\usepackage{colortbl}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usetikzlibrary{pgfplots.colormaps}
\pgfplotsset{compat=1.9}
% https://tex.stackexchange.com/a/83865/31877
\pgfplotstableset{
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
%
% 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
\pgfmathfloatparsenumber{\value}%
\pgfmathfloattofixed{\pgfmathresult}%
\let\value=\pgfmathresult
% map that value:
\pgfplotscolormapaccess
[\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
{\value}
{\pgfkeysvalueof{/pgfplots/colormap name}}%
% 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{\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,Zc
a,2,,2,,,
b,,2,,,4,2
c,,5,,,,
d,2,,8,,,
e,,,,,5,5
f,,,,3,,
g,,2,,,,
h,6,,,4,,
i,,,4,,,
j,,,,,,2
k,,2,,,3,2
l,,,7,,,
m,,6,,3,,
n,,,,,,7
\end{filecontents}
\begin{document}
\pgfplotstabletypeset[
%font=\tiny,
colorCell/.style={
preproc/expr={lg10(##1)},%fails to work on ANY number
color cells={min=1,max=16383}%max should be 783764
},
itemCell/.style={
string type,
column name={},
},
col sep=comma,
columns={item,Xa,Xb,Ya,Yb,Za,Zc},
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
问题出在以下发布的样式上https://tex.stackexchange.com/a/83865/31877:显然,颜色图访问方法需要一个数字,该数字是不是以浮点表示形式。一个快速修复方法是写入\pgfkeys{/pgf/fpu=false}
样式color cells
。
但是:我想你想排版原始数字,而不是它的对数,对吧?你也可以在样式中应用对数color cells
。这就是我在这里所做的 - 它不需要特殊的预处理步骤:
\documentclass{standalone}
\listfiles
\usepackage{colortbl}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usetikzlibrary{pgfplots.colormaps}
\pgfplotsset{compat=1.9}
% https://tex.stackexchange.com/a/83865/31877
\pgfplotstableset{
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
%
% 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}%
%\pgfmathfloatparsenumber{\value}%
\pgfmathparse{log10(\value)}%
\pgfmathfloattofixed{\pgfmathresult}%
\let\value=\pgfmathresult
\pgfkeys{/pgf/fpu=false}% this here was the problem
% map that value:
\pgfplotscolormapaccess
[\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
{\value}
{\pgfkeysvalueof{/pgfplots/colormap name}}%
% 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{\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,Zc
a,2,,2,,,
b,,2,,,4,2
c,,5,,,,
d,2,,8,,,
e,,,,,5,5
f,,,,3,,
g,,2,,,,
h,6,,,4,,
i,,,4,,,
j,,,,,,2
k,,2,,,3,2
l,,,7,,,
m,,6,,3,,
n,,,,,,7
\end{filecontents}
\begin{document}
\pgfplotstabletypeset[
%font=\tiny,
colorCell/.style={
% preproc/expr={lg10(##1)},%fails to work on ANY number
color cells={min=0.3,max=0.85}%max should be 783764
},
itemCell/.style={
string type,
column name={},
},
col sep=comma,
columns={item,Xa,Xb,Ya,Yb,Za,Zc},
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/Zc/.style={colorCell},
]{data.csv}
\end{document}