Figure legend is being cut off MATLAB

I'm trying to plot 81 variables into one plot in MATLAB and I need a legend with 81 respective labels. I've managed to separate the legend into several lines such that it would fit the figure better but still, being this large, the legend apears cut off: example is there any way to solve this problem, so that all the legend appears in the figure?

Thank you in advance

Upvotes: 3

Views: 1568

Answers (1)

Steve
Steve

Reputation: 4097

In my experience, the legend is not manageable once the number of plots becomes so large. You could try and hack this out by hand by trying to make a compact legend my manually moving the legend lines and text boxes around individually--but I fear this way will ultimately make you unhappy. If you want, you could start playing with the legend pieces by writing something like:

hl = legend(uniqueHandles, myNames);
hlc = get(hl, 'children');

This may not be possible and MATLAB will resist you. Instead, consider that a legend with 81 different colors becomes very hard to manage and you can't really tell the colors apart anyway. Instead of trying to get them all to fit I recommend grouping them into different colors and styles and then only showing 3-7 different sets like this:

Example

This could be further improved by customizing the data tip so that when you click on a line it gives you more detail.

I put together some basic code to show how you might do this. It has some shortcomings. Regardless, in this example I plot everything at once then go back and group the colors together. I then pick out a representative line from each grouping and use it as an example in the legend. You could also plot everything, color it correctly, and then plot a new line in the existing axis with a value nan and use THOSE in the legend. (edit) I also noted that you have grouped the colors already for some of them, so this should flow naturally from the work you've already done. If nothing else, you could grab the handles from every third entry and then only have 27 items in your legend... but best if you can reduce it further.

The example to create the above image is as follows:

%% Setup the problem:
nd  = 81;
nx = 256;
data = zeros(nx,nd);
t = linspace(0, 2*pi, nx);

%% We divide the data into 3 groups and set a color for each. This calculation then notes what group each line belongs to:
myCategory = ceil((1:nd)/27);
myColor = myColors(myCategory)';

myColors = ['r', 'b', 'g'];
myNames = {'1 to 27', '28 to 54', '55 to 81'};


%% Make a simple set of data:
randn('seed', 1982);
dt = 0;
for ind = 1:nd
    dt = dt+randn/5;
    data(:, ind) = sin(t+dt) + dt*cos(t);
end


%% Plot all at once: 
figure(33);
clf;
h = plot(data);

%% find the unique entries and choose one representative line for the legend.
%% We then use the handles of these lines to create the legend:
lastColor = -1;
uniqueHandles = [];

for ind = 1:nd
    set(h(ind), 'color', myColor(ind));
    if  lastColor ~= myColor(ind)        
        lastColor = myColor(ind);
        uniqueHandles(end+1) = h(ind);
    end
end

% make the legend:
legend(uniqueHandles, myNames);

Upvotes: 5

Related Questions