How do I dynamically add markerfacecolors to gscatter plots?

5 views (last 30 days)
I've been asked to build an M-file to help automate some plotting activities which utilize the gscatter command. I've discovered that specific verbiage can in fact be added to the legend. What I'm attempting to do now is apply colors and markerfacecolors to the dots that appear in the gscatter plots.
So I went back to the MATLAB documentation and re-visted the carsmall example & data. Typing the following into MATLAB:
load carsmall;
gscatter(Weight,MPG,Model_Year,'','xos');
I got the same plot as shown here:
To get the applicable verbiage in the legend, I used the following commnads - with the applicable curly bracketts (thanks A Jenkins);
for idx=1:size(Model_Year,1)
switch Model_Year (idx)
case 70
LegCol{idx} = 'seventy';
case 76
LegCol{idx} = 'seventy six';
case 82
LegCol{idx} = 'eighty two';
end
end
Since the gscatter command doesn't allow for specific markerfacecolors, I issued the following commands;
h = gscatter(Weight, MPG, LegCol,'','o');
set(h(1), 'Color', 'b', 'MarkerFaceColor', 'b');
set(h(2), 'Color', 'g', 'MarkerFaceColor', 'g');
set(h(3), 'Color', 'r', 'MarkerFaceColor', 'r');
This resulted in the dots being filled with the expected colors.
Finally, I added a hypothetical case with the following:
load carsmall;
for idx=1:size(Model_Year,1)
switch Model_Year (idx)
case 70
LegCol{idx} = 'seventy';
case 76
LegCol{idx} = 'seventy six';
case 82
LegCol{idx} = 'eighty two';
case 90
LegCol{idx} = 'ninety';
end
end
h = gscatter(Weight, MPG, LegCol,'','o');
set(h(1), 'Color', 'b', 'MarkerFaceColor', 'b');
set(h(2), 'Color', 'g', 'MarkerFaceColor', 'g');
set(h(3), 'Color', 'r', 'MarkerFaceColor', 'r');
set(h(4), 'Color', 'k', 'MarkerFaceColor', 'k');
where case 90 is not included in the carsmall dataset, but the 4th set command tries to account for it. I incorrectly assumed that since there are no data points associated with case 90, it would simply be ignored. Instead, I got the following error: ??? Index exceeds matrix dimensions
Since I could be facing the possibility of hand editing several dozen gscatter plots (after saving them as figures), I'm looking for any way to automate this process, including case 90.
Is it even feasible to dynamically add markerfacecolors to gscatter plots?

Accepted Answer

Brad
Brad on 17 Dec 2013
Thanks nkjt.
The gscatter function does contain the ability to add colors (it's in the part you have as '' before the marker type). 'MarkerFaceColor' has to be adjusted separately; you can define a list of colors initially and loop over them. Note that the color and marker lists don't have to be the same size as the number of groups, so this will work for anywhere between one and eight groups:
colors = 'rgbcmykw'
h = gscatter(Weight, MPG, Model_Year,colors,'o');
for n = 1:length(h)
set(h(n), 'MarkerFaceColor', colors(n));
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!