我想绘制一个热图样式的图表,其中只有 2d 矩阵的对角线有值。我期望图表只包含表示对角线元素上的值 > 0 的颜色。然而,我得到的却是这样的:
以下是 MWE:
\documentclass{article}
\usepackage{pgfplots, filecontents}
\pgfplotsset{compat=newest}
\begin{filecontents}{heatmap.dat}
0.000000000000000000e+00 0.000000000000000000e+00 8.912886550287250076e-01
1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00 9.175166561053226255e-01
2.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 2.000000000000000000e+00 8.031786095794766211e-01
3.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 3.000000000000000000e+00 9.125650835351171164e-01
4.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 4.000000000000000000e+00 9.731804164301872673e-01
5.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 5.000000000000000000e+00 8.030800439825510617e-01
6.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 6.000000000000000000e+00 9.381443321189661955e-01
7.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 7.000000000000000000e+00 9.573177461885088446e-01
8.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 8.000000000000000000e+00 9.696479795497311427e-01
9.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 9.000000000000000000e+00 9.189678833446063289e-01
\end{filecontents}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[view={0}{90},
colorbar,
y dir=reverse,
xlabel=$x$,
ylabel=$y$,]
\addplot3[surf, shader=faceted] table {heatmap.dat};
\end{axis}
\end{tikzpicture}
\caption{Heatmap with PGFPLOTS}
\end{figure}
\end{document}
在这种情况下我该如何关闭颜色插值?
答案1
您很可能正在寻找shader=flat corner
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{filecontents}[overwrite]{heatmap.dat}
0.000000000000000000e+00 0.000000000000000000e+00 8.912886550287250076e-01
1.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 0.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 1.000000000000000000e+00 9.175166561053226255e-01
2.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 1.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 2.000000000000000000e+00 8.031786095794766211e-01
3.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 2.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 3.000000000000000000e+00 9.125650835351171164e-01
4.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 3.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 4.000000000000000000e+00 9.731804164301872673e-01
5.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 4.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 5.000000000000000000e+00 8.030800439825510617e-01
6.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 5.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 6.000000000000000000e+00 9.381443321189661955e-01
7.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 6.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 7.000000000000000000e+00 9.573177461885088446e-01
8.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 7.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 8.000000000000000000e+00 9.696479795497311427e-01
9.000000000000000000e+00 8.000000000000000000e+00 0.000000000000000000e+00
0.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
1.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
2.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
3.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
4.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
5.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
6.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
7.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
8.000000000000000000e+00 9.000000000000000000e+00 0.000000000000000000e+00
9.000000000000000000e+00 9.000000000000000000e+00 9.189678833446063289e-01
\end{filecontents}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[view={0}{90},
colorbar,
y dir=reverse,
xlabel=$x$,
ylabel=$y$,]
\addplot3[surf, shader=flat corner] table {heatmap.dat};
\end{axis}
\end{tikzpicture}
\caption{Heatmap with PGFPLOTS}
\end{figure}
\end{document}