Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding "hold on" after "figure" causes the plot to be different

I've three 5x5 matrices, i.e. X, Y and U. Here's how they look like.

X =

         0         0         0         0         0
    0.2500    0.2500    0.2500    0.2500    0.2500
    0.5000    0.5000    0.5000    0.5000    0.5000
    0.7500    0.7500    0.7500    0.7500    0.7500
    1.0000    1.0000    1.0000    1.0000    1.0000


Y =

         0    0.2500    0.5000    0.7500    1.0000
         0    0.2500    0.5000    0.7500    1.0000
         0    0.2500    0.5000    0.7500    1.0000
         0    0.2500    0.5000    0.7500    1.0000
         0    0.2500    0.5000    0.7500    1.0000


U =

         0    0.2474    0.4794    0.6816    0.8415
    0.3093    0.5991    0.8519    1.0519    1.1862
    0.7191    1.0224    1.2623    1.4238    1.4962
    1.1929    1.4727    1.6611    1.7460    1.7220
    1.6829    1.8980    1.9950    1.9680    1.8186

Now if I try to plot U using the following snippet:

figure;
mesh(X, Y, U);

This is the output:

enter image description here

If instead I use the following code:

figure;
hold on;
mesh(X, Y, U);

I get:

enter image description here

Why is this happening? Apparently without hold on I've one more dimension. I don't know for my case which one would be correct. Why does Matlab does this?

like image 463
nbro Avatar asked Nov 16 '16 18:11

nbro


1 Answers

To understand what is happening, it's important to know that for most MATLAB plotting commands, if no axes is explicitly supplied to the command, the current axes is used by default. If no axes exists, one is created and it's appearance is controlled completely by the plotting command. If there is a current axes object, typically the plot command will not modify the appearance of the axes object since in theory you have already customized it.

hold on modifies the NexPlot property of the current axes so that the next object that's plotted won't overwrite previous objects. If no axes currently exists, hold will implicitly create an axes object. The default view of this new axes object is a 2D XY view. Since an axes object now already exists when you call mesh, it just uses the current view (and other axes parameters) rather than altering it.

In the case where you don't call hold on, no axes is present before calling mesh, so mesh creates a default axes object itself with properties that are ideal for visualizing a mesh. This includes using a 3D view and displaying grid lines.

You can manually change the properties of the axes created by hold on by calling view(3) to use the default 3D view and grid on to turn on the grid marks

figure
hold on

% Make it the default 3D view
view(3)

% Show the gridlines
grid on

mesh(X, Y, U)
like image 96
Suever Avatar answered Sep 30 '22 08:09

Suever