2017-10-03 12 views
2

저는 X 축에서 데이터를 축척하기 위해 세 개의 축 (객체)을 사용하고 있습니다.여러 축에 하나의 범례 넣기

제 문제는 제가 3 개의 플롯에 대한 멋진 전설을 얻는 방법을 모른다는 것입니다.

실제 데이터가 다른 샘플 속도로 샘플링되기 때문에이 작업을 수행해야합니다.

나는 약간 mt 파일을 편집하여 약간의 txt 파일에서 데이터를 읽는다.

이 예제에서는 데이터에 example_data 1-3을 사용했습니다.

이 예제에서는 example_data1이 example_data2와 같은 빈도로 보이도록 크기를 조정합니다.

나는 '스케일링'을 수행합니다. ax1.XLim = [0 length(x2)].

그 이유는이 솔루션이 나를 위해 작동하지 않습니다 : Plot with multiple axes but only one legend.

set(l3,'Parent',ax2);을 사용하여 데이터를 확장하는 방법을 폐허로 사용합니다. 스케일링은 두 가지 샘플링 속도 사이의 정확한 관계를 알지 못하기 때문에 제 문제에 대한 유일한 해결책입니다.

내 코드 :

example_data1  = repmat(1:100,1,10); 
example_data2  = 2 * repmat(1:0.5:100.5,1,5); 
example_data3  = [1:500 500:-1:1]; 

whole_length_data1 = length(example_data1); 
% 1. step 
    start_of_data = 1; 
    end_of_data = 1000; 
    % data2 
    y2 = example_data2(start_of_data:end_of_data); 
    x2 = 0:length(y2)-1; 
    % data3 
    y3 = example_data3(start_of_data:end_of_data); 
    x3 = 0:length(y3)-1; 
    % data1 
    y1 = example_data1(1:length(example_data1)); 
    x1 = 0:length(y1)-1; 

% 2. step 
    start_one = 1; 
    y1  = example_data1(start_one:length(example_data1)); 
    x1  = 0:length(y1)-1; 

% 3.step 
    end_one = whole_length_data1 - 500; 
    y1  = example_data1(start_one:end_one); 
    x1  = 0:length(y1)-1; 


Farbe1 = [0,1,0]*0.6; % Dunkelgrün 
Farbe2 = [1,0,0]*0.8; % Dunkelrot 
Farbe3 = get(groot,'DefaultAxesColorOrder') % default values 
Farbe3 = Farbe3(1,:);      % 1. Zeile der defaultvalues 
figure(1) 
    % 3 axes 

    clf 
     %------------------------------------------------------------------ 
     %-------------------------- plot1: --------------------------- 
     %------------------------------------------------------------------ 
     plot(x2,y2,'green','LineWidth',2,'Color',Farbe1,... 
      'DisplayName','name of the first plot') 
     ax1   = gca; 
     ax1.XLim  = [0 length(x2)] 
     ax1.YLim  = [min(y2) max(y2)] 
     ax1.YTick = [0:25:300] 
     ax1.FontSize = 12; 
     legend('show') 

     %---------------------------------------------------------------- 
     %-------------------------- plot2: -------------------------- 
     %---------------------------------------------------------------- 
     ax2    = axes('Position',ax1.Position); 
     plot(x3,y3,'blue','LineWidth',2,'Color',Farbe3,... 
      'DisplayName','plot2')  
     ax2.Color   = 'none'; 
     ax2.XTick   = []; 
     ax2.XLim   = [0 length(x3)]; 
     ax2.YAxisLocation = 'right'; 
     ax2.FontSize  = 12; 
     legend('show') 

     %---------------------------------------------------------------- 
     %-------------------------- plot3: ------------------------- 
     %---------------------------------------------------------------- 
     ax3    = axes('Position',ax1.Position); 
     plot(x1,y1,'red','LineWidth',2,'Color',Farbe2,... 
      'DisplayName','3') 
     ax3.XTick   = []; 
     ax3.YTick   = []; 
     ax3.Color   = 'none'; 
     ax3.XAxisLocation = 'top'; 
     ax3.YAxisLocation = 'right'; 
     ax3.XLim   = [0 length(x1)]; 
     ax3.YLim   = [min(y1) max(y1)*2]; 
     legend('show') 

이것은 아주 나쁜 찾고 전설 결과 :

bad looking legend

난 정말 누군가가 나를 도울 수 있기를 바랍니다.

대단히 감사합니다.

+0

마지막 플롯과 동일한 색상으로 마지막 (상단)'축 '에 더미 플롯을 만든 다음 마지막'축 '의 범례 만 표시합니다. 올바른 속성을 가진 더미 (보이지 않는) 플롯을 만들려면'NaN'을 사용해야합니다 : plot (NaN, 'Color', thisDataColor, 'DisplayName', thisDataName)' – Hoki

답변

1

, 다음 하나의 legend 호출에 그 통과 :

... 
h1 = plot(x2,y2,'green','LineWidth',2,'Color',Farbe1,... 
      'DisplayName','name of the first plot'); 
... 
h2 = plot(x3,y3,'blue','LineWidth',2,'Color',Farbe3,... 
      'DisplayName','plot2'); 
... 
h3 = plot(x1,y1,'red','LineWidth',2,'Color',Farbe2,... 
      'DisplayName','3'); 
... 
hl = legend(ax3, [h1 h2 h3]); % Place legend in top-most axes 

을 결과는 다음과 같습니다.

enter image description here

+0

내가 무엇을 찾고 있었습니까! 고맙습니다 :) – himynameis

0

그냥 x 값으로 실제 타임 스탬프를 사용 : 당신은 당신의 plot lineshandles를 저장하여 더 나은 결과를 얻을 수 있습니다

fig = figure; 
plot(x1/length(y1)*end_of_data, y1, 'LineWidth',2, 'Color',Farbe1, 'DisplayName','First plot') 
hold on 
plot(x2/length(y2)*end_of_data, y2, 'LineWidth',2, 'Color',Farbe2, 'DisplayName','Second plot') 
plot(x3/length(y3)*end_of_data, y3, 'LineWidth',2, 'Color',Farbe3, 'DisplayName','Third plot') 
legend 
+0

고맙습니다. 좋은 생각! – himynameis