I have a matrix consisting of 5 columns. The first and second columns are for x_start & y_start of the line, the third and fourth are for x_end & y_end. The fifth is -concentration of contaminant in this line- giving the value for the color of my graph. I want to plot x_start & y_start with x_end & y_end for each line and give this line a color based on the value of concentration which is ranging in color from Cmin to Cmax within a colormap. Any help?
问题:
回答1:
I hope I've understood your question correctly. You can try the following code. Assuming your data is in the following format:
% x_start y_start x_end y_end concentration
A = [0 0 1 1 0.3
0 1 3 3 0.6
3 1 6 2 1.2];
and you use one of the matlab colormaps
cmap = colormap;
Based on a minimum and maximum concentration (first and last value of the colormap) you can calculated to indices of the colors by
con_min = 0;
con_max = 2;
ind_c = round((size(cmap,1)-1)*A(:,5)/(con_max-con_min))+1
and overwrite the ColorOrder
of the graph with
figure;
set(gca,'ColorOrder',cmap(ind_c,:),'NextPlot','replacechildren');
and do the plot with
plot([A(:,1) A(:,3)]',[A(:,2) A(:,4)]');
EDIT: To display the correct colorbar, just add
colorbar % Display the colorbar
caxis([con_min con_max]) % Scale it to the correct min and max
回答2:
My approach which is not fully automated (if you expected something like this) would go:
- Determine the range of the
concentration of contaminant
meaning the min and max value. - Decide how many different plots you want and split your concentration values into the bins.
- Plot each line by providing an index of the bin each sample belong to.
To give an example:
I usually prefer a combined scheme with varying line style, point style and color
lines = '-:';
points = '<>^vdho';
color = 'rgbkm';
So if a sample falls into 1st bin (imagine i = 1
) I will do something like:
i = lines(mod(i,length(lines))+1);
p = points(mod(i,length(points))+1);
c = color(mod(i,length(color))+1);
plot(..., sprintf('%s%s%s', l, p, c));
and since you have a random combination of lines, points and colors the different lines you get are 2*7*5 = 70. Of course you can change the combinations.
I guess maybe @Nemesis's solution might be more elegant but this one gives direct control on some parameters and that's why I provide it.
P.S. I use only these colors because other like cyan ('c') or yellow ('y') do not show really well.