TransWikia.com

using tikz to plot a matrix as a heatmap

TeX - LaTeX Asked on July 5, 2021

How can I plot a matrix of density distributions like this ?

A=
    0.0333    0.0333    0.9000    0.9000
    0.9000    0.0333    0.0333    0.0333
    0.0333    0.9000    0.0333    0.0333
    0.0333    0.0333    0.0333    0.0333;

I would like to plot above matrix (for instance) with pgfplots or tikz as a bilinear interpolation heat map or an image in gray scale. How can I do it?

Update
From example on the web I made this:

documentclass[dvipsnames]{article}
usepackage{tikz}

usepackage{pgfplots, filecontents}
pgfplotsset{compat=1.13}
usepgfplotslibrary{colorbrewer}
usepackage{comment}
begin{filecontents*}{temp.dat}
0 0 0.0333            
1 0 0.0333             
2 0 0.9000            
3 0 0.9000            

0 1 0.9000
1 1 0.0333
2 1 0.0333
3 1 0.0333

0 2 0.0333
1 2 0.9000
2 2 0.0333
3 2 0.0333

0 3 0.0333
1 3 0.0333
2 3 0.0333
3 3 0.0333
end{filecontents*}
begin{document}
begin{tikzpicture}
  begin{tikzpicture}
        begin{axis}[
            view={0}{90},   % not needed for `matrix plot*' variant
            xlabel=context,
            ylabel=outcome,
            colormap = {whiteblack}{color(0cm)  = (white);color(1cm) = (black)},
            colorbar,
            colorbar style={
                title=$ $,
                yticklabel style={
                    /pgf/number format/.cd,
                    fixed,
                    precision=1,
                    fixed zerofill,
                },
            },
            title=$mathbf{A}_{mathrm{dom}}$,
            %
            % added these key-values
            enlargelimits=false,
            axis on top,
            point meta min=0,
            point meta max=1,
            %
            % uncomment me to show, that there are really no cells plotted
            % when using the second variant (`surf, shader=flat corner')
            %xmax=5,
            %ymax=5,
        ]
            %% gives the mean temperature of the neighbouring cells (4x4 matrix)
            %addplot3 [surf] file {temp.dat};

            %% gives the temperature of each point where the reference point
            %% is the lower left corner of the cell (also a 4x4 matrix)
            %% (I already asked the package author if this is intended or a bug)
            %addplot3 [surf,shader=flat corner] file {temp.dat};

            % this should give the desired output (5x5 matrix)
            addplot [matrix plot*,point meta=explicit] file [meta=index 2] {temp.dat};
        end{axis}
    end{tikzpicture}
end{tikzpicture}

end{document}

enter image description here

Problems with this figure

  1. It seems it got rotated. How can I fix it?

  2. Is there anyway to put xticklabels and yticklabels for the figure such as text instead of numbers?

  3. how can I remove the colorbar? I tried to comment out the line and I got errors.

  4. How could I use a similar structure of matrix as I posted earlier use to build this plot instead of breaking it down like examples?
    Thanks.

One Answer

  1. You can invert the direction of the y axis with y dir = reverse in the options of the axis environment.
  2. Manually you can control the tick labels with the xtick/ytick and xticklabels/yticklabels keys in the options of the axis environment. E.g.
    xtick = {0,1,2,3},
    ytick = {0,1,2,3},
    xticklabels = {
        $x_0$,
        $x_1$,
        $x_2$,
        $x_3$
    },
    yticklabels = {
        $y_0$,
        $y_1$,
        $y_2$,
        $y_3$
    },
    
  3. The color bar is controlled by the colorbar key. Set colorbar = false or just comment out colorbar.

Answered by luki on July 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP