Plotyy x-axis overwritting with two plotyy graphs and legend issues

1 visualización (últimos 30 días)
I've got the plot that I need. However when I do hold on to do an overlay of the two plots, the x-axis gets re-replicated and I can't make a one-to-one comparison between the two graphs. I also have problems getting the legend to show, it gets overwritten each time. I'm trying to show all four legend items.
clear clf
A1 = 0.1:0.1:1.34; E1 = nan(size(A1)); for i=1:length(A1) E1(i) = 614; B1(i) = 100000/(A1(i))^2;
end
A2 = 1.34:0.1:3; E2 = nan(size(A1)); for i=1:length(A2) E2(i) = 823.8/A2(i); B2(i) = 100000/(A2(i))^2; end
A3 = [3:1:30]; E3 = nan(size(A1)); for i=1:length(A3) E3(i) = 823.8/A3(i); B3(i) = 100000/(A3(i))^2; end
A4 = [30:1:100]; E4 = nan(size(A1)); for i=1:length(A4) E4(i) =27.5; B4(i) = 9400000/(A4(i))^3.336;
end
A5 = [100:1:400]; E5 = nan(size(A1)); for i=1:length(A5) E5(i) = 27.5; B5(i) = 2; end
A6 = [400:1:2000]; E6 = nan(size(A1)); for i=1:length(A6) E6(i) = 0.000000001; B6(i) = A6(i)/200; end
A7 = [2000:1:100000]; E7 = nan(size(A1)); for i=1:length(A7) E7(i) = 0.000000001; B7(i) = 10; end
A8 = [100000:1:300000]; E8 = nan(size(A1)); for i=1:length(A8) E8(i) = 0.000000001; B8(i) = (90*(A8(i)/1000) - 7000)/200; end
%scrsz = get(0,'ScreenSize'); %figure('Position',[1 scrsz(4)/2 scrsz(3)/2 scrsz(4)/2])
[ax, h1, h2] = plotyy([A1 A2 A3 A4 A5 A6 A7 A8],[E1 E2 E3 E4 E5 E6 E7 E8],[A1 A2 A3 A4 A5 A6 A7 A8],[B1 B2 B3 B4 B5 B6 B7 B8],'loglog') set(get(ax(1),'Ylabel'),'String','Electric Field Strength[V/m]') set(get(ax(2),'Ylabel'),'String','Power Density [W/m^2]') grid on legend hleg = legend('Field Strength-05','Power Density-05'); xlabel('Frquency Range [MHz]');
hold on
T1 = 0.003:0.2:0.1; U1 = nan(size(T1)); for i=1:length(T1) U1(i) = 614; Y1(i) = 1000000; end
T2 = 0.1:0.2:1.34; U2 = nan(size(T1)); for i=1:length(T2) U2(i) = 614; Y2(i) = 10000/(T2(i))^2; end
T3 = [1.34:.1:3]; U3 = nan(size(T1)); for i=1:length(T3) U3(i) = 823.8/T3(i); Y3(i) = 10000/(T3(i))^2; end
T4 = [3:1:30]; Y4 = 10000./(T4).^2; U4 = nan(size(T1)); for i=1:length(T4) U4(i) = 823.8/T4(i); end
T5 = [30:1:100]; U5 = nan(size(T1)); for i=1:length(T5) U5(i) = 27.5; Y5(i) = 940000/(T5(i))^3.336; end
T6 = [100:1:300]; U6 = nan(size(T1)); for i=1:length(T6) Y6(i) = 0.2; U6(i) = 27.5; end
T7 = [300:1:15000]; U7 = nan(size(T1)); for i=1:length(T7) Y7(i) = T7(i)/1500; U7(i) = 0.00001; end
T8 = [15000:1:300000]; U8 = nan(size(T1)); for i=1:length(T8) Y8(i) = 10; U8(i) = 0.00001; end
[ax, h1, h2] = plotyy([T1 T2 T3 T4 T5 T6 T7 T8],[U1 U2 U3 U4 U5 U6 U7 U8],[T1 T2 T3 T4 T5 T6 T7 T8],[Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8],'loglog') grid on legend hleg = legend('Field Strength-99','Power Density-99') xlabel('Frquency Range [MHz]') set(h1,'LineStyle','--') set(h1,'color','red'); set(h2,'LineStyle',':') title('UnControlled Environment') grid on

Respuestas (1)

Grzegorz Knor
Grzegorz Knor el 31 de En. de 2012
Using these commands together is a little complicated. My proposition to solve this problem is as follows (I removed the unnecessary loops):
clear
clf
A1 = 0.1:0.1:1.34;
E1 = 614*ones(size(A1));
B1 = 100000./A1.^2;
A2 = 1.34:0.1:3;
E2 = 823.8./A2;
B2 = 100000./A2.^2;
A3 = 3:1:30;
E3 = 823.8./A3;
B3 = 100000./A3.^2;
A4 = 30:1:100;
E4 = 27.5*ones(size(A4));
B4 = 9400000./A4.^3.336;
A5 = 100:1:400;
E5 = 27.5*ones(size(A5));
B5 = 2*ones(size(A5));
A6 = 400:1:2000;
E6 = 0.000000001*ones(size(A6));
B6 = A6/200;
A7 = 2000:1:100000;
E7 = 0.000000001*ones(size(A7));
B7 = 10*ones(size(A7));
A8 = 100000:1:300000;
E8 = 0.000000001*ones(size(A8));
B8 = (90*(A8/1000) - 7000)/200;
A = [A1 A2 A3 A4 A5 A6 A7 A8];
E = [E1 E2 E3 E4 E5 E6 E7 E8];
B = [B1 B2 B3 B4 B5 B6 B7 B8];
T1 = 0.003:0.2:0.1;
U1 = 614*ones(size(T1));
Y1 = 1000000*ones(size(T1));
T2 = 0.1:0.2:1.34;
U2 = 614*ones(size(T2));
Y2 = 10000./T2.^2;
T3 = 1.34:.1:3;
U3 = 823.8./T3;
Y3 = 10000./T3.^2;
T4 = 3:1:30;
U4 = 823.8./T4;
Y4 = 10000./T4.^2;
T5 = 30:1:100;
U5 = 27.5*ones(size(T5));
Y5 = 940000./T5.^3.336;
T6 = 100:1:300;
U6 = 27.5*ones(size(T6));
Y6 = 0.2*ones(size(T6));
T7 = 300:1:15000;
U7 = 0.00001*ones(size(T7));
Y7 = T7/1500;
T8 = 15000:1:300000;
U8 = 0.00001*ones(size(T8));
Y8 = 10*ones(size(T8));
T = [T1 T2 T3 T4 T5 T6 T7 T8];
U = [U1 U2 U3 U4 U5 U6 U7 U8];
Y = [Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8];
ax = plotyy(A,E,A,B,'loglog');
set(get(ax(1),'Ylabel'),'String','Electric Field Strength[V/m]')
set(get(ax(2),'Ylabel'),'String','Power Density [W/m^2]')
grid on
axes(ax(1)) %#ok<MAXES>
hold on
plot(T,U,'r--')
axes(ax(2)) %#ok<MAXES>
hold on
plot(T,Y,':')
legend('a','b','c','d')
title('UnControlled Environment')

Categorías

Más información sobre Two y-axis en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by