Effacer les filtres
Effacer les filtres

Cannot plot a color gradient (Data must be a single matrix Y or a list of pairs X,Y.)

4 vues (au cours des 30 derniers jours)
Hi, i want to plot on the same graph 30 curves with colour gradient decreasing from the first to the last curve (from yellow to red). I have tried this code :
c = colormap(jet(1000))
figure
plot((1:num_images),augm(1:30,1:num_images),'Color',c(600:10:880,:)','-')
  • My variable augm is a matrix of num_images*30.
  • When I run the program without the color part ('Color',c(600:10:880,:)'), it runs perfectly fine but i do not have my gradient of colours...
I think I did not understand completely the 'Color' function.
Could you help me with this ?
Thank you !

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Mai 2017
When you use the 'color' name/value pair, it applies to all lines that you drew in the single plot() call. It is not possible to use 'color' to give different colors for different lines.
If you draw multiple lines with one plot() call, then to give them different RGB colors, you need to loop through the graphics handles setting the Color property.
num_plot = 30;
h = plot(augm(1:num_plot,1:num_images) .', '-'); %make the different plots into columns
color_idx = 600 + 10 * (0:num_plot-1);
for K = 1 : length(h)
set(h(K), 'Color', c(color_idx(K),:) );
end
  1 commentaire
Stephen23
Stephen23 le 2 Mai 2017
"then to give them different RGB colors, you need to loop through the graphics handles setting the Color property."
Not true. See my answer.

Connectez-vous pour commenter.

Plus de réponses (2)

Stephen23
Stephen23 le 2 Mai 2017
Modifié(e) : Stephen23 le 2 Mai 2017
You can easily set the line ColorOrder property of the axes, then you just need one plot call:
>> map = [0,0,1;1,0.4,0.6]; % e.g. cool(2)
>> axes('ColorOrder',map,'NextPlot','replacechildren')
>> plot([0,1],[1,2],'-',[0,1],[2,1],'*-')

fish minox
fish minox le 2 Mai 2017
Hi Stephen, hi Walter,
Thank you both for your answers, i did not know that the color feature was applied to all curves at the same time. thank you again for the great and fast answers !

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by