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.


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]



The following does the job very well:

               title=View from top]
    \addplot3[surf] {x};
  • The tick does not align to the middle of rectangle in this approach. – Dilawar Jun 14 '18 at 7:00

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]};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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