How to make a matlab legend recognize multiple scatter plots?
Asked Answered
L

4

2

I want to place three scatter plots in the same figure window and have a legend that describes them. The scatter plots all load in the same window just fine, but the legend only recognizes the last series. In other words, the legend shows a red marker (the color for the last series) for each of its entries.

How do I make the legend recognize each scatter and not just the last one? I've tried a bunch of different things, and none of them seem to work. Thanks!

The picture is the plot for one of my datasets, note the legend.

s10 = scatter3(x1, y1, z1, 'b'); hold on;
s1 = scatter3(x2, y2, z2, 'g'); hold on;
s01 = scatter3(x3, y3, z3, 'r'); hold on;
legend([s10,s1,s01], {'10ms', '1ms', '0.1ms'}) 
% Every legend entry is red (pertains to the last series)

example of the problem

Limulus answered 10/10, 2015 at 23:10 Comment(0)
B
0

For me this seems to be associated with the recent version of Matlab (R2015b); in this version I get the same problem as you with the legend entries showing only one color (in contrast to the other answers that can't reproduce the problem). But if I roll back to a previous version (R2010b), the problem goes away. I'm not sure if you have that option, but it might help diagnose the precise issue.

Bettyebettzel answered 11/10, 2015 at 20:57 Comment(2)
I have R2015b. I don't get the OP's issue.Anthocyanin
Using a different version fixed it. I originally was using R2015b (which has been, in general, buggier than usual).Limulus
C
2

I can't reproduce the problem. Using your code above with random data seems to work (I did fix a typo, you need a comma after the first argument to legend):

x1 = rand(10, 1); y1 = rand(10, 1); z1 = rand(10, 1);
x2 = rand(10, 1); y2 = rand(10, 1); z2 = rand(10, 1);
x3 = rand(10, 1); y3 = rand(10, 1); z3 = rand(10, 1);
s10 = scatter3(x1, y1, z1, 'b'); hold on;
s1 = scatter3(x2, y2, z2, 'g'); hold on;
s01 = scatter3(x3, y3, z3, 'r'); hold on;
legend([s10,s1,s01], {'Series 10', 'Series 1', 'Series 01'})

enter image description here

Charlottecharlottenburg answered 10/10, 2015 at 23:35 Comment(3)
Thanks for looking at it. I copied in your code, and I get the same problem still. Every legend entry is still red. Oh well. I can work around it.Limulus
All red for me as well. I am using R2015b.Flywheel
Strange, it works for me in R2014a and R2016a but I get the all-red-legend result in R2015b.Charlottecharlottenburg
A
0

If the previous answer doesn't work, you can also exploit dynamic legends. This answer is what is inspiring this post: Dynamic Legend (Updates in every recursion). This is a rather undocumented feature but it does work very well. Basically, after each plot, you are able to dynamically update what the legend looks like without having to make one call to legend that has all of them together.

As such, try something like this. I'll borrow some of the previous answer's code to get me started:

x1 = rand(10, 1); y1 = rand(10, 1); z1 = rand(10, 1);
x2 = rand(10, 1); y2 = rand(10, 1); z2 = rand(10, 1);
x3 = rand(10, 1); y3 = rand(10, 1); z3 = rand(10, 1);

scatter3(x1, y1, z1, 'b', 'DisplayName', '10ms'); hold on;
legend('-DynamicLegend');
scatter3(x2, y2, z2, 'g', 'DisplayName', '1ms'); hold on;
legend('-DynamicLegend');
scatter3(x3, y3, z3, 'r','DisplayName', '0.1ms'); hold on;
legend('-DynamicLegend');

Call scatter3, then make sure that you use the 'DisplayName' flag and place what you would normally put in the appropriate legend spot. After each call to scatter3 after, you use the legend('-DynamicLegend'); command to signal to MATLAB that the legend entries will be forthcoming... you're going to specify them in the 'DisplayName' flag.

When you do that, this is the figure I get:

enter image description here

As a minor note, I can't reproduce your plot either. I get the same plot as the previous answer.

Anthocyanin answered 11/10, 2015 at 2:3 Comment(4)
You proposed solution does not work for R2015b either.Gonadotropin
Yes it does. I own R2015b.Anthocyanin
I use the following version: 8.6.0.267246Gonadotropin
@Anthocyanin - I get the all-red result in R2015b as well but not in R2014a and R2016a.Charlottecharlottenburg
B
0

For me this seems to be associated with the recent version of Matlab (R2015b); in this version I get the same problem as you with the legend entries showing only one color (in contrast to the other answers that can't reproduce the problem). But if I roll back to a previous version (R2010b), the problem goes away. I'm not sure if you have that option, but it might help diagnose the precise issue.

Bettyebettzel answered 11/10, 2015 at 20:57 Comment(2)
I have R2015b. I don't get the OP's issue.Anthocyanin
Using a different version fixed it. I originally was using R2015b (which has been, in general, buggier than usual).Limulus
F
0

This issue is caused by a Matlab bug affecting version R2015b. It was fixed in R2016a. There is a bugreport here, which contains a patch and 3 alternative workarounds.

Here are the workarounds in case the link goes dead:


If you are unable to install the patch, there are three alternative workarounds:

  1. If the CData of each scatter plot is an RGB triplet, then assign the MarkerEdgeColor or MarkerFaceColor of each scatter plot to the value of the CData:

    s1 = scatter(1:10,1:10);
    hold on
    s2 = scatter(2:11,1:10);
    s1.MarkerEdgeColor = s1.CData;
    s2.MarkerEdgeColor = s2.CData;
    legend('show');
    
  2. Assign an RGB triplet to the MarkerEdgeColor or MarkerFaceColor of each scatter plot:

    s1 = scatter(1:10,1:10);
    hold on
    s2 = scatter(2:11,1:10);
    s1.MarkerEdgeColor = [0 0.4470 0.7410];
    s2.MarkerEdgeColor = [0.8500 0.3250 0.0980];
    legend('show');
    

Use this workaround when all the points within each scatter plot are the same color.

  1. Call the legend function with two or more output arguments:

    s1 = scatter(1:10,1:10,[],1:10);
    hold on
    s2 = scatter(2:11,1:10,[],26:35);
    [h, ~] = legend('show');
    

Use this workaround when the points within each scatter plot are different colors.

Flywheel answered 12/10, 2016 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.