How to make a contourf plot follow a line?
2 views (last 30 days)
Show older comments
Good morning! I plotted a contourf plot in a figure, until then evrything is working, but now, I awant to make that contour follow a line. I have been trying to use a for loop but I cannot make it work. Any idea on what I could try to achieve my goal?
Here is my foor loop:
for i = 1:200
c = contourf(X, Y, -) %% different variables from my code to make it easier
xticks([])
yticks([])
cmap = colormap("summer");
xline(0, "black", "LineWidth", 2)
view([180 90]); % changes the view so that the pattern is facing down
pause(2)
drawnow
end
Thank you
2 Comments
Mathieu NOE
on 23 Jun 2023
Edited: Mathieu NOE
on 23 Jun 2023
hello
can you share your data ? and a picture / sketch of what you want to achieve
Accepted Answer
Mathieu NOE
on 23 Jun 2023
hello again
here a small demo code based on a first given elipse (bottom left on the picture)
you can use a spacing between them if needed (xr_spacing)
the intersections function is attached
% dummy ellipse
n = 100;
alpha = pi/n+(0:n)/n*2*pi;
a = 1;
b = 3;
x0 = 2.5;
y0 = 3;
x = x0 + a*cos(alpha);
y = y0 + b*sin(alpha);
% create the reference line
xref = (0:0.1:10);
yref = y0*ones(size(xref));
figure(1);
plot(x,y,'*-b',xref,yref,'r--')
xlim([0 8])
ylim([0 8])
axis square
% for fun let's apply a rotation angle to this
% rotation around the origin(0,0)
% Create rotation matrix
theta = 45; % to rotate 25 degrees counterclockwise
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
% Rotate your point(s)
for k =1:numel(x)
rotpoint = R*([x(k) ;y(k)]);
xr(k) = rotpoint(1);
yr(k) = rotpoint(2);
end
for k =1:numel(xref)
rotpoint = R*([xref(k) ;yref(k)]);
xrefr(k) = rotpoint(1);
yrefr(k) = rotpoint(2);
end
%
figure(2);
plot(xr,yr,'*-b',xrefr,yrefr,'r--')
xlim([-4 8])
ylim([0 12])
axis square
hold on
% now create replicates of the rotated elipse along the reference line
% intersections makes finding the intersection points very easy.
[xout,yout] = intersections(xr,yr,xrefr,yrefr,1);
dx = diff(xout);
dy = diff(yout);
% do n duplicates
xr_spacing = 0.25; % along the reference line (rotated)
xy_spacing = R*([xr_spacing ;0]); % gives the true x and y spacing in the absolute referential
for n = 1:3
newx = xr + n*(dx+xy_spacing(1));
newy = yr + n*(dy+xy_spacing(2));
plot(newx,newy);
end
19 Comments
More Answers (0)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!