# plotting with a custom colororder

6 views (last 30 days)
Tucker on 17 Mar 2015
Answered: Tucker on 23 Mar 2015
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

Tucker on 23 Mar 2015
I think I have arrived at the answer to this question.
The problem is the single point I was drawing to set the scale of the polar plot. Even though I was drawing this point before setting the colororder, MatLab was counting that draw when assigning colors. Since this point was not included in the colororder, this had the effect of shifting the colors for all the subsequent angles. Since the two plots have different separations between points, the change in color at each direction will be different.
I had thought that setting the colororder reset the draw count, but this does not seem to be the case. My issue can be resolved by simply including a dummy line in the colororder for the plotted point.

Christiaan on 18 Mar 2015
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
Tucker on 19 Mar 2015
Hi Christiaan,
Thanks for the response, unfortunately, your code produces the same problem as my original. The way I wrote my example was perhaps lazy, but the interpolation for colorhsv2, uses a subset of the angles used for interpolating colorhsv1. This means that colorhsv2 in my code is actually a subset of the colorhsv1 matrix, even though it was generated separately.
A more 'clean' solution than my original code, or your looped example would be to replace my call to interp1, with logical indexing as I did with angs2 and mags2:
colorhsv2=colorhsv1(tmp,:);
All 3 of these methods produce an identical colorhsv2 matrix, and all 3 methods show different colors between the main plot and the subset plot
Tucker

### Categories

Find more on Surface and Mesh Plots 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!