3

I'm trying to illustrate a confusion matrix, and when I scale every node and it seems to mess up the colorbar. Notice that the colorbar is not completely filled in.

Colorbar problem

And here's an 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}
  • 2
    Welcome! What is the reason you want to scale all the nodes? The text size? Can't you just add font=\small instead? – user121799 Dec 12 '18 at 4:12
  • I have a confusion matrix that is 26x26, so I would like to enlarge the matrix (so the cells are large enough to hold numbers that are readable), then scale down the labels on the axes and numbers in the cells. – NowhereMan Dec 12 '18 at 11:53
2

The scaling of the color filling inside color bar can be adjusted with plot graphics/node/.style inside colorbar style. Used scale=1.33 to compensate for the node scaling by 0.75.

    plot graphics/node/.style={scale=1.33,anchor=south west,inner sep=0pt,},

enter image description here

The complete code:

\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}
  • Thanks! That's exactly what I was looking for but could not find. – NowhereMan Dec 13 '18 at 0:25
0

for preamble of axis test the following suggestion:

\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}

enter image description here

smaller nodes not enables to write x labels horizontally. if you like to have even smaller them, than you need to rotate these labels by adding xticklabel style = {rotate=90,anchor=east}, to axis preamble. in this case you can reduce picture size to width =28mm, height=28mm,.

enter image description here

for even smaller picture size you need define smaller font size (not recommended).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.