Search code examples
matlabplotmatlab-figurelegendlegend-properties

Putting one legend for many axes


I'm using three axes-Objects to scale my data on the x-axis.

My problem is that i do not know how to get a nice legend for the three plots.

I have to do this cause my real data is sampled with different sample rates.

I edited my m-file for the diagram slightly cause normally I'm reading the data out of some txt files.

In this example i used example_data 1 to 3 for my data.

In this example I'm scaling the example_data1 so it looks like the same frequency as example_data2.

I do the 'scaling' ax1.XLim = [0 length(x2)].

That's why this solution doesn't work for me: Plot with multiple axes but only one legend.

It uses set(l3,'Parent',ax2); which somehow ruins my approache to scale my data. The scaling is the only solution to my problem cause i don't know the exact relation between the two sampling rates.

my code:

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')

This results in a very bad looking legend:

bad looking legend

I really hope somebody can help me.

Thank very much.


Solution

  • You can get better results by storing handles for each of your plot lines, then passing those to a single legend call:

    ...
    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
    

    And here's the result:

    enter image description here