Problem changing FaceColor in contourm

4 views (last 30 days)
I am using contourm to make a filled contour plot on a lat/lon map. I want to customize the colors of the lines and fill so that there are only 6 filled colors shown, which I do by manually changing the 'Color' and 'FaceColor' parameters in the Children:
[~,h_Vp] = contourm(lat_Vp_mat,lon_Vp_mat,Vp_month_case_mat,SS_thresh,'Fill','on');
temp = get(h_Vp,'Children');
numthresh = length(SS_thresh);
for pp=1:numthresh
set(temp(pp),'Color',SS_colors{numthresh-pp+1})
set(temp(numthresh+pp),'FaceColor','flat')
end
This works for changing the line color. But when run inside of my script, it does not change the FaceColor; the changes to 'FaceColor' in the Children apparently get ignored and are overwritten back to default. If I stop the script to debug at the end of these lines of code and then copy and paste the code into the command line manually, it will correctly change the fill colors.
Anyone know how to fix this? Or an alternative way to do this?
Here is the code:
%%Define data
lat_mat = repmat(10:-1:1,10,1)';
lon_mat = repmat(1:10,10,1);
data_mat = lat_mat.^2+lon_mat.^2;
%%Define color thresholds
c_thresh = [20 50 80 120 130]; %contour thresholds
c_colors = {[.9 .9 .9] [.5 .75 0] [1 1 0] [1 .5 0] [1 0 0] [.5 0 0]};
%%Make filled contour plot
figure(1)
set(gcf,'Visible','on')
[~,h_pl] = contourm(lat_mat,lon_mat,data_mat,c_thresh,'Fill','on');
temp = get(h_pl,'Children');
numthresh = length(c_thresh);
for pp=1:numthresh
set(temp(pp),'Color',c_colors{numthresh-pp+1})
set(temp(numthresh+pp),'FaceColor','flat')
end

Accepted Answer

Doug Hull
Doug Hull on 27 Mar 2013
It has been my experience that when you see a problem where the code works in debug mode, but not when run all at once, you need drawnow added.
What is drawnow?
MATLAB puts the graphics commands in a buffer, and runs all the code in the buffer when it has time. This can lead to optimizations for speed. Drawnow forces MATLAB to execute everything in the buffer RIGHT NOW.
When you are in debug mode, we are very slow. As we walk through the code, MATLAB is idle a lot of the time. While it is waiting for us, it draws. It is like having a drawnow at the end of every line.
My guess, is you should put a drawnow in after the contourm. MATLAB will then be forced to draw the contourm. I did this and it made a different looking visualization. Not clear if it is the right one, but it is different.
I suspect that in clearing the buffer, MATLAB sees all these different commands trying to change the graphic, realizes they overwrite each other, so do not bother with all of them. Somewhere along the line MATLAB does the wrong one last.
  1 Comment
Dan Chavas
Dan Chavas on 27 Mar 2013
Awesome, that does work -- I put "drawnow" after the line that changes the FaceColor inside of the for loop. Very strange. Thanks so much! Dan

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!