# pgfplots matrix plot “perfect” rectangles [closed]

Edit: I removed the duplicate part of the question, which was answered here: How to change label and ticks of a pgfplots colorbar? and here: pgfplots: Reshape a MATRIX of data before conditional 3D surface plotting (Feature Request for PGFplots https://sourceforge.net/p/pgfplots/feature-requests/76/)

I'm trying to plot a matrix with pgfplots and have three questions one question regarding the following MWE. Should I ask every question separately? Edit: Yes I should have.

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.colormaps}

\usepackage{filecontents}
\begin{filecontents}{\jobname.dat}
x y C
0 0 32
0 1 0
0 2 0
0 3 0
0 4 0
1 0 0
1 1 16
1 2 0
1 3 0
1 4 0
2 0 0
2 1 0
2 2 16
2 3 0
2 4 0
3 0 0
3 1 0
3 2 0
3 3 16
3 4 0
4 0 0
4 1 0
4 2 0
4 3 0
4 4 16
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{%
colormap={WhiteRedBlack}{%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
},
}
\begin{axis}[%
small,
every tick label/.append style={font=\tiny},
tick align=outside,
minor tick num=5,
%
xlabel=$\beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=5,
xtick={0, 5, ..., 4},
%
ylabel=$\alpha$,
ylabel style={rotate=-90},
ymin=-1, ymax=5,
ytick={0, 5, ..., 4},
%
point meta min=0,
point meta max=32,
point meta=explicit,
%
colorbar sampled line,
colormap name=WhiteRedBlack,
scale mode=scale uniformly,
]
\draw (axis description cs:0,0) -- (axis description cs:1,0);
\addplot[matrix plot, mesh/cols=5] table[meta=C] {\jobname.dat};
\end{axis}
\end{tikzpicture}
\end{document}

1. When one zooms into the drawn rectangles, one can see a short line dropping from the left-lower and right-upper corner of the rectangle, so not a "perfect" rectangle is drawn (see screenshot). Can this be fixed?

Regarding the question if this stems from a problem with the PDF viewer: I checked it with Evince, the Firefox PDF viewer, mupdf (here the lines appear) whereas Adobe Acrobat and Okular showed the PDF without these lines..

• What is pgfplots doing with this data? If you indent to draw rectangles, \addplot may not be the best approach. You can use normal tikz commands with axis cs: coordinates. – John Kormylo Aug 14 '18 at 13:16
• pgfplots comes with the possibility of doing patch plots, where you specify the coordinates of the vertices once and then indicate how they are to be connected to faces. The buzzword is patch table. An impressive example is on the bottom of p. 179 of the pgfplots manual. This may or may not automatically answer the question parts 2. and 3. (I think here it is perfectly OK to ask a question with 3 parts. I just may be that if one answers 1., the other parts may be redundant or need to be reformulated.) – user121799 Aug 14 '18 at 13:24
• I edited the question, I guess I have to wait for the duplication mark to get removed by someone else? – asante Aug 14 '18 at 17:19
• because it is most likely a PDF viewer issue and thus unrelated to LaTeX (and friends) – Christian Feuersänger Aug 26 '18 at 15:52
• The artifacts in your plot are created because the matrix plot output creates a "stroke and fill" path in the pdf, instead of a simple "fill path". I'm not sure if this is a bug in pgfplots, but it can be fixed by editing the raw pdf file and doing a find and replace on the rectangles. Here's a quick python script that does this: gist.github.com/GjjvdBurg/22de176179b46678b0b6206dc70a407d, it requires pdftk. – GjjvdBurg Nov 8 at 15:43

A complicated solution. I did not manage to pass a list of coordinates to the plot, so I ended up creating a new table from your matrix.

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

\begin{filecontents*}{entries.dat}
32 0 0 0
0 16 0 0
0 0 16 0
0 0 0 16
\end{filecontents*}
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
\begin{document}
\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}
{
\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

%\pgfplotstabletypeset[empty cells with={---}]\strangetable
\begin{tikzpicture}
\pgfplotsset{%
colormap={WhiteRedBlack}{%
rgb255=(255,255,255)
rgb255=(255,0,0)
rgb255=(0,0,0)
},
}
\begin{axis}[%
small,
every tick label/.append style={font=\tiny},
tick align=outside,
minor tick num=5,
%
xlabel=$\beta$,
xticklabel pos=right,
xlabel near ticks,
xmin=-1, xmax=5,
xtick={0, 5, ..., 4},
%
ylabel=$\alpha$,
ylabel style={rotate=-90},
ymin=-1, ymax=5,
ytick={0, 5, ..., 4},
%
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,
]
\draw (axis description cs:0,0) -- (axis description cs:1,0);
matrix plot,
%mesh/cols=4,
point meta=explicit,
] table [meta=color,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}
\end{document}


Let me mention that, in order to do something like you want you do not even need pgfplots.

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepackage{filecontents}
\begin{filecontents*}{entries.dat}
32 0 0 0
0 16 0 0
0 0 16 0
0 0 0 16
\end{filecontents*}
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
\begin{document}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\begin{tikzpicture}
\foreach \Y [evaluate=\Y as \PrevY using {int(\numrows-\Y)},
evaluate=\Y as \NewY using {int(\numrows-\Y+1)}] in {1,...,\numrows}
{
\foreach \X  [evaluate=\X as \PrevX using {int(\X-1)}] in {1,...,\numcols}
{
\ifnum\Current=0
\else
\pgfmathsetmacro{\myc}{148-4*\Current}
\fill[red!\myc!black] (\PrevX,\numrows-\PrevY-1) rectangle ++ (1,1);
\fi
}
}
\draw (0,0) grid (\numrows,\numcols);
\end{tikzpicture}
\end{document}


• For this I need the vertices.dat and the faces.dat files, right? I have to say that I prefer the matrix plot style, which seems to require much less boilerplate info. – asante Aug 14 '18 at 17:02
• @asante, I would say so, too. matrix plot is perfectly fine for your needs. Maybe even better would be matrix plot*. – Stefan Pinnow Aug 14 '18 at 17:32