32 views (last 30 days)
Show older comments
Azra Zainurin on 18 Apr 2016
-
-
Link
Direct link to this question
https://matlabcentral.mathworks.com/matlabcentral/answers/279598-how-to-edit-my-scatter-plot-legend
Commented: dpb on 23 Apr 2016
Accepted Answer: dpb
- Screen Shot 2016-04-18 at 20.44.45.png
Hi,
I have a problem putting legend on my scatter plot.
I have used legend('Intact','Damage 1','Damage 2','Damage 3');
However, I want 'b*' to be my Intact, 'go' to be my Damage 1, 'k^' to be my Damage 2, and lastly 'rs' to be my Damage 3.
How do I change this?
Please see attached to have better understanding.
I hope I make sense. I really need help with this one
THANK YOU IN ADVANCE!!
Regards, Azra
2 Comments Show NoneHide None
Show NoneHide None
Kuifeng on 18 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/279598-how-to-edit-my-scatter-plot-legend#comment_359691
for example, b*, your intact got 21 points, 1:5 is a 5 element vector. how would you like to plot 21 y values with 5 x values?
%similarly. other three groups
dpb on 23 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/279598-how-to-edit-my-scatter-plot-legend#comment_361058
From doc for plot "If one of Yn or Xn is a matrix and the other is a vector, it [i.e., PLOT] plots the vector versus the matrix row or column with a matching dimension to the vector." OP has his Y data in an array which in Matlab must be rectangular so there cannot be 21 points in the first array but an (indeterminate) number that is a multiple of 5 (the length of X vector). Since as you point out, there are 21 distinct points that N must be a minimum of 5; there could be more. It appears the data were collected with only two decimal digits of precision so either() there are NaN placeholders for missing values which *plot ignores silently or the other values are duplicated to other points in which case they're indistinguishable being overlaid identically.
Hence, there are N line objects for each array; these will be numbered sequentially and as noted above legend will associated text labels on a one-to-one basis with the first M lines if specific line handles are not provided for a differing association.
(*) Discounting the possibility there are other values outside the given plot limits that are thereby clipped from view...
Sign in to comment.
Sign in to answer this question.
Accepted Answer
dpb on 18 Apr 2016
Edited: dpb on 19 Apr 2016
Open in MATLAB Online
Need to save the line handles from plot and select one from each group. Each group (column) of points in the plot is a separate line for each group of Y values (one for each X in that dimension of the array) and by default legend begins with the first line and uses the labels given in sequence. Hence, internally the labels as shown in your plot are associated with the first four lines (which are all in the first group/array), not the groups even though there are only four Y arrays. When you don't draw a line between points it's easy to forget the orientation of the lines as plot defines them; to see it, set the line style property on a couple of them and see how the points are then connected.
Since you didn't provide the code in an editable way I'll use an artificial demo instead of trying to type yours in by hand...
>> hL=plot(1:3,rand(4,3),'b*',4:6,rand(4,3),'g^'); % two groups of four, similarly; line save handles
>> xlim([0 7]) % clean up limits for viewing
>> legend(hL(1:4:end),'A','B')
>>
Select the first line of the two groups of lines; the 4 is number of rows in each group. In the example above I used 3x4 whereas your figure uses 5xN instead; if that were to vary would have to make the selection appropriately for each X grouping.
plot associates the size of the Y array with the appropriate length of the X vector so there are four lines in my case each of length 3.
As noted above, try
set(hL(1),'linestyle','-')
to see a particular line and which will make why the above on the legends works to get the desired correlation of line marker to the label works easier to see.
2 Comments Show NoneHide None
Show NoneHide None
Azra Zainurin on 20 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/279598-how-to-edit-my-scatter-plot-legend#comment_360315
Oh my it works!
Thank you very much for your help.
I apologise for my late reply
Regards, Azra
dpb on 20 Apr 2016
Direct link to this comment
https://matlabcentral.mathworks.com/matlabcentral/answers/279598-how-to-edit-my-scatter-plot-legend#comment_360359
No problem, glad to be able to help... :) Thanks for letting know did actually help; makes it much more pleasant to know the effort isn't wasted.
Sign in to comment.
More Answers (1)
Kuifeng on 18 Apr 2016
Open in MATLAB Online
plot(linspace(1,5,21), Intact..., 'b*',linspace(6,10,21), D1b..., 'go',...
linspace(11,15,21), D2b..., 'k^',linspace(16,20,21), D3b..., 'rs'). %note, may not be 21 elements.
legend...
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend
Find more on Legend in Help Center and File Exchange
Tags
- legend
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office