MATLAB: Streaks while using color indexing with fill

color indexingcolormapfillinterpolationMATLAB

I am trying to fill the area between two curves with a color that depends on a value at each point along one of the curves, but I am getting variations in color that should not be there. I have two (100×1) vectors: y1 and y2 which I plot against a third vector x (also 100×1) such that every point in y2 is directly above (or below or at the same location as) a point on y1. I have a fourth vector v that is the same length as x, y1, and y2, which I would like to use as a color index to fill the area between the curves defined by (x,y1) and (x,y2). (The datasets are attached below, for reference.) The command fill needs a closed loop to define the area to be filled, so I have created vectors X and Y to form the loop:
X=[x;flipud(x)];
Y=[y1;flipud(y2)];
If I type
fill(X,Y,'b')
the area between y1 and y2 will be filled in with blue. I want to fill this area with colors defined by the third variable, so I created C to serve as the color index (per the helpfile on fill):
C=[v;flipud(v)];
fill(X,Y,C)
I therefore expect the area between y1 and y2 to be filled with a color that varies horizontally (from one x value to the next), but not vertically, since each value of C corresponds to a single value in X. I get substantial vertical variation, however:
The command fill works as described in the documentation when I do not use a color index, so I believe that I am using it correctly. I have verified that the color index C has the same value at both the upper and lower points in x, since that would account for at least some variation in C between the curves at the same x position. There was no error in the color index, and it would not account for the degree of value variation in any event.
Is this a known issue while working with fill? Does anyone have suggestions as to 1) why this is happening and/or 2) how I can fix it?

Best Answer

I solved this two ways: The 'quick and dirty' way, which involved plotting the transpose of my data so that the interpolation looked reasonable (even though the underlying problem was still present--see the discussion below for details), and using rotated text arrow annotations (because textboxes are not rotatable) in conjunction with getframe() to save the figure as it appeared before rotating. (This is fiddly to set up, since you have to manually set the position of the axis and tick labels. It's neither particularly quick nor particularly imprecise, but it's a faster-running fix than the next one.)
And the technically more-correct method, where I selected each consecutive pair of x locations on each curve (so, four points: two each on the upper and lower curves) to form a trapezoid, then filled the trapezoid with the value in the upper left corner (I could have done a simple arithmetic mean of the values or any other combination you could name, but applied the KISS principle instead). This was slower to run, but (surprisingly) faster to set up. It also has the advantage of being able to represent three variables in RGB space (e.g., concentrations in a three-part solution).
All of which will hopefully become obsolete as the older versions of MATLAB are used less and less.