Filling inside of a plot with color (gradient)

조회 수: 103 (최근 30일)
Niklas Kurz
Niklas Kurz 2021년 6월 3일
댓글: Star Strider 2021년 11월 1일
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...

채택된 답변

Star Strider
Star Strider 2021년 6월 3일
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
Sergio Yanez-Pagans
Sergio Yanez-Pagans 2021년 10월 31일
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 2021년 11월 1일
@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!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by