整数填零,小数不填零

整数填零,小数不填零

我有一个如下所示的相关矩阵图:

矩阵 1

问题是我想让“1”显示为“1.0”,并且小数将不会有任何尾随零,精度设置为 3。我认为解决方案是使用 \pgfmathifisint。但我不知道如何将其纳入其中。

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}

\usepackage{filecontents}
\usepackage{amsmath,amssymb,physics}

\begin{document}
    \begin{tikzpicture}
        \begin{groupplot}[group style={group size=2 by 1}]
        \nextgroupplot[
            title=A correlation matrix,
            xlabel=input parameter $\vb{x}$,
            xtick={1,2,3,4,5},
            xticklabels={1,2,3,4,5},
            ylabel=input parameter $\vb{y}$,
            ytick={5,4,3,2,1},
            yticklabels={1,2,3,4,5},
            point meta min=-1,
            point meta max=1,
            colormap/temp,
            enlargelimits=false,
            axis on top,
        ]
            \addplot[
            matrix plot*,
            nodes near coords,
            nodes near coords align={center},
            nodes near coords style={
                font=\small,
                /pgf/number format/.cd,
        fixed,
        precision=3
            },
            point meta=explicit,
            ] file {cor_i.txt};;
        \end{groupplot}
    \end{tikzpicture}
\end{document}

cor_i.txt中使用的数据集:

1 1 -0.206
2 1 -0.044
3 1 -0.045
4 1 -0.866
5 1 1.0

1 2 0.227
2 2 0.053
3 2 -0.105
4 2 1.0
5 2 -0.866

1 3 -0.021
2 3 0.012
3 3 1.0
4 3 -0.105
5 3 -0.045

1 4 -0.655
2 4 1.0
3 4 0.012
4 4 0.053
5 4 -0.44

1 5 1.0
2 5 -0.655
3 5 -0.214
4 5 0.23
5 5 -0.206

我已经尽力了,但还没有找到解决方案。还是根本就不可能找到?

答案1

如果您同意所有条目都有 3 位小数,fixed zerofill那么您可以使用precision=3

在此处输入图片描述

        \addplot[
        matrix plot*,
        nodes near coords,
        nodes near coords align={center},
        nodes near coords style={
            font=\small,
            /pgf/number format/.cd,
    fixed,
    precision=3,
    fixed zerofill
        },
        point meta=explicit,
        ] file {cor_i.txt};

答案2

我终于找到了解决问题的方法,其中包括nodes near coords修改

nodes near coords={\pgfmathifisint{\pgfplotspointmeta}
    {\pgfmathprintnumber[fixed,precision=1,zerofill]{\pgfretval}}
    {\pgfmathprintnumber[fixed, precision=3]{\pgfretval}}%
}

所以我最终得到的输出是:

输出

完整代码如下:

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}

\usepackage{filecontents}
\usepackage{amsmath,amssymb,physics}

\begin{document}
    \begin{tikzpicture}
        \begin{groupplot}[group style={group size=2 by 1}]
        \nextgroupplot[
            title=A correlation matrix,
            xlabel=input parameter $\vb{x}$,
            xtick={1,2,3,4,5},
            xticklabels={1,2,3,4,5},
            ylabel=input parameter $\vb{y}$,
            ytick={1,2,3,4,5},
            yticklabels={1,2,3,4,5},
            point meta min=-1,
            point meta max=1,
            colormap/temp,
            enlargelimits=false,
            axis on top,
        ]
            \addplot[
            matrix plot*,
            nodes near coords={\pgfmathifisint{\pgfplotspointmeta}
          {\pgfmathprintnumber[fixed,precision=1,zerofill]{\pgfretval}}
          {\pgfmathprintnumber[fixed, precision=3]{\pgfretval}}%
},
            nodes near coords align={center},
            nodes near coords style={
                font=\small,
            },
            point meta=explicit,
            ] file {cor_i.txt};;
        \end{groupplot}
    \end{tikzpicture}
\end{document}

相关内容