1

May you please help me on a question on DRAWNOW in Matlab?

When we using drawnow in Matlab, what happens inside?

It stores the figure of the previous-graph, then plot the next-part-of-graph on the same figure?

OR it forgets the whole previous-graph and plot the actual-new-graph (with both previous and next-part)?

The both methods show the same effect: a dynamic graph. But I want to know exactly what happens inside.

Thank you!

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Thien Nhat
  • 13
  • 1
  • 3

1 Answers1

7

drawnow makes sure that MATLAB stops doing whatever its doing and draws in the screen.

If you do

hold on
for ii=1:1000
   plot(ii,rand(1)); % assume complicated maths here
end

MATLAB will run the code and send the plot calls to the graphics engine. However, MATLAB is too busy running the loop to be drawing, as the code has priority over the plot.

If you do

hold on
for ii=1:1000
   plot(ii,rand(1));
   drawnow; % Take a break, draw everything that you must before continuing
end

Then, as the comment says, you temporarily stop the execution of the code, to draw everything in the graphics pipeline, and then continue executing the code.

drawnow has no influence in the fact that the figure is stored or not, that is the job of hold on.

If you are worried about redrawing the whole thing, then make sure you have a look at set and get methods for graphics. With them you can get the xdata, modify it, and set it again, by ensuring that the graphics engine does not redraw/recompute anything else.


Documentation for the hold function:

https://uk.mathworks.com/help/matlab/ref/hold.html

Wolfie
  • 27,562
  • 7
  • 28
  • 55
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Thanks for your quickly support. – Thien Nhat Feb 20 '17 at 10:39
  • @ThienNhat you are welcome. Consider accepting the asnwer as valid if it helped you – Ander Biguri Feb 20 '17 at 10:40
  • 1
    I should take a time to look your answer. Thank you. – Thien Nhat Feb 20 '17 at 10:41
  • Your examples are nice. My question arises when I use `drawnow` package in Python. In Python, it seems to forget any point, although the figure is still _hold on_. So I have to redraw the old-data. – Thien Nhat Feb 20 '17 at 11:03
  • 2
    @ThienNhat This question is tagged "MATLAB" so everyone assumes is about it! Ask the same question about python with the right tag, maybe someone expert on that can tell you more ;) – Ander Biguri Feb 20 '17 at 11:04
  • @AnderBiguri MATLAB is also explicitly stated in the question too! It's possible the OP meant `matplotlib` - the Python plotting library... – Wolfie Feb 20 '17 at 11:45