Clear Filters
Clear Filters

How to color a region of a plot

10 views (last 30 days)
Sobhan
Sobhan on 18 Oct 2023
Commented: Dyuman Joshi on 18 Oct 2023
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])

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 18 Oct 2023
Edited: Dyuman Joshi on 18 Oct 2023
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 Comments
Sobhan
Sobhan on 18 Oct 2023
Thanks! Works perfectly, and thanks for including the colorbar as well. Was wondering how to get that.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!