根据输入数据绘制矩阵

根据输入数据绘制矩阵

我有一个矩阵 M,从 Matlab 导出到 .dat 文件。我想将其导入 tikz 并分别绘制 M 实部和虚部的密度图。x 轴为列索引,y 轴为行,z 为存储在元素 [M]_{i,j} 中的值。

这个问题被问到并得到了答复这个帖子。但是,该解决方案需要对数据进行特定的操作,而且考虑到它是近 4 年前提出的,我想知道是否还有其他方法。

数据矩阵如下所示:

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

任何帮助都将受到赞赏。

答案1

这是一些读取表格并允许您生成实部和虚部矩阵图的代码。实部和虚部的提取使用xstring。对于实部,我们只需i用替换*0,然后解析结果。对于虚部,我们用替换i*1减去实部。似乎工作正常。

\documentclass[border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.colormaps}
\pgfplotsset{compat=1.16}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xstring}

\begin{filecontents*}{complexentries.dat}
1.083+0.329i    0.329+-0.373i   0-0.139i            -0.139-0.896i
0.329-0.683i    -0.683-0.379i   0.139+0i            4.494e-12+0.215i
0.329-0.683i    -0.683-0.379i   0.139-4.494e-12i    0+0.215i
-0.373-0.379i   -0.379+0.282i   0.896-0.215i        -0.215+0i
\end{filecontents*}
\def\pgfmathsetmacroFPU#1#2{\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}%
\newcommand\ExtractRealAndImaginary[1]{%
\StrSubstitute{#1}{i}{*0}[\mytemp]%
\pgfmathsetmacroFPU{\myreal}{\mytemp}%
\StrSubstitute{#1}{i}{*1}[\mytemp]%
\pgfmathsetmacroFPU{\myim}{\mytemp-\myreal}}


\def\ipExtractRealAndImaginary#1+#2i{\def\myreal{#1}\def\myim{#2}}
\def\imExtractRealAndImaginary#1-#2i{\def\myreal{#1}\def\myim{-#2}}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\begin{document}
\pgfplotstableread[header=false]{complexentries.dat}\datatable
%\pgfplotstabletypeset\datatable

%\end{document}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\foreach \nY in {1,...,\numrows}
{\pgfmathtruncatemacro{\newY}{\numrows-\nY}
\foreach \nX in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\the\numexpr\nY-1}{\the\numexpr\nX-1}{\Current}%
%\typeout{===========}
\edef\temp{\noexpand\ExtractRealAndImaginary{\Current}}
\temp
%\typeout{Re(\Current)=\myreal,Im(\Current)=\myim}
%\edef\myentry{\myim}%
\pgfmathtruncatemacro{\nZ}{\nX+\nY}%
\ifnum\nZ=2
\xdef\LstX{\the\numexpr\nX-1}%
\xdef\LstY{\the\numexpr\nY-1}%
\xdef\LstRe{\myreal}%
\xdef\LstIm{\myim}%
\else
\xdef\LstX{\LstX,\the\numexpr\nX-1}%
\xdef\LstY{\LstY,\the\numexpr\nY-1}%
\xdef\LstRe{\LstRe,\myreal}%
\xdef\LstIm{\LstIm,\myim}%
\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/real/.style={create col/set list={\LstRe}},
 create on use/im/.style={create col/set list={\LstIm}},
}}
\temp
\pgfmathtruncatemacro{\strangenum}{\numrows*\numcols}
\pgfplotstablenew[columns={x,y,real,im}]{\strangenum}\strangetable

%\pgfplotstabletypeset\strangetable

\begin{tikzpicture}
\begin{axis}[%
    small,title=real parts,
    tick align=outside,
    minor tick num=4,
    %
    xlabel=$x$,
    xlabel near ticks,
    xmin=-1, xmax=4,
    xtick=\empty,
    %
    ylabel=$y$,
    ylabel style={rotate=-90},
    ymin=-1, ymax=4,     
    ytick=\empty,
%     point meta min=0,
%     point meta max=32,
    point meta=explicit,
    %
    %colorbar sampled,
    colorbar as palette,
    colorbar style={samples=3},
    %colormap name=WhiteRedBlack,
    scale mode=scale uniformly,
]
 \addplot [
        matrix plot,
        mesh/cols=4,
        point meta=explicit,
] table [meta=real,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[%
    small,title=imaginary parts,
    tick align=outside,
    minor tick num=4,
    %
    xlabel=$x$,
    xlabel near ticks,
    xmin=-1, xmax=4,
    xtick=\empty,
    %
    ylabel=$y$,
    ylabel style={rotate=-90},
    ymin=-1, ymax=4,     
    ytick=\empty,
%     point meta min=0,
%     point meta max=32,
    point meta=explicit,
    %
    %colorbar sampled,
    colorbar as palette,
    colorbar style={samples=3},
    %colormap name=WhiteRedBlack,
    scale mode=scale uniformly,
]
 \addplot [
        matrix plot,
        mesh/cols=4,
        point meta=explicit,
] table [meta=im,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容