Scatter Plot with different "markers" and "data labels"

151 views (last 30 days)
a=[32.0 30.0 29.0 42.0 61.0 81.0]
b=[0.9 0.8 0.2 0.4 0.3 0.10]
c={'US'; 'GM'; 'SA'; 'IN'; 'EG'; 'BZ'}
scatter(a,b)
xlim([0 50])
ylim([0 1])
refline
grid
I need a "scatter plot" with different "markers" and "data labels" (array in c). Also the refline should always start from (zero,zero)
  2 Comments
Adam
Adam on 18 Feb 2020
doc refline
seems to have various options that take arguments.
Charles Hendricks
Charles Hendricks on 23 Nov 2022
() Determine the cubic spline equations for the data set below:  () Plot f(x) as scatter plot, then add splines for each segment. a. (2pts) X and Y axes must be labeled properly x 0 100 200 400 600 800 1000 f(x) 0 0.653 1.0000 0.841 0.376 0.18813 0.08

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 18 Feb 2020
"I need a scatter plot with different markers"
The scatter() function only allows one marker definition so the data are plotted within a loop that iterates through a list of markers. The list of markers is replicated so that you never run out of markers in case the dataset grows, though that would result in duplicate markers if the number of points exceeds 13. Note that you could also change the color of the markers within the loop.
"...and data labels"
The data can be labeled either by a legend or by labeling the actual data points. The first block of code below shows how to use a legend to label the points. The secon block of code shows how to label the points on the plot.
"the refline should always start from (zero,zero)"
fitlm() is used to compute the slope of the regression line with an intercept of 0. refline() is used to draw the reference line.
See inline comments for details.
% Produce the data
a=[32.0 30.0 29.0 42.0 61.0 81.0];
b=[0.9 0.8 0.2 0.4 0.3 0.10];
c={'US'; 'GM'; 'SA'; 'IN'; 'EG'; 'BZ'};
% Define a sequence of symbols
syms = {'o' 's' '+' '*' 'x' 'd' '.' '^' 'v' '<' '>' 'p' 'h'};
% Replicate symbols if number of data points is larger than number of syms
syms = repmat(syms, 1, ceil(numel(a)/numel(syms)));
% Plot the data within a loop
hold on % important
h = gobjects(numel(a),1);
for i = 1:numel(a)
% Assign the label to the legend string using DisplayName
h(i) = scatter(a(i),b(i),36,[0 0 1],syms{i},'DisplayName',c{i});
end
% Set axis limits
xlim([min(0, min(a)), max(a)])
ylim([min(0, min(a)), max(b)])
% Do the regression with an intercept of 0 and plot the line
linFit = fitlm(a,b,'Intercept',false);
refline(linFit.Coefficients.Estimate, 0);
% Add grid and legend
grid
legend(h)
Alternatively, you could lable the points directly on the plot using the labelpoints() function from the file exchange. Just add this line below to the end of the block of code from above (after downloading the labelpoints function).
% Label will be "North" of the datapoint with 0.1 spacing
labelpoints(a,b,c,'N',0.1)
  4 Comments
Muhammad Rahil Rafiq
Muhammad Rahil Rafiq on 18 Feb 2020
what if we plot the same data on "quarter polar plot"
Adam Danz
Adam Danz on 18 Feb 2020
Is there something you're having trouble with adapting this to polar plots?
If so, share the code and describe what the problem is. I'd be glad to help.

Sign in to comment.

More Answers (1)

dpb
dpb on 18 Feb 2020
Edited: dpb on 18 Feb 2020
Adam posted first but will add slightly different way to get to same place (sans some of the finishing niceties...)
Why scatter is so braindead re: markers by point is a wonder... :(
But, you can use plot instead; end up at similar thing in that multiple scatter calls also generate a handle for each call as do multiple lines in plot
N=nan(size(a)); % dummy placeholder NaN array (see why later...)
hL=plot([N;a],[N;b]); % create the plot w/ data, no markers, yet...
mkr={'o','+','*','x','s','d'}; % some markers...
set(hL,{'marker'},mkr') % now put the markers on...
m=a.'\b.'; % compute LS slope no intercept model via backslash operator
refline(m,0)
ylim([0 1])
grid
results in same figure as above w/o the labels yet.
The nan() array is needed to force plot to treat the two row vectors as separate columns by actually passing an array instead of just vectors--plot() will force a vector of either row or column to be treated as a single line instead of making a separate line by column as we need here to add a marker to each. NaN values are silently ignored so they don't show up as data; they're just placeholders to make plot do what is wanted.
  5 Comments
dpb
dpb on 18 Feb 2020
Edited: dpb on 19 Feb 2020
Ah! Knowing what had written, I had failed to notice the possibility of misgrouping to have the perception of a char() string...
While not often for most, probably, the difference between " . '" and " .' " is profound if one has any complex variables around. Since much of my past consulting work dealt in the complex domain routinely, I soon learned to invariably use the dot version unless I specifically wanted/needed the other. Accidentally changing to the complex transpose vis a vis not if one is simply rearranging a variable's storage orientation is an insidious and often very difficult bug to find.
Adam Danz
Adam Danz on 19 Feb 2020
I also use .' by default unless I'm transposing a cell array or some non-numeric datatype. I believe it was about a year ago I learned the importance of that. Still, when I see a.'\b.' my brain wants to see the char interpretation first. It's like the blue/gold dress.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!