Confine contour lines to region defined by data
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zachary
am 7 Jan. 2025
Kommentiert: Mathieu NOE
am 9 Jan. 2025
I am using contour() to construct contour lines onto a 2D grid of data defined by the first two columns of "tog.txt," with the value defined by the third column. This data can have 10^3 or more rows. I would like to confine these lines to exist only in the region where the data exists.
I plot the scatter data as well as the border that bounds the data with boundary() for visualization.
The results of each attempt are attached. How can I ensure the contour lines are terminated by the data boundary?
togtxt = load('tog.txt');
x1 = togtxt(:,1);
y1 = togtxt(:,2);
c1 = togtxt(:,3);
% ---- customize figure -------------------------------------------------------
f(1)=figure;
hold all
ax = gca;
set(gca,'LineWidth',1.3,'TickLength',[0.015 0.015],'plotboxaspectratio',[1 1 1]); % size of ticks
set(gca,'ColorScale','log')
xscale log
jjj = boundary(x1,y1);
plot(x1(jjj),y1(jjj));
scatter(togtxt(:,1),togtxt(:,2),2,'filled');
N = 120;
%%% attempt 1
v1 = logspace(log10(min(c1)),log10(max(c1)),50);
I = scatteredInterpolant(x1,y1,c1);
X = linspace(min(x1),max(x1),N);
Y = linspace(min(y1),max(y1),N);
[X,Y] = meshgrid(X,Y);
Z = I(X,Y);
contour(X,Y,Z,v1);
colorbar
%%% attempt 2
% v1 = logspace(log10(min(c1)),log10(max(c1)),50);
% xv = linspace(min(x1),max(x1),N);
% yv = linspace(min(y1),max(y1),N); % will run out of memory unless N small
% [Xm,Ym] = ndgrid(xv,yv);
% MFm = griddata(x1,y1,c1,Xm,Ym);
% contour(Xm, Ym, MFm,v1)
0 Kommentare
Akzeptierte Antwort
Mathieu NOE
am 7 Jan. 2025
Bearbeitet: Mathieu NOE
am 8 Jan. 2025
hello
sorry for not putting alot of comments, but I am in a hurry....
here 's my first suggestion, maybe a little dirty but it works so far
togtxt = load('tog.txt');
x1 = togtxt(:,1);
y1 = togtxt(:,2);
c1 = togtxt(:,3);
% ---- customize figure -------------------------------------------------------
f(1)=figure;
hold all
ax = gca;
set(gca,'LineWidth',1.3,'TickLength',[0.015 0.015],'plotboxaspectratio',[1 1 1]); % size of ticks
set(gca,'ColorScale','log')
jjj = boundary(x1,y1,0.5);
% boundary points
x1b = x1(jjj);
y1b = y1(jjj);
c1b = c1(jjj);
plot(x1b,y1b,'b','linewidth',3);
scatter(x1,y1,2,c1,'filled');
N = 15;
v1 = logspace(log10(min(c1)),log10(max(c1)),N);
I = scatteredInterpolant(x1,y1,c1);
X = linspace(min(x1),max(x1),100);
Y = linspace(min(y1),max(y1),100);
[X,Y] = meshgrid(X,Y);
Z = I(X,Y);
[C,h] = contour(X,Y,Z,v1,'edgecolor','none'); % do not plot yet so use 'edgecolor','none'
[m,n] = size(C);
% do a bit of post processing to avoid contour lines go out of boundary
[matchingValues, ind, ~] = intersect(C(1,:), v1);
ind = [ind; n+1]; % add end (+1)
for k = 1:numel(ind)-1
xc = C(1,ind(k)+1:ind(k+1)-1);
yc = C(2,ind(k)+1:ind(k+1)-1);
% use inpolygon to remove data outside boundary
in = inpolygon(xc,yc,x1b,y1b);
xc = xc(in);
yc = yc(in);
plot(xc,yc,'k');
end
hold off
xscale log
8 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Contour Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



