Define colormap according to certain variable.

53 次查看(过去 30 天)
Hey!
I have hundreds of 2D-curves in one plot. Curves are produced by function where is a random variable. Now I would like to specify that the curves are displayed with different colors with respect to the value of a used random variable.
What is the proper way to manage this?

回答(2 个)

Hekseli
Hekseli 2013-10-24
Thanks. I solved this in a following way!
%Defining a colormap
CMap=colormap(jet(nsamples));
%Sorting the initial y-coordinates for colormap (So colors depend on the initial y coordinate of a particular curve. Could be some other variable too.)
init_Y_p_sorted=sort(Y_p(1,:));
%Plotting curve by curve in a loop
for j=1:nsamples
ph=plot(X_p(:,j), Y_p(:,j));
%Find the sorted index of a initial coordinate
CMap_ind=find(init_Y_p_sorted==Y_p(1,j));
set(ph,'Color',CMap(CMap_ind,:));
hold on;
Only problem is that if nsamples is a large number, then the colorbar of the figure becomes totally black so somekind of bining should be still added.
  3 个评论
Walter Roberson
Walter Roberson 2019-8-8
The only reference to Color there is in the set(ph) call. ph is the result of the plot() call, so ph will be a graphics handle to a lineseries object, and the set() call will be setting the line color of the lineseries object.

请先登录,再进行评论。


Jonathan LeSage
Jonathan LeSage 2013-10-23
You can define a colormap using the hsv function and then define the color of each line according to the random variable that you mention. If you have a vector of the random variables prior to plotting, you can group them via histc and then use the different bin counts to determine which color to assign each line.
If your random numbers are already integers, the problem becomes fairly straightforward. Here is some example code demonstrating the general procedure you might want to try!
% Generate arbitrary random variables
numRandom = 10;
rangeInts = [1 5];
randomVariables = randi(rangeInts,numRandom,1);
% Generate 5 hue-saturation-value color map for your data
% Each row of 'colorVec' defines a different RGB color for you lines
colorVec = hsv(numRandom);
% Define Arbitrary Function for Plotting
xVec = 0:0.1:10;
hold on;
for i = 1:numRandom,
% Arbitrary function which relies on the random variable
yVec = randomVariables(i)*xVec + randn(1,numel(xVec));
% Plot line with color selected by random number
plot(xVec,yVec,'Color',colorVec(randomVariables(i),:))
end
hold off;
Hope this helps you to get started!

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by