我试图说明一个混淆矩阵,当我缩放每个节点时,它似乎弄乱了颜色条。请注意,颜色条没有完全填满。
以下是 MWE:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\pgfkeys{
/pgf/number format/fixed zerofill=true }
\pgfplotstableset{
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{tikzpicture}[every node/.style={scale=0.8}]
\begin{tikzpicture}[scale=0.2, every node/.style={scale=0.75}]
\begin{axis}[
width=5cm,
height=5cm,
colormap={bluewhite}{color=(white) rgb255=(100,149,237)},
xticklabels={benign,malware},
xtick={0,...,1},
xtick style={draw=none},
yticklabels={benign,malware},
ytick={0,...,1},
ytick style={draw=none},
enlargelimits=false,
colorbar,
colorbar style={
ytick={0,0.20,0.40,0.60,0.80,1.00},
yticklabels={0,0.20,0.40,0.60,0.80,1.00},
yticklabel={\pgfmathprintnumber\tick},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=1}
},
point meta min=0.0,point meta max=1.0,
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords style={
yshift=-7pt,
/pgf/number format/fixed,
/pgf/number format/precision=2},
]
\addplot[
matrix plot,
mesh/cols=2,
point meta=explicit,draw=gray
] table [meta=C] {
x y C
0 0 0.99
1 0 0.01
0 1 0.21
1 1 0.79
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
内部颜色填充的缩放比例color bar
可以通过plot graphics/node/.style
inside进行调整colorbar style
。用于scale=1.33
补偿节点缩放 0.75。
plot graphics/node/.style={scale=1.33,anchor=south west,inner sep=0pt,},
完整代码:
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\pgfkeys{/pgf/number format/fixed zerofill=true }
\begin{document}
\begin{tikzpicture}[scale=1, every node/.style={scale=0.75}]
\begin{axis}[
width=5cm,
height=5cm,
colormap={bluewhite}{color=(white) rgb255=(100,149,237)},
xticklabels={benign,malware},
xtick={0,...,1},
xtick style={draw=none},
yticklabels={benign,malware},
ytick={0,...,1},
ytick style={draw=none},
enlargelimits=false,
colorbar,
colorbar style={
plot graphics/node/.style={scale=1.33,anchor=south west,inner sep=0pt,},
ytick={0,0.20,0.40,0.60,0.80,1.00},
yticklabels={0,0.20,0.40,0.60,0.80,1.00},
yticklabel={\pgfmathprintnumber\tick},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=1}
},
point meta min=0.0,point meta max=1.0,
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords style={
yshift=-7pt,
/pgf/number format/fixed,
/pgf/number format/precision=2},
]
\addplot[
matrix plot,
mesh/cols=2,
point meta=explicit,draw=gray
] table [meta=C] {
x y C
0 0 0.99
1 0 0.01
0 1 0.21
1 1 0.79
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
对于测试前言,axis
建议如下:
\begin{axis}[
width =34mm, height=34mm, % <--- minimal picture size
colormap={bluewhite}{color=(white) rgb255=(100,149,237)},
xticklabels={benign,malware},
xtick={0,...,1},
xtick style={draw=none},
yticklabels={benign,malware},
ytick={0,...,1},
ytick style={draw=none},
tick label style={font=\tiny}, % <--- added font size "\tiny"
enlargelimits=false,
colorbar,
colorbar style={
ytick={0,0.20,0.40,0.60,0.80,1.00},
yticklabels={0,0.20,0.40,0.60,0.80,1.00},
yticklabel={\pgfmathprintnumber\tick},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=1}
},
point meta min=0.0,point meta max=1.0,
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords style={
yshift=-7pt, font=\tiny, % <--- added font size "\tiny"
/pgf/number format/fixed,
/pgf/number format/precision=2},
]
% ...
\end{axis}
较小的节点不允许水平写入 x 个标签。如果您希望它们更小,则需要通过添加xticklabel style = {rotate=90,anchor=east},
到axis
前言来旋转这些标签。在这种情况下,您可以将图片尺寸缩小到width =28mm, height=28mm,
。
对于更小的图片尺寸,您需要定义更小的字体大小(不推荐)。