How to make a matlab legend recognize multiple scatter plots?

痞子三分冷 提交于 2019-12-12 18:28:19

问题


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)


回答1:


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.




回答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'})




回答3:


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:

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




回答4:


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.



来源:https://stackoverflow.com/questions/33059911/how-to-make-a-matlab-legend-recognize-multiple-scatter-plots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!