Add a colorbar for this quiver plot

13 ビュー (過去 30 日間)
mohammad mortezaie
mohammad mortezaie 2025 年 1 月 17 日
コメント済み: Voss 2025 年 1 月 17 日
I wrote a code to have a quiver plot as I attached here. I want to add a colorbar to this code as it shows the magnitude of the vectors. How can I do that?
nu1=3.87e5;
nu2=0.46e5;
v_m=2.84e5;
v_p=7.18e5;
%
% nu1=8.4;
% nu2=1;
% v_m=1;
% v_p=2.5;
s=1;
s1=-1;
kesi=1;
kesi1=-1;
DeltaSO=41.9e-3;
alpha=1;
A=0.0;
hbar=1;
V=0;
iform=complex(0.0,1.0);
for i=1:25
k_x=-0.3+2*(i-1)*0.3/25;
for j=1:25
k_y=-0.3+2*(j-1)*0.3/25;
sigmax=[0 1; 1 0];
sigmay=[0 -iform; iform 0];
sigmaz=[1 0; 0 -1];
sigma0=[1 0; 0 1];
H1=hbar*k_x*nu1*sigmay-hbar*k_y*(s*nu2*sigmax+kesi*v_m*sigma0+kesi*v_p*sigmaz)+DeltaSO*(alpha-s*kesi)*sigmax+A*(s*sigmaz-kesi*sigmax)+V*sigma0;
H2=hbar*k_x*nu1*sigmay-hbar*k_y*(s1*nu2*sigmax+kesi*v_m*sigma0+kesi*v_p*sigmaz)+DeltaSO*(alpha-s1*kesi)*sigmax+A*(s1*sigmaz-kesi*sigmax)+V*sigma0;
H3=hbar*k_x*nu1*sigmay-hbar*k_y*(s*nu2*sigmax+kesi1*v_m*sigma0+kesi1*v_p*sigmaz)+DeltaSO*(alpha-s*kesi1)*sigmax+A*(s*sigmaz-kesi1*sigmax)+V*sigma0;
H4=hbar*k_x*nu1*sigmay-hbar*k_y*(s1*nu2*sigmax+kesi1*v_m*sigma0+kesi1*v_p*sigmaz)+DeltaSO*(alpha-s1*kesi1)*sigmax+A*(s1*sigmaz-kesi1*sigmax)+V*sigma0;
[v1,E1]=eig(H1);
[v2,E2]=eig(H2);
[v3,E3]=eig(H3);
[v4,E4]=eig(H4);
KX(i,j)=k_x;
KY(i,j)=k_y;
sx(i,j)=v4(:,2)'*sigmax*v4(:,2);
sy(i,j)=v4(:,2)'*sigmay*v4(:,2);
end
end
quiver(KX,KY,sx,sy,1)
q.ShowArrowHead = 'off';
q.Marker = 'o';
Qu

採用された回答

Voss
Voss 2025 年 1 月 17 日
編集済み: Voss 2025 年 1 月 17 日
figure
hold on
NColors = 10;
scale_factor = 0.02;
sr = sqrt(sx.^2+sy.^2);
[sr_min,sr_max] = bounds(sr,'all');
qidx = discretize(sr,linspace(sr_min,sr_max,NColors+1));
sx_tmp = sx.*scale_factor;
sy_tmp = sy.*scale_factor;
cmap = parula(NColors);
q = gobjects(1,NColors);
for ii = 1:NColors
idx = qidx == ii;
q(ii) = quiver(KX(idx),KY(idx),sx_tmp(idx),sy_tmp(idx),0,'Color',cmap(ii,:));
end
colormap(cmap)
colorbar
  2 件のコメント
mohammad mortezaie
mohammad mortezaie 2025 年 1 月 17 日
Thank you very much.
Voss
Voss 2025 年 1 月 17 日
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Sandeep Mishra
Sandeep Mishra 2025 年 1 月 17 日
編集済み: Sandeep Mishra 2025 年 1 月 17 日
Hi mohammad,
You can use the MATLAB ‘scatter’ function to add a colorbar to the quiver plot.
Refer to the following code snippet to implement the colorbar to a quiver plot:
% Calculating Magnitude
magnitude = sqrt(sx.^2 + sy.^2);
q = quiver(KX, KY, sx, sy, 1);
q.Marker = 'o';
% Adding Scatter to quiver plot
hold on
scatter(KX(:), KY(:), 20, magnitude(:), 'filled'); % '20' sets marker size
% Adding colorbar
colorbar;
For more information, refer to the following MathWorks Documentation:
  1. ‘scatter’ function: https://www.mathworks.com/help/releases/R2024b/matlab/ref/scatter.html
  2. ‘colorbar’ function: https://www.mathworks.com/help/releases/R2024b/matlab/ref/colorbar.html
I hope this helps you!
  2 件のコメント
mohammad mortezaie
mohammad mortezaie 2025 年 1 月 17 日
Thank you.
I want a plot without these filled circles. I need only color vectors.
Is there a code to solve this?
mohammad mortezaie
mohammad mortezaie 2025 年 1 月 17 日
Actually I need a quiver plot with colored vectors.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeVector Fields についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by