How to fix the legends for these 3 cases?
1 次查看(过去 30 天)
显示 更早的评论
I've plotted 3 subplots to test out how is the legend arrange. Multiple lineplot for different condition in each subplot. I want to label the respective lines correctly, however, I failed. First subplot has label respectively but I want to rename. Second and third subplots can be renamed, but cannot represent each line colour.
Please give me some suggestion for the correction of my code (only the plotting figures part). Thank you.
%----Physical constant---------% m = 9.1e-31; % electron mass [kg] c= 3e8; % speed of light [m/s] eps = 8.854e-12;% permittivity [F/m] e = 1.6e-19; % charge [Coulomb] h_bar = 6.626e-34 /2/pi; % reduced Planck constant_bar [m^2 kg/s]
%----Calculating K for different ro and different energy----% E_beam = linspace(10e6,100e6,10); %Electron beam energy gamma = E_beam./0.511e6; gamma'; M=gamma'*ones(1,10)
n =linspace(1e24,1e25,10); %density n'; N=n'*ones(1, 10)
P =sqrt( M.*(N/1e6))
ro = linspace(1e-6, 5e-6,5); for i = 1:length (ro) K(:,:,i)= 1.33E-10.*P.*(ro(i)/1e-6) end
%----Plasma frequency----% plasma_freq=sqrt((n(1,:)*(1.6e-19)^2)/(9.1e-31*8.85e-12)); lambda_plasma = 2*pi*c./plasma_freq; % plasma wavelength [m] wavenum = 2*pi./lambda_plasma; % [m-1]
%----Betatron parameter----% w_beta = plasma_freq./sqrt(2*gamma); % fundamental beta freq [s-1] % lambda_beta = 2*pi*c/w_beta; % betatron period [m] lambda_beta_1um = (2*pi*gamma(1,:)*ro(1,1))./K(1,:,1); lambda_rad_1um = lambda_beta_1um./(2.*gamma(1,:).*gamma(1,:)); % fundamental wavelength
%----Plotting figures----% figure (1) subplot(3,1,1) AX= plot(gamma(1,:),K(:,:,1),'r',gamma(1,:),K(:,:,2),'g',gamma(1,:),K(:,:,3),'k',gamma(1,:),K(:,:,4),'b',gamma(1,:),K(:,:,5),'c','LineWidth',2); title ('legend is correct for each but cannot rename each of them'); ylabel('K'); xlabel('gamma'); ff = findobj('Color','r'); gg = findobj('Color','g'); hh = findobj('Color','k'); tt = findobj('Color','b'); yy = findobj('Color','c'); v = [ff(1) gg(1) hh(1) tt(1) yy(1)]; legend(v, 'Location','northeastoutside');
subplot(3,1,2) h1 = 'rgkbc'; h2=('12345'); for iter=1:length(ro) ax=plot(N(:,i),K(:,:,iter),'Color',h1(iter),'LineWidth',2);hold on h=legend ([ax], {h2(1) h2(2) h2(3) h2(4) h2(5)},'Location','northeastoutside');hold on title ('only showing last iteration value as legend') end ylabel('K'); xlabel('Density,n [m^{-3}]');
subplot(3,1,3) format short e color = 'rgkbc'; for i=1:length(ro) plot(N(:,1),K(:,:,i),'Color',color(i),'Linewidth', 2); hold on; entries(i) = { sprintf('ro = %i ',ro(i)) }; end legend('String',entries,'Location','northeastoutside');% Create legend using the 'entries' strings ylabel('K'); xlabel('Density,n [m^{-3}]'); title('only show one colour for different label');
0 个评论
采纳的回答
KSSV
2017-3-27
clear all;
close all;
clc;
%----Physical constant---------%
m = 9.1e-31; % electron mass [kg]
c= 3e8; % speed of light [m/s]
eps = 8.854e-12;% permittivity [F/m]
e = 1.6e-19; % charge [Coulomb]
h_bar = 6.626e-34 /2/pi; % reduced Planck constant_bar [m^2 kg/s]
%----Calculating K for different ro and different energy----%
E_beam = linspace(10e6,100e6,10); %Electron beam energy
gamma = E_beam./0.511e6;
gamma';
M=gamma'*ones(1,10)
n =linspace(1e24,1e25,10); %density
n';
N=n'*ones(1, 10)
P =sqrt( M.*(N/1e6))
ro = linspace(1e-6, 5e-6,5);
for i = 1:length (ro)
K(:,:,i)= 1.33E-10.*P.*(ro(i)/1e-6)
end
%----Plasma frequency----%
plasma_freq=sqrt((n(1,:)*(1.6e-19)^2)/(9.1e-31*8.85e-12));
lambda_plasma = 2*pi*c./plasma_freq; % plasma wavelength [m]
wavenum = 2*pi./lambda_plasma; % [m-1]
%----Betatron parameter----%
w_beta = plasma_freq./sqrt(2*gamma); % fundamental beta freq [s-1]
% lambda_beta = 2*pi*c/w_beta; % betatron period [m]
lambda_beta_1um = (2*pi*gamma(1,:)*ro(1,1))./K(1,:,1);
lambda_rad_1um = lambda_beta_1um./(2.*gamma(1,:).*gamma(1,:)); % fundamental wavelength
%----Plotting figures----%
figure (1)
subplot(3,1,1)
AX= plot(gamma(1,:),K(:,:,1),'r',gamma(1,:),K(:,:,2),'g',gamma(1,:),K(:,:,3),'k',gamma(1,:),K(:,:,4),'b',gamma(1,:),K(:,:,5),'c','LineWidth',2);
title ('legend is correct for each but cannot rename each of them');
ylabel('K');
xlabel('gamma');
ff = findobj('Color','r');
gg = findobj('Color','g');
hh = findobj('Color','k');
tt = findobj('Color','b');
yy = findobj('Color','c');
v = [ff(1) gg(1) hh(1) tt(1) yy(1)];
str = {'a' 'b' 'c' 'd' 'e'} ;
legend(v, str1,'Location','northeastoutside');
subplot(3,1,2)
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!