# Filling inside of a plot with color (gradient)

153 ビュー (過去 30 日間)
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 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 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
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!
.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangePolygons についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by