Data points in regression with different colors and shapes

21 visualizzazioni (ultimi 30 giorni)
Hi everyone,
I would like to know whether there is a way to make the data points have different colors and shapes?
Each data point has 2 attributes. So for example all the points with attribute number 1: equal to "x" I would color them all with red and all points with attribute number 2: equal to "y" I would give them a different shape (square, circle, star, etc.).
Thanks!
Extra details, for the above plot:
I have the following matrix M, rows give the first "attribute" while colomuns give the second "attribute". Then I colapse them into one vector using M=M(:) of size (1 x n )
Then I use these attributes (each correspond to the number of hubs and the number of nodes a newly node is added to of a scale free network, the values in the matrix give the "alpha" parameter of the power law associated to the degree distribution)
I feed the scale free network with both attributes to a function that returns the value "Y" (vector of size 1 x n) on the y-axis.
Then given M and Y, I do the following:
mdl=fitlm(M,Y)
plot(mdl)
  8 Commenti
dpb
dpb il 22 Ott 2020
Yes, even though all of one row or column may have one attribute in common, the combination is different and so each needs its own handle or the attribute must accept an array of values.
As noted above plot has only one value for both the marker type and color so would have to set each point separately to use it.
scatter allows an array of color triplets so could use it to do all elements in a column with the color for each row and one marker which cuts down the amount of code required by the number of rows.
Why TMW didn't allow the properties to be set within the line object is a real shortcoming; the half-fixed it w/ scatter but only half; the markers can't be set by point. The fact there is such a limited number of markers is annoying, too.
Sha
Sha il 23 Ott 2020
thank you all very much! I ploted the data seperatley and used a colormap, the answer below seems more eficient than what I have done.

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 22 Ott 2020
Modificato: Adam Danz il 23 Ott 2020
Since you collapsed M into a column vector using M(:) and Y apparently has the same size and shape, then your linear model has 1 predictor and 1 response variable which is why plot(mdl) is returning a simple scatter plot instead of an added-value-plot.
Here's an example using a dataset provided by Matlab where W is a 5x6 matrix of car weight and MPG is a 5x6 matrix of fuel efficiency. You can imagine that each row and each column is some attribute such as manufacturer and year.
load carsmall
W = reshape(Weight(1:30), 5, 6);
MPG = reshape(MPG(1:30), 5, 6);
Now we'll collapse them vertically into column vectors,
W = W(:);
MPG = MPG(:);
and run them through a linear model using weight (W) as a predictor of fuel efficiency (MPG)
mdl = fitlm(W,MPG);
And plot the results
h = plot(mdl);
Note that this produces 4 line objects: Two dotted lines for the confidence bounds, one solid line for the predictor, and one line-object containing just the markers showing the raw data.
get(gca, 'Children')
ans =
4×1 Line array: Line Line (Confidence bounds) Line (Fit) Line (Data)
In fact, we could plot the raw data directly on top of the simple scatter plot (see caveat at the end of this answer),
figure()
plot(mdl)
hold on
plot(W,MPG, 'go')
But if you want to use different marker colors for each column and different marker shapes for each row of the original predictor matrix, you'll need to plot each point individually or you could use scatter().
figure()
plot(mdl)
hold on
% Define color for each column
color = lines(6);
% Define marker for each row
marker = ['so*ph'];
% I prefer to use plot at the moment but you could
% also use scatter with a different setup
for i = 1:numel(W)
[row, col] = ind2sub([5,6], i); % Get row and col num
plot(W(i), MPG(i), 'Marker', marker(row), 'Color', color(col,:), 'LineWidth',2, 'MarkerSize', 12)
end
% Remove legend (to keep the demo simple)
delete(findobj(gcf, 'type', 'legend'))
An important caveat is that if your model contains more than 1 predictor, the result of plot(mdl) will be a added-variable plot which does not have a direct relationship to the predictore/response variables. In that case, you'd need to use the model along with your predcitors to compute those scatter point positions.
  1 Commento
Sha
Sha il 23 Ott 2020
Modificato: Sha il 23 Ott 2020
thank you very much for the detailed answer, it's perfect!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by