Matlab 为多个轴放置一个图例

Matlab 为多个轴放置一个图例,matlab,plot,matlab-figure,legend,legend-properties,Matlab,Plot,Matlab Figure,Legend,Legend Properties,我使用三轴对象在x轴上缩放数据 我的问题是,我不知道如何为这三个情节找到一个好的传奇 我必须这样做,因为我的真实数据是以不同的采样率采样的 我为图表编辑了我的m文件,因为通常我从一些txt文件中读取数据 在本例中,我使用示例_数据1到3作为我的数据 在本例中,我将缩放示例_data1,使其看起来与示例_data2的频率相同 我进行“缩放”ax1.XLim=[0长度(x2)] 这就是为什么这个解决方案对我不起作用的原因: 它使用set(l3,'Parent',ax2)以某种方式破坏了我缩放数据的方

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

我的问题是,我不知道如何为这三个情节找到一个好的传奇

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

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

在本例中,我使用示例_数据1到3作为我的数据

在本例中,我将缩放示例_data1,使其看起来与示例_data2的频率相同

我进行“缩放”
ax1.XLim=[0长度(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

通过为每个调用存储,然后将这些调用传递给单个调用,可以获得更好的结果:

结果如下:


在最后(顶部)
轴上创建与前一个图相同颜色的虚拟图,然后仅显示最后一个
轴的图例。要创建具有所有正确属性的虚拟(不可见)绘图,只需使用
NaN
plot(NaN,'Color',thisDataColor,'DisplayName',thisDataName)
我正在搜索的内容!谢谢:)
...
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