MATLAB Answers

Problem changing FaceColor in contourm

조회 수: 3(최근 30일)
Dan Chavas
Dan Chavas 2013년 3월 23일
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
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
[~,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

채택된 답변

Doug Hull
Doug Hull 2013년 3월 27일
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
Dan Chavas
Dan Chavas 2013년 3월 27일
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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by