TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 4 down vote accepted

The following does the job very well:

               title=View from top]
    \addplot3[surf] {x};
share|improve this answer

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.

  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]


share|improve this answer

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.

    \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]};
share|improve this answer
you know: tug.org/PSTricks/main.cgi?file=pst-plot/3D/contour ? – Herbert Jan 5 '11 at 13:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.