Filling inside of a plot with color (gradient)
89 次查看(过去 30 天)
显示 更早的评论
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...
0 个评论
采纳的回答
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
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
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 Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!