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.
%build angle and magnitude vectors
%plot the max point to set my polar axes, as hold all will prevent drawing
%them later:
%make my custom colororder so the color is defined by angle:
%make a matrix to be able to compare colormaps after running
%hold everything so plotting doesn't overwrite the colororder
hold all
%plot all the rays
hold off
%now take a subset of the data, the 50% with the largest magnitude:
%plot the max point from my original set to set my polar axes at the same
%scale as the previous plot
%make a new custom colororder for the new set of angles:
%make a matrix to be able to compare colormaps after running
%hold everything so plotting doesn't overwrite the colororder
hold all
%plot all the rays
hold off

Accepted Answer

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.

More Answers (1)

Christiaan on 18 Mar 2015
Dear Tucker,
A solution for this problem is if you replace the line
For this code:
for i=1:length(angs2)
for j=1:length(angs1)
if angs2(1,i)==angs1(1,j)
colorhsv2(i,:) = colorhsv1(j,:);
Good luck! Christiaan van Ommeren
  1 Comment
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:
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!