Assign specific color to legend based on specific values.
Show older comments
I am trying to create a scatter plot with 8 suplots (2x4) with gscatter. The first row plots get data from table A while the second from table B. The two datasets contain a column named "type" which I want to use as group/color in my scatter plots.
Everything works OK but...
While the same values appear in A.type and B.type, they don't get the same color. For example if A.type = 'man' gets green color, while if B.type = 'man' gets a red color which makes it frustrating in order to read on the same plot.
Is there a way to assign specific color to specific values?
Example code:
[...]
subplot(2,4,4)
gscatter(A.parameter3, A.parameter4, A.type);
subplot(1,4,5)
gscatter(B.parameter1, B.parameter2, B.type);
[...]
7 Comments
dpb
on 6 Jul 2019
There's optional 'color' paramter to use.
"gscatter(x,y,group,clr,sym,siz) specifies the color, marker type, and size for each group. clr is either a character array of colors recognized by the plot function or a three-column matrix of color specifications. ... If you do not specify enough values for all groups,gscatter cycles through the specified values as needed."
Not clear just what you have in mind can be done by routine use of the grouping variables or not, but give that a shot first.
Without your data or at least a visual of what you have vis a vis what is wanted, hard to say exactly. You might be forced to build the plots piecemeal by judicious use of 'hold on' and multiple calls to have enough handles to the lines drawn; dunno about that w/o the full skinny.
Emmanouil Barmpounakis
on 6 Jul 2019
dpb
on 6 Jul 2019
Well, the above doc says specifically the color input variable clr specifies the color for each group. Did you try it?
Emmanouil Barmpounakis
on 7 Jul 2019
dpb
on 7 Jul 2019
Show how you set the color the same by group using the optional clr argument...the code you show doesn't use it.
Emmanouil Barmpounakis
on 7 Jul 2019
"gscatter(x,y,group,clr,sym,siz) specifies the color, marker type, and size for each group. clr is either a character array of colors recognized by the plot function or a three-column matrix of color specifications. ... If you do not specify enough values for all groups,gscatter cycles through the specified values as needed."
Write
gscatter(A.parameter3, A.parameter4, A.type,yourcolorordervector);
as the doc instructs...and the example shows changing both the color and symbol for the two groups in the example dataset on the local R2017b doc or actually there's four-group example on current doc at https://www.mathworks.com/help/stats/gscatter.html?s_tid=doc_ta#mw_e8f95d9c-cb60-470e-844d-99fb39693e2f.
Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!