plotting with a custom colororder
이전 댓글 표시
I'm using matlab 2012b, and the polar function to plot a collection of rays, each with a defined angle and magnitude. I want to color code the rays by their angle so that the color of each ray is graded nicely against its neighbors, rather than varying randomly. I've figured out the basics of creating a custom colororder for the axes and the colors seem to vary nicely. Unfortunately, If I try to plot a subset of my angles, the colors at any given angle are not consistent between the full plot and the subset plot. I've attached example code that generates two plots with the observed problem. The first plot is a set of 30 randomly generated rays. and the second plot is the 15 largest rays selected from the first set. The script stores the colormaps and angles into the variables cmap1 and cmap2. You should be able to see that, although the color progresses smoothly in both plots, the rays at specific angles are not the same colors. This is most pronounced where a large gap appears in the second plot, and least pronounced near 0 or 180deg. If you look at cmap1 and cmap2, both plots appear be using the same colors for rays at the same angles, even when the plots themselves display different colors. I'm clearly missing something about the behavior of matlab plotting with regards to colororder, and I'm hoping someone can help me understand why my two colororders don't produce the same color at the same angle.
Thanks
Tucker
%build angle and magnitude vectors
tmp=sort(3.14*2*(rand(30,1)-.5));
angs1=[tmp,tmp]';
mags1=[zeros(30,1),5*rand(30,1)]';
%plot the max point to set my polar axes, as hold all will prevent drawing
%them later:
figure
polar(0,max(mags1(2,:)))
%make my custom colororder so the color is defined by angle:
colorhsv1=interp1(linspace(-pi,pi,360)',hsv(360),angs1(1,:));
%make a matrix to be able to compare colormaps after running
cmap1=[angs1(1,:)',colorhsv1];
set(gca,'colororder',colorhsv1)
%hold everything so plotting doesn't overwrite the colororder
hold all
%plot all the rays
polar(angs1,mags1);
hold off
%
%now take a subset of the data, the 50% with the largest magnitude:
tmp=mags1(2,:)>median(mags1(2,:));
angs2=angs1(:,tmp);
mags2=mags1(:,tmp);
%plot the max point from my original set to set my polar axes at the same
%scale as the previous plot
figure
polar(0,max(mags2(2,:)))
%make a new custom colororder for the new set of angles:
colorhsv2=interp1(linspace(-pi,pi,360)',hsv(360),angs2(1,:));
%make a matrix to be able to compare colormaps after running
cmap2=[angs2(1,:)',colorhsv2];
set(gca,'colororder',colorhsv2)
%hold everything so plotting doesn't overwrite the colororder
hold all
%plot all the rays
polar(angs2,mags2);
hold off
채택된 답변
추가 답변 (1개)
Christiaan
2015년 3월 18일
Dear Tucker,
A solution for this problem is if you replace the line
colorhsv2=interp1(linspace(-pi,pi,360)',hsv(360),angs2(1,:));
For this code:
for i=1:length(angs2)
for j=1:length(angs1)
if angs2(1,i)==angs1(1,j)
colorhsv2(i,:) = colorhsv1(j,:);
end
end
end
Good luck! Christiaan van Ommeren
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!