Custom color map for plotting matrix values

Here's an example of what I want to do, but instead of a loop for the plot, I want to feed in plot(x,y); instead of plot(x,y(i,:))
% Define vector values
n = 101; N = 30;
x = linspace(0,2,n);
y = ((-N/2:N/2)').*x.^4;
% Plot
figure; hold on;
% Prepare colormap
Color1 = [0,0.5,1];
Color2 = [1,0.5,0];
for i = 1:N
Col = Color1 + (Color2 - Color1)*(i-1)/(N-1);
plot(x,y(i,:),'Color',Col);
end
Where, I want to replace
for i = 1:N
Col = Color1 + (Color2 - Color1)*(i-1)/(N-1);
plot(x,y(i,:),'Color',Col);
end
with
for i = 1:N
Col(i,:) = Color1 + (Color2 - Color1)*(i-1)/(N-1);
end
plot(x,y,'Colors',Col);
with the following plot as the result.

2 comentarios

Adam
Adam el 28 de Jul. de 2017
What are you actually trying to do? Colourmaps don't apply to line plots whose default colours are controlled by the 'ColorOrder' property of the colourmap.
But your 'Col' variable is just a 3-element vector - i.e. a single colour, so if you just want to set all lines to the one colour then that is trivial, but doesn't count as a colourmap so I assume something is missing from your example?
AK19
AK19 el 30 de Jul. de 2017
Editada: AK19 el 30 de Jul. de 2017
Ah, thanks for pointing that out.
I want to change the colours of the line plots without having to set a hold on; plotting loop.
in the loop, it should be (and I've alredy edited it)
for i = 1:n
Col(i,:) = Color1 + (Color2 - Color1)*(i-1)/(n-1);
end
And I'll try out the ColorOrder property, thanks!

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 30 de Jul. de 2017

1 voto

I think you probably want scatter(). With scatter(), you can pass in an array of colors so that you can vary each data point's color on a point-by-point basis.
If you really want color order, see my attached demo, but I don't think you want that over scatter.

3 comentarios

AK19
AK19 el 30 de Jul. de 2017
I like that you can alter the colours, but I want to feed a matrix of y-values into the plotting function, and scatter doesn't do that. I also still want line plots.
Image Analyst
Image Analyst el 30 de Jul. de 2017
OK, now that I see what you want in your plot you just inserted I guess you want colored lines so scatter() can't do that.
But did my colororder demo help you? Look - I'll even include a screenshot from it:
Look like what you want, right?
AK19
AK19 el 17 de Nov. de 2017
Looks great! Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 28 de Jul. de 2017

Comentada:

el 17 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by