为多个轴放置一个图例

Putting one legend for many axes

我正在使用三轴对象在 x 轴上缩放我的数据。

我的问题是我不知道如何为这三个地块取一个漂亮的图例。

我必须这样做,因为我的真实数据是以不同的采样率采样的。

我稍微编辑了图表的 m 文件,因为通常我是从一些 txt 文件中读取数据。

在这个例子中,我使用 example_data 1 到 3 作为我的数据。

在这个例子中,我正在缩放 example_data1,因此它看起来与 example_data2 的频率相同。

我做 'scaling' ax1.XLim = [0 length(x2)]

这就是此解决方案对我不起作用的原因:

它使用 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')

这导致图例看起来很糟糕:

我真的希望有人能帮助我。

非常感谢。

只需使用真实时间戳作为 x 值:

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

您可以通过存储 handles for each of your plot lines, then passing those to a single 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

结果如下: