9

I'm using matlab2tikz to export imagesc figures into TikZ/pgfplots code. This process gives me a TikZ file and a rasterized png version of the image with colours matching the colourbar at the time of execution.

The file produced by matlab2tikz is as follows:

\begin{tikzpicture}
  \begin{axis}[
    axis on top,
    xmin=75,
    xmax=575,
    xlabel={$x$},
    y dir=reverse,
    ymin=-0.045,
    ymax=0.955,
    ylabel={$y$},
    colorbar,
    point meta min=0,
    point meta max=80000000]

  \addplot graphics [xmin=75,xmax=575,ymin=-0.045,ymax=0.955] {p.png};
  \end{axis}
\end{tikzpicture}%

The problem is that because I'm doing this for multiple figures, I need to be able to switch the colourbar upper and lower bounds. Therefore, the rasterized png images do not match the modified colourbar.

The output I'd like to get is:

enter image description here

I was wondering whether it is possible to produce the exact same figure (ticks centered on the square etc.) with TikZ/pgfplots, while retaining the ability to change the colour of each square to match the colourbar --- ideally, the colour will be determined by the value loaded from a text file and the colourbar ranges.

Edit: Sample data can be found here. First two columns are the x and y axes and the fourth column (or third) will be what decides the colour of the square.

4
  • Alternatively, you could use the same scale for all plots when you create them in Matlab, by using imagesc(data, [min max]), where min and max would be the same for all your plots.
    – Jake
    Commented Jul 16, 2013 at 17:15
  • That would require me to do this twice --- once to identify the lowest and maximum bounds and once more to re-produce the imagesc figures with the new bounds. I was wondering if there's a better way to do this.
    – sudosensei
    Commented Jul 16, 2013 at 17:19
  • Could you edit your qestion to include some sample data?
    – Jake
    Commented Jul 16, 2013 at 17:46
  • Sure. I'll edit it now and add a link. Please let me know if it's the format you wanted it in. Done.
    – sudosensei
    Commented Jul 16, 2013 at 17:52

3 Answers 3

9

You could use a scatter plot with square markers for this. This requires a bit of work to scale the axis and markers so they join exactly, but this can be tucked away in a style. Then you can simply say

\begin{tikzpicture}
\begin{axis}[
        width=8cm, 
        matrix plot,
        colorbar
    ]
  \addplot table [meta=funceval] { ... };
\end{axis}    
\end{tikzpicture}

to get

Note that you have to specify the number of rows and columns using the keys matrix rows and matrix cols.

Here's the complete code:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\begin{document}

