Mathematica Stack Exchange is a question and answer site for users of Mathematica. 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

I'm trying to locate minima / maxima in matrices on a heat map. So I have my 2D matrix and did a MatrixPlot,

my matrix plot

and I can find the position of the smallest value.

My question: how do I place a graphical marker at that position? Like a box to outline field (7,6)?

Completely stumped.

share|improve this question
Use the Epilog option. – Szabolcs Jan 30 '13 at 18:04
Thanks, Szabolcs! Works just as expected! – blinkenstack Jan 30 '13 at 18:40
Overlay could work too, but may be more difficult to position. Another possibility is to use Show – Sjoerd C. de Vries Jan 30 '13 at 18:43
If you want an unfilled Rectangle, use {EdgeForm[Black], FaceForm[None], Rectangle[...]}. – Szabolcs Jan 30 '13 at 18:44
Okay, 'Overlay' did work as well, though with more overhead. The unfilled box Szabolcs suggested is actually what I was fiddling with just now: link - thank you, good sirs! – blinkenstack Jan 31 '13 at 14:58

To position the highlight rectangle properly, the array position of the highlighted value must be converted into a graphics coordinate. For a data array of $n$ rows, to highlight the value at row $i$ and column $j$ requires the bottom left corner of the rectangle to be placed at graphics coordinate $\{j-1, n-i\}$


highlight[data_, patt_, directives_] :=
 {FaceForm[None], EdgeForm[directives],
  Rectangle[{#2 - 1, Length[data] - #1}] & @@@ Position[data, patt]}

data = N@Table[x^2 + y^2, {x, -4, 3, 6/11}, {y, -4, 3, 6/11}];

MatrixPlot[data, Epilog -> {
   highlight[data, Min[data], {Thick, Red}],
   highlight[data, x_ /; 5 < x < 6, {Blue, Dashed}]

enter image description here

share|improve this answer

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.