Error when adding multiple legends to graph
Afficher commentaires plus anciens
Hello, I am trying to get a legend for the 10 tests I performed, but I am getting the error:
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 commentaires
Walter Roberson
le 25 Avr 2020
What is HIT? In context it would have to be graphic handles to work
birdnerd007
le 25 Avr 2020
birdnerd007
le 25 Avr 2020
Walter Roberson
le 25 Avr 2020
What is it you expect legend() to do when you pass hardness values as the first parameter ?
dpb
le 25 Avr 2020
"HIT is a 10x1 array for one of my variables."
Well as Walter points out, "that's just wrong!" The first argument to legend if not just text strings is and must be either subset or target, both of which are (and must be) graphics objects--the former a set of data series objects like line handles of those wanted on the legend if not all or an axes object of one kind or another for the latter.
See the documentation for the details.
Just taking q (you don't need it at all, removing that line would be good, too) out of the argument list should work; it's less typing to write something like
hLg=legend(num2str([1:numel(HIT)].','Test %d');
birdnerd007
le 25 Avr 2020
birdnerd007
le 25 Avr 2020
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Legend dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!