How can i plot a boundary line in contour plot
4 views (last 30 days)
Show older comments
mohammad mortezaie
on 22 Aug 2021
Commented: Adam Danz
on 23 Aug 2021
Hello,
I have this code for ploting a contour plot.
%bare electron mass (kg)
a=1.855;
eb=-3.276;
ep=-1.979;
t=-1.844;
FS = 24; %label fontsize
FSN = 24; %number fontsize
LW = 2; %linewidth
% Change default axes fonts.
% set(0,'DefaultAxesFontName', 'Times New Roman');
% set(0,'DefaultAxesFontSize', FSN);
% Change default text fonts.
% set(0,'DefaultTextFontname', 'Times New Roman');
% set(0,'DefaultTextFontSize', FSN);
hbarChar=['\fontname{MT Extra}h\fontname{Times New Roman}'];
iform = complex(0.0,1.0);
% Creating necessary k-vectors
kx = linspace(-1.4,1.4,500);
ky = linspace(-2,2,500);
[k_x,k_y] = meshgrid(kx, ky);
phi=exp(-iform.*k_x*a)+2.*exp(iform.*k_x*a/2).*cos(sqrt(3).*k_y.*a/2);
figure (2)
energy_mesh1 = (eb+ep)/2+sqrt(((eb-ep)/2)^2+t^2.*phi.*conj(phi)) ;
energy_mesh2 = (eb+ep)/2-sqrt(((eb-ep)/2)^2+t^2.*phi.*conj(phi));
g=energy_mesh1-energy_mesh2;
contour(k_x,k_y,g,500,'LineWidth',1.5)
colormap('jet');
I want to connect dark points of the contour plot.
What should I do?
5 Comments
Adam Danz
on 23 Aug 2021
When I run your code, g contains imaginary numbers which result in an error in contour().
Accepted Answer
Adam Danz
on 23 Aug 2021
Edited: Adam Danz
on 23 Aug 2021
Copy of original code with the following changes.
- contour(k_x,k_y,g,500) chaanged to contour(k_x,k_y,real(g),500)
- Commented-out changes to default font properties
%bare electron mass (kg)
a=1.855;
eb=-3.276;
ep=-1.979;
t=-1.844;
FS = 24; %label fontsize
FSN = 24; %number fontsize
LW = 2; %linewidth
% Change default axes fonts.
% set(0,'DefaultAxesFontName', 'Times New Roman');
% set(0,'DefaultAxesFontSize', FSN);
% Change default text fonts.
% set(0,'DefaultTextFontname', 'Times New Roman');
% set(0,'DefaultTextFontSize', FSN);
hbarChar=['\fontname{MT Extra}h\fontname{Times New Roman}'];
iform = complex(0.0,1.0);
% Creating necessary k-vectors
kx = linspace(-1.4,1.4,500);
ky = linspace(-2,2,500);
[k_x,k_y] = meshgrid(kx, ky);
phi=exp(-iform.*k_x*a)+2.*exp(iform.*k_x*a/2).*cos(sqrt(3).*k_y.*a/2);
figure (2)
energy_mesh1 = (eb+ep)/2+sqrt(((eb-ep)/2)^2+t^2.*phi.*conj(phi)) ;
energy_mesh2 = (eb+ep)/2-sqrt(((eb-ep)/2)^2+t^2.*phi.*conj(phi));
g=energy_mesh1-energy_mesh2;
contour(k_x,k_y,real(g),500)
colormap('jet');
Compute the location of lowest values excluding the image edges
% Get lowest points
idx = find(imregionalmax(-real(g)));
% Remove lowest points at edges
[row,col] = ind2sub(size(g),idx);
isEdge = row==1 | row==size(g,1) | col==1 | col==size(g,2);
idx(isEdge) = [];
Compute center and radius of circle
This assumes that the points will lie along the circumference of a circle. If the expected shape is oval, that's an easy change. If the expected shape is unknown, you'll need to sort the points so they are in order and then just plot their connections with a line or compute a polyshape.
% compute circle
xyLoc = [k_x(idx),k_y(idx)];
cnt = mean(xyLoc);
radius = max(range(xyLoc))/2;
% Plot points and circle
axis equal
hold on
h = plot(k_x(idx),k_y(idx), '*w','MarkerSize', 14);
circ = rectangle('Position',[cnt-radius, [2,2]*radius], ...
'Curvature',[1,1], 'EdgeColor', 'w');
Linear connections
First you have to sort the coordinates.
[th, ~] = cart2pol(xyLoc(:,1),xyLoc(:,2));
[~, sortIdx] = sort(th);
xyLocSort = xyLoc(sortIdx,:);
xyLocSort = [xyLocSort; xyLocSort(1,:)]; % wrap polygon
h = plot(xyLocSort(:,1), xyLocSort(:,2), 'w-');
2 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D 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!