\pgfplotsset{
    matrix plot/.style={
        axis on top,
        clip marker paths=true,
        scale only axis,
        height=\matrixrows/\matrixcols*\pgfkeysvalueof{/pgfplots/width},
        enlarge x limits={rel=0.5/\matrixcols},
            enlarge y limits={rel=0.5/\matrixrows},
            scatter/use mapped color={draw=mapped color, fill=mapped color},
            scatter,
        point meta=explicit,
        mark=square*,
        cycle list={
            mark size=0.5*\pgfkeysvalueof{/pgfplots/width}/\matrixcols
        }
    },
    matrix rows/.store in=\matrixrows,
    matrix rows=10,
    matrix cols/.store in=\matrixcols,
    matrix cols=10
}
  \begin{tikzpicture}
    \begin{axis}[
            width=8cm, 
            matrix plot,
            colormap/hot,
            colorbar
        ]
      \addplot table [meta=funceval] {
ppm popsize timessolved funceval
0.005   100 30  5.2312e+06
0.105   100 30  1.6282e+06
0.205   100 29  2.0174e+06
0.305   100 30  1.66e+06
0.405   100 28  2.639e+06
0.505   100 24  4.404e+06
0.605   100 16  7.9352e+06
0.705   100 8   1.1446e+07
0.805   100 6   1.2217e+07
0.905   100 9   1.0817e+07

0.005   150 30  8.688e+06
0.105   150 30  2.1773e+06
0.205   150 30  2.2315e+06
0.305   150 30  2.401e+06
0.405   150 30  2.6153e+06
0.505   150 26  5.426e+06
0.605   150 21  8.7765e+06
0.705   150 23  7.0593e+06
0.805   150 16  1.1344e+07
0.905   150 14  1.2771e+07

0.005   200 30  6.6252e+06
0.105   200 30  3.0197e+06
0.205   200 30  2.9699e+06
0.305   200 30  3.1497e+06
0.405   200 30  3.5268e+06
0.505   200 30  3.7531e+06
0.605   200 27  6.1353e+06
0.705   200 24  8.3013e+06
0.805   200 21  1.0925e+07
0.905   200 17  1.4032e+07

0.005   250 30  8.1181e+06
0.105   250 30  3.5341e+06
0.205   250 30  3.8653e+06
0.305   250 30  3.7785e+06
0.405   250 30  4.3767e+06
0.505   250 30  4.3917e+06
0.605   250 29  5.3275e+06
0.705   250 29  4.6693e+06
0.805   250 23  1.0961e+07
0.905   250 25  8.02e+06

0.005   300 30  8.5783e+06
0.105   300 30  4.0582e+06
0.205   300 30  4.263e+06
0.305   300 30  4.692e+06
0.405   300 30  5.1478e+06
0.505   300 30  5.4769e+06
0.605   300 29  6.3917e+06
0.705   300 26  9.4055e+06
0.805   300 26  8.7111e+06
0.905   300 18  1.9458e+07

0.005   350 30  1.0232e+07
0.105   350 30  4.5218e+06
0.205   350 30  4.9123e+06
0.305   350 30  5.3983e+06
0.405   350 30  5.9751e+06
0.505   350 30  6.3135e+06
0.605   350 30  5.7111e+06
0.705   350 30  4.6045e+06
0.805   350 27  8.4856e+06
0.905   350 24  1.2849e+07

0.005   400 30  8.3002e+06
0.105   400 30  5.2684e+06
0.205   400 30  5.5534e+06
0.305   400 30  6.1292e+06
0.405   400 30  6.6702e+06
0.505   400 30  7.2192e+06
0.605   400 30  6.9306e+06
0.705   400 30  5.5206e+06
0.805   400 30  4.0857e+06
0.905   400 29  5.1603e+06

0.005   450 30  1.0286e+07
0.105   450 30  5.8948e+06
0.205   450 30  5.9761e+06
0.305   450 30  6.5749e+06
0.405   450 30  7.6008e+06
0.505   450 30  8.2071e+06
0.605   450 30  7.7313e+06
0.705   450 30  6.0063e+06
0.805   450 29  6.7191e+06
0.905   450 28  7.9783e+06

0.005   500 30  9.0253e+06
0.105   500 30  6.2937e+06
0.205   500 30  6.7477e+06
0.305   500 30  7.3703e+06
0.405   500 30  8.5554e+06
0.505   500 30  8.8726e+06
0.605   500 30  8.4899e+06
0.705   500 30  6.775e+06
0.805   500 30  5.2942e+06
0.905   500 29  6.8066e+06

0.005   550 30  1.1179e+07
0.105   550 30  7.0593e+06
0.205   550 30  7.3797e+06
0.305   550 30  7.7037e+06
0.405   550 30  8.7875e+06
0.505   550 30  9.9175e+06
0.605   550 30  1.0188e+07
0.705   550 30  7.4751e+06
0.805   550 30  5.8577e+06
0.905   550 30  4.4054e+06      
      };
    \end{axis}    
  \end{tikzpicture}

\end{document}
1
  • That is phenomenal, Jake. Exactly what I needed. Thank you!
    – sudosensei
    Commented Jul 16, 2013 at 19:12
8

