Legend Displays Wrong Colors when Plotting Multiple Functions R2015b
40 ビュー (過去 30 日間)
古いコメントを表示
When trying to display a legend corresponding to a scatter plot, the color of the markers do not correspond to the colors in the plot. If more than one function is plotted on the same axes, the legend will make all markers the color of the most recently plotted function. I have tested this on several computers running 2015b and the error persists. I've tested the exact same code on R2014b and everything works fine.
Sample Code:
x1 = 1:0.2:3;
y1 = 1:0.2:3;
x2 = 5:0.2:7;
y2 = 1:0.2:3;
scatter(x1,y1);
hold on;
scatter(x2,y2);
legend('red','blue');
Any help would be greatly appreciated.
1 件のコメント
Souradeep Sinha
2016 年 2 月 29 日
Hi Joseph,
Even though this is a known bug, here's how you can solve it manually.
1. Double click the wrong color legend item in 'Figure Properties' window. 2. Change the name of the item to something like 'Test1'. 3. Press Escape. 4. Color should change to what it should have been. 5. Double click and change the name of the legend item to what you originally wanted.
- Souradeep
採用された回答
Sudhanshu Bhatt
2015 年 10 月 26 日
Hi Joseph,
I understand that you are having issues working with legends on scatter plot in MATLAB R2015b.
This is a known issue and there is a bug report which describes the issue and has a work around:
A patch is provided which can be applied by following the instructions in the bug report.
If this still doesnot solve your issue, you can create a Technical Support Request by clicking the link below:
Thanks
Sudhanshu Bhatt
0 件のコメント
その他の回答 (2 件)
Jacob Kamminga
2016 年 6 月 28 日
編集済み: Walter Roberson
2016 年 6 月 28 日
FRom the bug report this was an easy fix:
3.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.
Gregory Pelletier
2023 年 9 月 27 日
Hi Joseph,
This works correctly (from https://www.mathworks.com/matlabcentral/answers/407745-plot-legend-not-correct):
x1 = 1:0.2:3;
y1 = 1:0.2:3;
x2 = 5:0.2:7;
y2 = 1:0.2:3;
h1 = scatter(x1,y1);
hold on;
h2 = scatter(x2,y2);
legend([h1(1),h2(1)],'red','blue');
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!