3

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}
\pgfplotsset{compat=newest}

\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? lines from corners

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..

asante
  • 367
  • 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.) –  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
  • To question 1: This is a duplicate of e.g. https://tex.stackexchange.com/q/411110/95441 – Stefan Pinnow Aug 14 '18 at 17:25
  • To question 2: This is most likely a viewer issue. Please open the PDF with another viewer and confirm that this "artifacts" are also visible there. (I can confirm them with SumatraPDF, but with Acrobat Reader there aren't any.) – Stefan Pinnow Aug 14 '18 at 17:26
  • And to the initial question: Yes, if the questions are unrelated -- as in your case -- you should ask each of them separately (the next time). – Stefan Pinnow Aug 14 '18 at 17:28
  • Thanks @StefanPinnow, I also added the other answer. Regarding the viewers: it appears with Evince, the Firefox PDF viewer and mupdf, while Okular shows the PDF as Acrobat does. – asante Aug 15 '18 at 08:49
  • Ok, since you have confirmed that the remaining question is a PDF viewer issue, this is unrelated to LaTeX and therefore the question will be closed as unrelated (as are many other similar questions). – Stefan Pinnow Aug 15 '18 at 10:06
  • 4
    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
  • 1
    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: https://gist.github.com/GjjvdBurg/22de176179b46678b0b6206dc70a407d, it requires pdftk. – GjjvdBurg Nov 08 '19 at 15:43

1 Answers1

4

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*}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\begin{document}
\pgfplotstableread[header=false]{entries.dat}\datatable
\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}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\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);
 \addplot [
        matrix plot,
        %mesh/cols=4,
        point meta=explicit,
] table [meta=color,col sep=comma] \strangetable;
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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*}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\begin{document}
\pgfplotstableread[header=false]{entries.dat}\datatable
\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}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\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}

enter image description here

  • 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