How to color a region of a plot

10 次查看(过去 30 天)
How do I color the disk in the middle (where r<1) white over the imagesc but under the vectors so they are still visible. Alternatively, how do I omit this region from the imagesc.
x=linspace(-2,2,300);
y=linspace(-2,2,300);
x2=x.^2;
y2=x.^2;
xy=x.*y';
U=1;
a=1;
vx=U*a^2*(x2./(x2+y2').^2-y2'./(x2+y2'));
vy=U*a^2*(xy./(x2+y2').^2+xy./(x2+y2'));
r=sqrt(x2+y2');
vx(abs(r)<1)=0;
vy(abs(r)<1)=0;
imagesc(x,y,vx)
hold on
quiver(x(10:10:end),y(10:10:end),vx(10:10:end,10:10:end),vy(10:10:end,10:10:end),1.5,'k')
axis equal
xlim([-2 2])
ylim([-2 2])

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-10-18
编辑:Dyuman Joshi 2023-10-18
You can set the corresponding values to NaN and change the color of the NaN values to white (or rather transparent) -
x=linspace(-2,2,300);
y=linspace(-2,2,300);
x2=x.^2;
y2=x.^2;
xy=x.*y';
U=1;
a=1;
vx=U*a^2*(x2./(x2+y2').^2-y2'./(x2+y2'));
vy=U*a^2*(xy./(x2+y2').^2+xy./(x2+y2'));
r=sqrt(x2+y2');
vx(abs(r)<1)=NaN;
vy(abs(r)<1)=NaN;
h = imagesc(x,y,vx);
set(h, 'AlphaData', ~isnan(vx))
colorbar
hold on
quiver(x(10:10:end),y(10:10:end),vx(10:10:end,10:10:end),vy(10:10:end,10:10:end),1.5,'k')
axis equal
xlim([-2 2])
ylim([-2 2])
  2 个评论
Sobhan
Sobhan 2023-10-18
Thanks! Works perfectly, and thanks for including the colorbar as well. Was wondering how to get that.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by