Error when adding multiple legends to graph

13 ビュー (過去 30 日間)
birdnerd007
birdnerd007 2020 年 4 月 25 日
コメント済み: Star Strider 2020 年 4 月 25 日
Hello, I am trying to get a legend for the 10 tests I performed, but I am getting the error:
Error using legend (line 279)
Invalid argument. Type 'help legend' for more information.
This is my code:
xposition=(1:4:40);
yposition=zeros(length(xposition),1)+20;
pointsize = 15;
scatter(xposition, yposition, pointsize, HIT, 'filled')
axis([0,40, 0,40]);
grid on
colorbar
colormap("jet")
barnone=colorbar;
set(get(barnone,'Title'),'String','Hardness')
xlabel('X Axis Position (µm)')
ylabel('Y Axis Position (µm)')
q=[HIT(1);HIT(2);HIT(3);HIT(4);HIT(5);HIT(6);HIT(7);HIT(8);HIT(9);HIT(10)];
legend(q,'Test 1', 'Test 2', 'Test 3','Test 4', 'Test 5', 'Test 6', 'Test 7','Test 8', 'Test 9', 'Test 10');
What am I doing incorrectly? Thanks for your help!
  7 件のコメント
birdnerd007
birdnerd007 2020 年 4 月 25 日
dqb - That's exactly what I'm looking for; just one issue, only one blue dot appears as opposed to each of the colors corresponding to each test.
Walter - my apologies, I am still learning this stuff. I was hoping when I used the legend feature and calling my hardness, that it would allow me to name each test in the array. Again, I am probably not making sense because I still learning. Thanks for understanding.
birdnerd007
birdnerd007 2020 年 4 月 25 日
dqb - Do you know how I can get each color of dot to correspond with Test 1, Test, 2, etc? I appreciate your help.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2020 年 4 月 25 日
The only way to do what you want is to plot each ‘HIT’ value individually in a for loop:
hold on
for k = 1:numel(HIT)
q(k) = scatter(xposition(k), yposition(k), pointsize, HIT(k), 'filled');
end
hold off
then this works:
legend(q,'Test 1', 'Test 2', 'Test 3','Test 4', 'Test 5', 'Test 6', 'Test 7','Test 8', 'Test 9', 'Test 10', 'Loccation','N', 'NumColumns',2)
  2 件のコメント
birdnerd007
birdnerd007 2020 年 4 月 25 日
You've been absolutely amazing today! Thanks for all of your help. I'm learning so much. Thank you again.
Star Strider
Star Strider 2020 年 4 月 25 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by