Following this post I have a function that when run, updates 4 plots. This works as expected, except when I go to change which plot is displayed, it looks like there are remnants of the previously displayed plot. I go from a bar
graph to a surfc
, but I still see the bars across a flat plane. I am currently setting my data and drawing with
set(hplot2, 'yData', ME)
drawnow
Do I need to refresh the axes/plot somehow? I change which plot is on the axes with set(plot1, 'Parent', axes1)
. I have no idea where is problem is arising.
If you're switching between two plots you will either want to clear the axes prior to plotting the next thing using cla
cla(axes1);
or you will want to simply toggle the visibility of the existing plot objects.
% To show only the bar plot
set(hbar, 'Visible', 'on')
set(hsurf, 'Visible', 'off')
% To show only the surf plot
set(hbar, 'Visible', 'off')
set(hsurf, 'Visible', 'on')
The root of the problem, is that an axes can actually hold many plots, so if you simply create a new plot and assign it as a child to an axes, the other plot objects are still there.
If you are creating entirely new graphics objects every time you plot something (by calling bar
or surfc
) using cla
will be easiest. That being said, if you can adjust your code to simply update existing plot objects, that is ideal from both a performance and graphics management perspective.
Also, as another side note. I would discourage using set(plot1, 'Parent', axes1)
after object creation. It is more robust to specify the Parent
property directly in the object constructor. This way you ensure that it goes directly to the axes you want.
plot1 = bar(data, 'Parent', axes1);
Edit
Now that I think about it, since you're toggling between 3D and 2D data, it may be easier to simply have two axes at the same location (one for the bar
and one for the surf
). You would then toggle the visibility of the axes on/off as needed. This way all of your view settings are preserved for a given axes.
barax = axes();
surfax = axes();
% Ensure they are located at the same position
link = linkprop([barax, surfax], 'Position');
hbar = bar(data, 'Parent', barax);
hsurf = surfc(data, 'Parent', surfax);
% Toggle these to switch plots.
set(barax, 'Visible', 'off')
set(surfax, 'Visible', 'on')