How can I plot a matrix of real values with TikZ/PGF such that it looks like plotted with imshow
in MATLAB?
\addplot3
does something similar, but I'd like to have that in a color-coded 2D plot.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityHow can I plot a matrix of real values with TikZ/PGF such that it looks like plotted with imshow
in MATLAB?
\addplot3
does something similar, but I'd like to have that in a color-coded 2D plot.
There's a problem with @Christian's first answer: It does not work for data matrices. In that case, the values get mapped at the corners of each squares, and the colors are some interpolation between adjacent data values, not the values themselves. I found the following solution. It's not completely satisfactory since the size of the marks does not adapt to the size of the picture, but it's a start. You can also have empty values, as shown in the example.
\begin{tikzpicture}
\begin{axis}[
width=0.75\textwidth, height=0.75\textwidth,
tick align=inside, unbounded coords=jump,
xmin=-1, xmax=5, ymin=-1, ymax=5,
point meta min=0, point meta max=8, colorbar, colormap/bluered]
\addplot[mark=square*,only marks, scatter, scatter src=explicit,
mark size=17]
coordinates {
(0,0) [0]
(0,1) [1]
(0,2) [2]
(0,3) [3]
(0,4) [4]
(1,0) [1]
(1,1) [2]
(1,2) [3]
(1,3) [4]
(1,4) [5]
(2,0) [2]
(2,1) [3]
(2,4) [6]
(3,0) [3]
(3,1) [4]
(3,2) [5]
(3,3) [6]
(3,4) [7]
(4,0) [4]
(4,1) [5]
(4,2) [6]
(4,3) [7]
(4,4) [8]
};
\end{axis}
\end{tikzpicture}
The following does the job very well:
\begin{tikzpicture}
\begin{axis}[view={0}{90},
xlabel=$x$,
ylabel=$y$,
title=View from top]
\addplot3[surf] {x};
\end{axis}
\end{tikzpicture}
I hacked a solution by my own, using a scatter plot. The key here is to adjust x and y, and the mark size to the right values.
\begin{tikzpicture}[]
\begin{axis}[x=4mm,y=4mm,xtick=\empty,ytick=\empty]
\addplot[scatter,scatter src=explicit,
only marks, mark=square*,mark size=2mm]
coordinates {(0,1)[0] (1,1)[.1] (2,1)[.2]
(0,2)[.3] (1,2)[.4] (2,2)[.5]};
\end{axis}
\end{tikzpicture}