3

I plot the contour of an exemplary xyz matrix.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}

\pgfplotsset{compat=newest}

\usepgfplotslibrary{colormaps}

\pgfplotsset{/pgfplots/colormap={colmap}{
        rgb255=(0,0,102)
        rgb255=(0,0,255)
        rgb255=(0,255,255)
        rgb255=(255,255,255)
        rgb255=(255,255,0)
        rgb255=(255,0,0)
        rgb255=(102,0,0)
    }
}

\begin{filecontents}{test.txt}
1000    1000    1.29403
1001    1000    -0.95088
1002    1000    -0.30126
1003    1000    -0.40809
1004    1000    -2.75751
1005    1000    0.09494
1006    1000    0.3524
1007    1000    -0.22458
1008    1000    0.07528
1009    1000    -0.18389
1010    1000    -1.22863
1000    1005    0.37092
1001    1005    0.82494
1002    1005    -0.38305
1003    1005    0.55881
1004    1005    -0.86485
1005    1005    -1.22789
1006    1005    -2.14137
1007    1005    0.17896
1008    1005    -0.99818
1009    1005    -0.4235
1010    1005    1.06234
1000    1010    0.0401
1001    1010    1.29255
1002    1010    -0.9097
1003    1010    1.51249
1004    1010    -0.38255
1005    1010    0.99011
1006    1010    -0.192
1007    1010    0.01934
1008    1010    0.17867
1009    1010    0.69037
1010    1010    -0.8169
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            view={0}{90},
            colorbar,           
            ]
            
            \addplot3[
            surf,
            shader=interp,
            mesh/rows=3,
            mesh/cols=11,
            mesh/check=false,
            ]
            table[
            x index=0,
            y index=1,
            z index=2,
            ] {test.txt};           
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Now, my goal is a uniform colormap around zero. Therefore, one can define two individuel colormaps for the respective negative and positive values, but the easiest way that came in my mind is to find the maximum of the absolute values of all z values and use this as point meta min and point meta max for the colormap.

Since I am not (yet) that familiar with the coding in LaTeX, maybe someone can give me a helping hand to tackle my issue.

EDIT: I had a look at the pdfplotstable documentation and started to calculate the absolute values of the z values.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{filecontents}

\begin{filecontents}[overwrite]{test.txt}
1000    1000    1.29403
1001    1000    -0.95088
1002    1000    -0.30126
1003    1000    -0.40809
1004    1000    -2.75751
1005    1000    0.09494
1006    1000    0.3524
1007    1000    -0.22458
1008    1000    0.07528
1009    1000    -0.18389
1010    1000    -1.22863
1000    1005    0.37092
1001    1005    0.82494
1002    1005    -0.38305
1003    1005    0.55881
1004    1005    -0.86485
1005    1005    -1.22789
1006    1005    -2.14137
1007    1005    0.17896
1008    1005    -0.99818
1009    1005    -0.4235
1010    1005    1.06234
1000    1010    0.0401
1001    1010    1.29255
1002    1010    -0.9097
1003    1010    1.51249
1004    1010    -0.38255
1005    1010    0.99011
1006    1010    -0.192
1007    1010    0.01934
1008    1010    0.17867
1009    1010    0.69037
1010    1010    -0.8169
\end{filecontents}

\pgfplotstableread{test.txt}{\testdata}

\begin{document}

\pgfplotstableset{
    create on use/abs/.style={
        create col/expr={abs(\thisrowno{2})}},
}
\pgfplotstabletypeset[
columns={abs},
]
{\testdata}

\end{document} 

Now I need to calculate the maximum of this table and save the value. But here I got stucked again, because I have no clue how to "save" the resulting table for further calculations.

So my new question would be: How to make the resulting table accessible for additional calculations?

1 Answer 1

2

Well, i will give it a try. First i modified your input file by adding a header (for better understanding what's going on).

\begin{filecontents}[overwrite]{test.txt}
    x       y       z
    1000    1000    1.29403
...
\end{filecontents}

Then i dived into the documentation of pgfplotstable and also used the search function. So in the end: will this work for you?

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{filecontents}

\newcommand{\findMinMax}[4]{%
    % Input arguments:
    %   1: Filename
    %   2: Name of column to search
    % Output argument:
    %   3: Minimum value
    %   4: Maximum value
    \pgfplotstableread[col sep=space]{#1}\intable%
    %
    \pgfplotstablegetrowsof{\intable}
    \pgfmathsetmacro{\nRowsTab}{\pgfplotsretval-1}
    \pgfplotstablegetcolsof{\intable}
    \pgfmathsetmacro{\nCols}{\pgfplotsretval-1}
    %
    \pgfplotstablegetelem{0}{#2}\of{\intable}%
    \pgfmathsetmacro{\maxval}{\pgfplotsretval}%
    \pgfmathsetmacro{\minval}{\pgfplotsretval}%
    %
    \pgfplotsinvokeforeach {1,...,\nRowsTab}{%
        \pgfplotstablegetelem{##1}{#2}\of{\intable}%
        %
        \pgfmathsetmacro{\currVal}{\pgfplotsretval}%
        %
        \ifdim \currVal pt > \maxval pt%
        \pgfmathsetmacro{\maxval}{\pgfplotsretval}%
        \fi%
        %
        \ifdim \currVal pt < \minval pt%
        \pgfmathsetmacro{\minval}{\pgfplotsretval}%
        \fi%
    }%
    \let#3\minval%
    \let#4\maxval%
}

\begin{document}

\pgfmathsetmacro{\pMax}{0}
\pgfmathsetmacro{\pMin}{0}
\findMinMax{test.txt}{z}{\pMin}{\pMax}
\pgfmathsetmacro{\pLim}{abs(\pMin) > abs(\pMax) ? abs(\pMin) : abs(\pMax)} % 1st EDIT

\begin{tikzpicture}
\begin{axis}[
view={0}{90},
xlabel={\pMax},% only to show an output
ylabel={\pMin},
colorbar,
colorbar style={
    point meta max=\pLim,  % 1st EDIT
    point meta min=-\pLim,
},  
colormap name=viridis, % nope, i won't use Jet - nobody should
]

\addplot3[surf, shader=interp, mesh/rows=3, mesh/cols=11, mesh/check=false, 
point meta=\thisrow{z},
]
table[x=x, y=y, z=z] {test.txt};           
\end{axis}
\end{tikzpicture}
\end{document}
3
  • @ Venez: I tried your code and it worked out. The problem is, that this do not change the scaling of the colormap. Is it possible to calculate 'max(abs(Pmax,Pmin))' and use the positive and negative argument of this value as the new Pmax and Pmin?
    – Excelsior
    Commented Feb 20, 2021 at 15:03
  • 1
    Sure it is (see edited code). Simply define another macro where you use the ternary operator condition ? (true) : (false) and change the limits of the colorbar.
    – Venez
    Commented Feb 20, 2021 at 15:57
  • @ Venez: That is exactly what I was looking for. Thanks a lot for your help!
    – Excelsior
    Commented Feb 20, 2021 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .