编辑: 我删除了问题的重复部分,并在此处进行了回答:如何更改 pgfplots 颜色条的标签和刻度?和这里:pgfplots:在条件 3D 曲面绘图之前重塑数据矩阵(PGFplots 的功能请求https://sourceforge.net/p/pgfplots/feature-requests/76/)
我正在尝试使用 pgfplots 绘制矩阵,有三个问题,其中一个问题与以下 MWE 有关。我应该分别询问每个问题吗?
编辑:是的,我应该这么做。
\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}
关于这是否源于 PDF 查看器的问题:我使用 Evince、Firefox PDF 查看器、mupdf 进行了检查(这里出现了这些线条),而 Adobe Acrobat 和 Okular 显示的 PDF 却没有这些线条。
答案1
一个复杂的解决方案。我没能将坐标列表传递给绘图,所以我最终从你的矩阵中创建了一个新表。
\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}
让我说一下,为了做你想做的事情,你甚至不需要 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}