With the new features matrix plot and matrix plot* available in PGFPlots v1.13 Jake's answer can be simplified to the following code. You can find the description in section 4.6.12 on page 164 of the manual.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=8cm,
            enlargelimits=false,
            axis on top,    % <-- (use with `matrix plot*')
            colormap/viridis,
            colorbar,
            point meta=explicit,
            % change me to adjust ``end point'' values used for the colormap
            point meta min=0e7,
            point meta max=2e7,
            % change min and max values to just show that interval in the colorbar
%            colorbar style={
%                ymin=0.5e7,
%                ymax=2.5e7,
%            },
        ]
%            \addplot [matrix plot] table [meta=funceval] {
            \addplot [matrix plot*] table [meta=funceval] {
                ppm popsize timessolved funceval
                0.005   100 30  5.2312e+06
                0.105   100 30  1.6282e+06
                0.205   100 29  2.0174e+06
                0.305   100 30  1.66e+06
                0.405   100 28  2.639e+06
                0.505   100 24  4.404e+06
                0.605   100 16  7.9352e+06
                0.705   100 8   1.1446e+07
                0.805   100 6   1.2217e+07
                0.905   100 9   1.0817e+07

                0.005   150 30  8.688e+06
                0.105   150 30  2.1773e+06
                0.205   150 30  2.2315e+06
                0.305   150 30  2.401e+06
                0.405   150 30  2.6153e+06
                0.505   150 26  5.426e+06
                0.605   150 21  8.7765e+06
                0.705   150 23  7.0593e+06
                0.805   150 16  1.1344e+07
                0.905   150 14  1.2771e+07

                0.005   200 30  6.6252e+06
                0.105   200 30  3.0197e+06
                0.205   200 30  2.9699e+06
                0.305   200 30  3.1497e+06
                0.405   200 30  3.5268e+06
                0.505   200 30  3.7531e+06
                0.605   200 27  6.1353e+06
                0.705   200 24  8.3013e+06
                0.805   200 21  1.0925e+07
                0.905   200 17  1.4032e+07

                0.005   250 30  8.1181e+06
                0.105   250 30  3.5341e+06
                0.205   250 30  3.8653e+06
                0.305   250 30  3.7785e+06
                0.405   250 30  4.3767e+06
                0.505   250 30  4.3917e+06
                0.605   250 29  5.3275e+06
                0.705   250 29  4.6693e+06
                0.805   250 23  1.0961e+07
                0.905   250 25  8.02e+06

                0.005   300 30  8.5783e+06
                0.105   300 30  4.0582e+06
                0.205   300 30  4.263e+06
                0.305   300 30  4.692e+06
                0.405   300 30  5.1478e+06
                0.505   300 30  5.4769e+06
                0.605   300 29  6.3917e+06
                0.705   300 26  9.4055e+06
                0.805   300 26  8.7111e+06
                0.905   300 18  1.9458e+07

                0.005   350 30  1.0232e+07
                0.105   350 30  4.5218e+06
                0.205   350 30  4.9123e+06
                0.305   350 30  5.3983e+06
                0.405   350 30  5.9751e+06
                0.505   350 30  6.3135e+06
                0.605   350 30  5.7111e+06
                0.705   350 30  4.6045e+06
                0.805   350 27  8.4856e+06
                0.905   350 24  1.2849e+07

                0.005   400 30  8.3002e+06
                0.105   400 30  5.2684e+06
                0.205   400 30  5.5534e+06
                0.305   400 30  6.1292e+06
                0.405   400 30  6.6702e+06
                0.505   400 30  7.2192e+06
                0.605   400 30  6.9306e+06
                0.705   400 30  5.5206e+06
                0.805   400 30  4.0857e+06
                0.905   400 29  5.1603e+06

                0.005   450 30  1.0286e+07
                0.105   450 30  5.8948e+06
                0.205   450 30  5.9761e+06
                0.305   450 30  6.5749e+06
                0.405   450 30  7.6008e+06
                0.505   450 30  8.2071e+06
                0.605   450 30  7.7313e+06
                0.705   450 30  6.0063e+06
                0.805   450 29  6.7191e+06
                0.905   450 28  7.9783e+06

                0.005   500 30  9.0253e+06
                0.105   500 30  6.2937e+06
                0.205   500 30  6.7477e+06
                0.305   500 30  7.3703e+06
                0.405   500 30  8.5554e+06
                0.505   500 30  8.8726e+06
                0.605   500 30  8.4899e+06
                0.705   500 30  6.775e+06
                0.805   500 30  5.2942e+06
                0.905   500 29  6.8066e+06

                0.005   550 30  1.1179e+07
                0.105   550 30  7.0593e+06
                0.205   550 30  7.3797e+06
                0.305   550 30  7.7037e+06
                0.405   550 30  8.7875e+06
                0.505   550 30  9.9175e+06
                0.605   550 30  1.0188e+07
                0.705   550 30  7.4751e+06
                0.805   550 30  5.8577e+06
                0.905   550 30  4.4054e+06
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1

Base on Jake's answer here is a MATLAB script that returns a TikZ figure:

Input arguments:

  • MATRIX: a [m x n] matrix (indices used as positions) or a [m x 3] [x y data] (if 4e arg. is list).
  • FILENAME: output file.
  • WIDTH: width of the figure.
  • LIST: if equal to list treats the matrix as a [m x 3] matrix.

function matrix2TikZ(matrix, filename, width, list)
%MATRIX2TIKZ    Convert matrix to TikZ/Pgfplots figure.
%
%   MATRIX2TIKZ(MATRIX, FILENAME,...)
%   convert the 2d MATRIX to LaTeX figure saved in FILENAME.
%   MATRIX is a [n x m] or [m x 3] matrix.
%
%   MATRIX2TIKZ(MATRIX, FILENAME, WIDTH) explicitely specifies the width of the
%   figure. (default: 8cm)
%
%   MATRIX2TIKZ(MATRIX, FILENAME, WIDTH, list) if list == 'list' the MATRIX is
%   already a [m x 3] matrix and is directly written to file.

%Input checks
if ndims(matrix) ~= 2
    error('I can only convert a 2D matrix.');
end

if nargin < 2
    error('You must provide at least two arguments: a MATRIX and a FILENAME.');
elseif nargin < 3
    width = '8cm';
    list = '';  
end

%Generate output matrix [x y data]
if strcmp(list, 'list')
    out = matrix;
else
    out = zeros(size(matrix,1)*size(matrix,2),3);
    for x=1:size(matrix, 2)
        for y=1:size(matrix, 1)
            %Y axis is inverted
            out((y-1)*size(matrix, 2)+x,:) = [x size(matrix,1)+1-y matrix(y,x)];
        end
    end
end

%Write to file
fileID = fopen(filename,'w');
fprintf(fileID,[ ...
    '\\pgfplotsset{\n' ...
    '    matrix plot/.style={\n' ...
    '        axis on top,\n' ...
    '        clip marker paths=true,\n' ...
    '        scale only axis,\n' ...
    '        height=\\matrixrows/\\matrixcols*\\pgfkeysvalueof{/pgfplots/width},\n' ...
    '        enlarge x limits={rel=0.5/\\matrixcols},\n' ...
    '        enlarge y limits={rel=0.5/\\matrixrows},\n' ...
    '        scatter/use mapped color={draw=mapped color, fill=mapped color},\n' ...
    '        scatter,\n' ...
    '        point meta=explicit,\n' ...
    '        mark=square*,\n' ...
    '        cycle list={\n' ...
    '            mark size=0.5*\\pgfkeysvalueof{/pgfplots/width}/\\matrixcols\n' ...
    '        }\n' ...
    '    },\n' ...
    '    matrix rows/.store in=\\matrixrows,\n' ...
]);

fprintf(fileID, [ ...
    '    matrix rows=%i,\n' ...
], size(matrix,1));

fprintf(fileID, [ ...
    '    matrix cols/.store in=\\matrixcols,\n' ...
]);

fprintf(fileID, [ ...
    '    matrix cols=%i,\n' ...
], size(matrix,2));

fprintf(fileID, [ ...
    '}\n' ...
    '\\begin{tikzpicture}\n' ...
    '    \\begin{axis}[\n' ...
]);

fprintf(fileID, [ ...
    '        width=%s,\n' ...
], width);

fprintf(fileID, [ ...
    '        matrix plot,\n' ...
    '        colormap/hot,\n' ...
    '        colorbar\n' ...
    '    ]\n' ...
    '        \\addplot table [meta=peaks]{\n' ...
    '            time channel peaks\n' ...
]);

fprintf(fileID, [ ...
    '            %f %f %f\n' ...
], out');

fprintf(fileID, [ ...
    '        };\n' ...
    '    \\end{axis}\n' ...
    '\\end{tikzpicture}' ...
]);

fclose(fileID);

The code is also hosted on github.

You must log in to answer this question.

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