图例不适用于 TikZ/PGFplots

图例不适用于 TikZ/PGFplots

我在这里使用答案邮政,生成存储在data.dat文件中的矩阵的热图。存储在文件中的矩阵data.dat如下所示:

1.083   0.329   0       -0.139
0.329   -0.683  0.139   4.494e-12
0.329   -0.683  0.139   0
-0.373  -0.379  0.896   -0.215

这只是一个例子,因为我使用相同的脚本来绘制许多不同类型的矩阵。我使用以下 MWE,但图例不正确。它总是绘制值0, 5, 10, 15而不考虑data.dat文件。我该如何修复此问题,以便图例根据文件中的矩阵缩放data.dat。MWE 是:

平均能量损失

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.colormaps}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\begin{document}

\pgfplotstableread[header=false]{data.dat}{\datatable}   
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\xdef\LstX{}
\xdef\LstY{}
\xdef\LstC{}
\foreach \Y [evaluate=\Y as \PrevY using {int(\Y-1)},count=\nY] in {1,...,\numrows}
{\pgfmathtruncatemacro{\newY}{\numrows-\Y}
\foreach \X  [evaluate=\X as \PrevX using {int(\X-1)},count=\nX] in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\pgfmathtruncatemacro{\nZ}{\nX+\nY}
\ifnum\nZ=2
\xdef\LstX{\PrevX}
\xdef\LstY{\PrevY}
\xdef\LstC{\Current}
\else
\xdef\LstX{\LstX,\PrevX}
\xdef\LstY{\LstY,\PrevY}
\xdef\LstC{\LstC,\Current}
\fi
}
}
\edef\temp{\noexpand\pgfplotstableset{
 create on use/x/.style={create col/set list={\LstX}},
 create on use/y/.style={create col/set list={\LstY}},
 create on use/color/.style={create col/set list={\LstC}},}}
\temp
\pgfmathtruncatemacro{\strangenum}{\numrows*\numcols}
\pgfplotstablenew[columns={x,y,color}]{\strangenum}\strangetable

\begin{tikzpicture}

\begin{axis}[every tick label/.append style={font=\normalsize},
    xticklabel pos=right,
    xlabel near ticks,
    xmin=-0.5, xmax=\numcols-0.5,
    ylabel style={rotate=-90},
    ymin=-0.5, ymax=\numrows-0.5,
    point meta=explicit,
    colorbar as palette,
    colormap/viridis,colorbar sampled,
    scale mode=scale uniformly,
]
\draw (axis description cs:0,0) -- (axis description cs:1,0);
 \addplot [matrix plot,
        point meta=explicit,
] table [meta=color,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}
\end{document}

答案1

您需要去掉。将其添加到矩阵图中colorbar as palette也不会造成任何损害。colorbar source

\documentclass[tikz]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
1.083   0.329   0       -0.139
0.329   -0.683  0.139   4.494e-12
0.329   -0.683  0.139   0
-0.373  -0.379  0.896   -0.215
\end{filecontents*}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.colormaps}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\begin{document}

\pgfplotstableread[header=false]{data.dat}{\datatable}   
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\xdef\LstX{}
\xdef\LstY{}
\xdef\LstC{}
\foreach \Y [evaluate=\Y as \PrevY using {int(\Y-1)},count=\nY] in {1,...,\numrows}
{\pgfmathtruncatemacro{\newY}{\numrows-\Y}
\foreach \X  [evaluate=\X as \PrevX using {int(\X-1)},count=\nX] in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\pgfmathtruncatemacro{\nZ}{\nX+\nY}
\ifnum\nZ=2
\xdef\LstX{\PrevX}
\xdef\LstY{\PrevY}
\xdef\LstC{\Current}
\else
\xdef\LstX{\LstX,\PrevX}
\xdef\LstY{\LstY,\PrevY}
\xdef\LstC{\LstC,\Current}
\fi
}
}
\edef\temp{\noexpand\pgfplotstableset{
 create on use/x/.style={create col/set list={\LstX}},
 create on use/y/.style={create col/set list={\LstY}},
 create on use/color/.style={create col/set list={\LstC}},}}
\temp
\pgfmathtruncatemacro{\strangenum}{\numrows*\numcols}
\pgfplotstablenew[columns={x,y,color}]{\strangenum}\strangetable

\begin{tikzpicture}

\begin{axis}[every tick label/.append style={font=\normalsize},
    xticklabel pos=right,
    xticklabels={0,1,2,3,4},
    yticklabels={0,1,2,3,4},
    %
    xlabel near ticks,
    xmin=-0.5, xmax=\numcols-0.5,
    ylabel style={rotate=-90},
    ymin=-0.5, ymax=\numrows-0.5,
    point meta=explicit,
    colormap/viridis,colorbar sampled,
    scale mode=scale uniformly,
]
\draw (axis description cs:0,0) -- (axis description cs:1,0);
 \addplot [matrix plot,colorbar source,
        point meta=explicit,
] table [meta=color,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容