Message Boards Message Boards

How to use MatrixPlot dynamically?

Posted 10 years ago

Hi,

I am trying to plot a matrix with MatrixPlot[]. Whenever a value in the matrix changes, MatrixPlot[] should reflect this change graphically. I am using this code:

Dynamic[
 For[i = 1, i < 1000, i++, 
  matrix = Table[RandomReal[{0, 10}], {i, 1, 10}, {j, 1, 10}]]; 
 MatrixPlot[matrix, ColorFunction -> ColorData["Rainbow"]]]

However, I am not sure whether this method stays suitable, if the number of instructions within the Dynamic[] brackets increases. Does anybody know another way for a faster graphical response? Is there a way to specify the dynamic effect only to MatrixPlot[] without having to include the whole code with it?

Thanks Saf

POSTED BY: Saf Al
2 Replies

You can try in one cell

         Dynamic@MatrixPlot[matrix, ColorFunction -> ColorData["Rainbow"]]

Then in another cell (no need for Table, RandomReal can generate a matrix as is) also Do is little better than For

    Do[matrix = RandomReal[{0, 10}, {10, 10}];  Pause[.1],{k,10}]

Added a pause so you can see the update, else it will happen too fast. (ps. first time, you can ignore the error when matrix is not yet defined, one you execute the second cell, it will work). I suggest you use Manipulate or Animate for this instead:

Animate[k; MatrixPlot[RandomReal[{0, 10}, {10, 10}], ColorFunction -> ColorData["Rainbow"]], {k, 10}]
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

Hi Nasser,

Both suggested methods worked perfectly. Thanks so much for your fast reply.

Regards

POSTED BY: Saf Al
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract