Search code examples
matlabmatlab-figurelegend

Display surplus legend entries for gscatter plot


how can i display surplus legend entries, that don't have a corresponding group in a gscatter plot?

I.E. display the "Six" in following code:

len = 100;
x = zeros(len, 1);
groups = randi(5, len, 1);
gscatter(1:len, x, groups);
legend('One', 'Two', 'Three', 'Four', 'Five', 'Six');

Solution

  • Here is a trick: You can add an additional point to a place far away from your data, and then force the plot to be centred where you want with the function axis.

    len = 100;  
    x = zeros(len, 1);
    groups = randi(5, len, 1);
    %gscatter(1:len, x, groups);
    gscatter([1:len 100], [x ; 100], [groups ; 6]);  % Add a group 6 point far away at (100,100)
    legend('One', 'Two', 'Three', 'Four', 'Five', 'Six');
    axis([-5 105 -1 1]); % Close up the window to your original data making group 6 invisible
    

    gscatter