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

 Akzeptierte Antwort

Voss
Voss am 17 Jan. 2025
Bearbeitet: Voss am 17 Jan. 2025

0 Stimmen

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 Kommentare

mohammad mortezaie
mohammad mortezaie am 17 Jan. 2025
Thank you very much.
Voss
Voss am 17 Jan. 2025
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sandeep Mishra
Sandeep Mishra am 17 Jan. 2025
Bearbeitet: Sandeep Mishra am 17 Jan. 2025

0 Stimmen

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 Kommentare

mohammad mortezaie
mohammad mortezaie am 17 Jan. 2025
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 am 17 Jan. 2025
Actually I need a quiver plot with colored vectors.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Vector Fields finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 17 Jan. 2025

Kommentiert:

am 17 Jan. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by