How can i plot a boundary line in contour plot

조회 수: 6 (최근 30일)
mohammad mortezaie
mohammad mortezaie 2021년 8월 22일
댓글: Adam Danz 2021년 8월 23일
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
Adam Danz
Adam Danz 2021년 8월 23일
When I run your code, g contains imaginary numbers which result in an error in contour().
Adam Danz
Adam Danz 2021년 8월 23일
I have the same question as @Wan Ji. What are the dark points (your image didn't help) and how do you want to connect them? With lines? Perhaps an illustration would help.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2021년 8월 23일
편집: Adam Danz 2021년 8월 23일
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
mohammad mortezaie
mohammad mortezaie 2021년 8월 23일
편집: mohammad mortezaie 2021년 8월 23일
Thank you so much but I want a hexagonal shape when the dark points are connecting.
circle shape is not true for my porpuse.
Adam Danz
Adam Danz 2021년 8월 23일
Answer updated.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by