Custom map coloring and legend in Matlab 2014b

조회 수: 2 (최근 30일)
Attila Lazar
Attila Lazar 2014년 10월 11일
편집: Matt Tearle 2014년 10월 16일
I have recently installed Matlab 2014b and some of my old code does not work anymore. I have a programmatic gui that displays some of the results on a GIS map. I need to use quintiles (i.e. five classes) to explicitly distinguish between areas on the map. Thus I cannot use the standard Matlab (continuous) colorbar. This code used to work with Matlab 2014a:
UnionShape=shaperead('Union_new_653.shp');
Sundarbans=shaperead('Sundarbans_new.shp');
mapshow(UnionShape, 'FaceColor','white');
mapshow(Sundarbans, 'FaceColor','white');
% create the colours
polygonColors = makesymbolspec('polygon',{'Temp1',5,'FaceColor',[1 0 0]},... % red
{'Temp1',4,'FaceColor',[1 0.5 0]},... % orange
{'Temp1',3,'FaceColor',[1 1 0]},... % yellow
{'Temp1',2,'FaceColor',[0.5 1 0.5]},... % greenish
{'Temp1',1,'FaceColor',[0 1 0]},... % green
{'Default','FaceColor','w'}); % no data
mapshow(UnionShape,'SymbolSpec',polygonColors); hold on;
% Create the legend
txt1=[num2str(Min),' - ', num2str(Med1),' ',unit];
txt2=[num2str(Med1),' - ', num2str(Med2),' ',unit];
txt3=[num2str(Med2),' - ', num2str(Med3),' ',unit];
txt4=[num2str(Med3),' - ', num2str(Med4),' ',unit];
txt5=[num2str(Med4),' - ', num2str(Max),' ',unit];
b=bar(round(rand(5,5)*10),'stacked');
colormap([0 1 0;0.5 1 0.5;1 1 0;1 0.5 0;1 0 0])
legend(txt1,txt2,txt3,txt4,txt5)
set(b,'Visible','off'); hold off;
The code updates the attribute table of the polygon shapefile (this part is not shown in the above code), and displays the 'Temp1' field. Thus, Temp1 specifies which polygon belongs to which of the five classes. The difficulty was to create a legend with only five colors. Thus, I plotted a dummy bar next to the map and simply colored this barchart based on the colors of the map. However, this method (i.e. to color the my custom legend) does not work anymore in Matlab 2014b. The map is colored properly, but the custom legend (i.e. the bar chart) has only one color, that is red. Could anyone help, please?

답변 (1개)

Matt Tearle
Matt Tearle 2014년 10월 16일
I can't exactly replicate your problem because I don't have the files you're reading from, but I can get the same basic problem with a bunch of random patches. There seems to be some issue with ordering of the axes children and/or how legend is picking up the colors. Anyway, if possible, try switching the order in which you make the plots:
  • plot the bars & set the colormap
  • make the legend
  • do a drawnow (this is strange but seems to be necessary)
  • hold on
  • mapshow
I don't know if mapshow will behave differently, but this worked for me with random patches.
  댓글 수: 2
Attila Lazar
Attila Lazar 2014년 10월 16일
Thanks! Unfortunately, none of the variations worked. The most promising was this, but failed at the end:
mapshow(UnionShape,'SymbolSpec',polygonColors); hold on;
title(FigTitle,'FontSize',14,'FontWeight','bold');
b=bar(round(rand(5,5)*10),'stacked');
colormap([0 1 0;0.5 1 0.5;1 1 0;1 0.5 0;1 0 0])
legend(txt1,txt2,txt3,txt4,txt5)
drawnow;
set(b,'Visible','off');
hold off;
print('-f','-r400','-djpeg',filename)
When using this version, the map was drawn correctly, and the legend had proper colors, but then the colors of the legend turned back to red and when I checked the printed figure (i.e. jpeg file), the saved file had the same (all red) legend. My feeling is that the set(b,'Visible','off') command line causes some kind of 'memory loss' to the program. If I place this line somewhere else in the code, the legend always red. Unfortunately, the shapefile I am using has 653 polygons and thus it is quite large (8.5 MB when zipped). So I cannot upload to this site.
What is also strange is that the proportions of the map has also changed compared to Matlab 2014a (as if it was in a different projection system) and the whole map drawing process seems to take longer.
Matt Tearle
Matt Tearle 2014년 10월 16일
편집: Matt Tearle 2014년 10월 16일
It seems like legend detects what's in the figure whenever it is invoked or modified. I guess printing somehow triggers this update? (And because the bars are now invisible, it messes up the legend colors.) Hmm. It may be time for you to call support -- they have people ready to deal specifically with 14b graphics questions :)
Regarding the other stuff with mapshow, if it's rendering a ton of individual polygons (I don't know how mapshow works under the hood), that may cause some slowdown. But the changing proportions is weird. The new graphics includes a slightly different way that axis limits are calculated -- is it possibly something to do with that? Anyway, these are probably all things that should be reported to customer support.
Good luck! Sorry I couldn't find a magic workaround fix :)

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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by