Filling inside of a plot with color (gradient)

89 visualizaciones (últimos 30 días)
Niklas Kurz
Niklas Kurz el 3 de Jun. de 2021
Comentada: Star Strider el 1 de Nov. de 2021
I'm coming back to filling areas with color, trying this time to get the most beautiful result:
Here I've got the plain vectorvalued function:
phi = linspace(0,2.*pi,200);
g = (1+cos(phi)).*[cos(phi);sin(phi)];
gx = (1,:);
gy = (2,:);
My first attempt was using polyshape because it was the very thirst thing one gave into my hands:
plot(polyshape(gx,gy))
However, this gives a warning, that I can't preempt. Alhough it's just a warning, it really triggers.
Next try was patch because I've heard about all the magnificent things you can blow up with it, but never dared using it:
patch(gx,gy,1)
what seems to be similar to
fill(fx,gy,1)
surprisingly this works without warning and I should be satisficed (not satisfied).
But while we are at it: how can I implement a color gradient in the given shape? People seem to love color gradients (at least I do) and that's what patch is made for isn't it? It might be done with the last enty related to 'Colorspec', but I didn't get a hang of modifying it...

Respuesta aceptada

Star Strider
Star Strider el 3 de Jun. de 2021
There are likely several (non-explosive) approaches.
One approach —
phi = linspace(0,2.*pi,200);
g = (1+cos(phi)).*[cos(phi);sin(phi)];
gx = g(1,:);
gy = g(2,:);
figure
plot(gx,gy)
axis('equal')
figure
patch(gx, gy, phi)
axis('equal')
colormap(turbo)
I’m not certain what result you want, however giving patch a vector for the color argument can produce interesting results.
Experiment with it to get the result you want.
.
  4 comentarios
Sergio Yanez-Pagans
Sergio Yanez-Pagans el 31 de Oct. de 2021
Great solution Strider! One question, how would you implement this to a simple cos(x) plot? I want to plot cos(x) with a colormap that goes from positive (red) to negative values (blue)
Star Strider
Star Strider el 1 de Nov. de 2021
@Sergio Yanez-Pagans — Thank you!
That is close enough to the original question that I will post it here. Choose whatever interesting value defines the desired colour gradient.
One approach —
phi = linspace(0, 2*pi, 200);
g = abs(cos(phi)).*[cos(phi);sin(phi)];
gx = g(1,:);
gy = g(2,:);
figure
patch(gx, gy, phi)
axis('equal')
colormap(turbo(numel(phi)))
figure
patch(gx, gy, gx)
axis('equal')
colormap(turbo(numel(phi)))
figure
patch(gx, gy, gy)
axis('equal')
colormap(turbo(numel(phi)))
figure
patch(gx, gy, hypot(gx,gy))
axis('equal')
colormap(turbo(numel(phi)))
The absolute value of the cosine is required, otherwise it just plots an empty circle.
Have fun with it!
.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by