How to Colour regions in a gscatter?

The attached graph shows 3 different groups. I want to shade the groups as well. all of them are separated from each other so it my be linear shading. Please assist.

6 Comments

Hi Bhavick,
In the figure it looks like grouping of coordinates have been done on the basis of a particular range the y coordinatse lie in, could you confirm if that's the case?
It might help if you provide the data. The groups are already in different colours, so I'm wondering what you mean by "shade the groups". Do you mean provide different background shading in the axis for the regions containing the groups?
Bhavick Singh
Bhavick Singh on 16 Jun 2021
Edited: Bhavick Singh on 16 Jun 2021
Hi Scott, this is what I mean. My groups are separated through colour. Just wanted to shade behind it. This example is from Matlab onramp. This is what i want to do for my graph given in the question.
Hmm, very interesting. I don't think this can be done with gscatter. Is there code for this example?
Hi Scott, the example is from Matlab Onramp.
@Shelly Choudhary yes they have been grouped based on where they lie on the y axis.

Sign in to comment.

Answers (1)

This is actually from the Machine Learning Onramp, not MATLAB Onramp. See Chapter 2.5. The one you show is specifically in the background popup of Task 2.
Those plots are created following the steps outlined on the Visualize Decision Surfaces of Different Classifiers.
Here's an example based on that doc page:
load fisheriris
X = meas(:,1:2);
y = categorical(species);
labels = categories(y);
% Visualize the data using a scatter plot. Group the variables by iris species.
gscatter(X(:,1),X(:,2),species,'rgb','osd');
xlabel('Sepal length');
ylabel('Sepal width');
% Train a naive Bayes model.
classifier = fitcnb(X,y);
% Create a grid of points spanning the entire space within some bounds of the actual data values.
x1range = min(X(:,1)):.01:max(X(:,1));
x2range = min(X(:,2)):.01:max(X(:,2));
[xx1, xx2] = meshgrid(x1range,x2range);
XGrid = [xx1(:) xx2(:)];
% Predict the iris species of each observation in XGrid using all classifiers. Plot the a scatter plot of the results.
predictedspecies = predict(classifier,XGrid);
% Visualize the decision surface
gscatter(xx1(:), xx2(:), predictedspecies,[255 191 191; 191 255 191; 191 191 255]./255);
hold on
gscatter(X(:,1),X(:,2),species,'rgb','.');
hold off
title('Naive Bayes')
legend off, axis tight
legend(labels,'Location',[0.35,0.01,0.35,0.05],'Orientation','Horizontal')

Categories

Products

Release

R2021a

Asked:

on 15 Jun 2021

Edited:

on 16 Jun 2021

Community Treasure Hunt

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

Start Hunting!