Setting marker and colour in gscatter by the categorical group string

70 ビュー (過去 30 日間)
Derek Hamilton
Derek Hamilton 2019 年 11 月 4 日
コメント済み: Marcus Leiwe 2020 年 8 月 19 日
I have datasets that will always have a category attached (species of animal) and I want to be able to define that each species has a different marker type. I will likely use this to also set the colour by a second group – the site from where the animals came.
I have seen code for having Matlab use a different colour for each group, but not a different marker.
More importantly, I want to be able to define these so they are always the same (cattle are always circles and horses are always squares, etc).

回答 (2 件)

Subhadeep Koley
Subhadeep Koley 2019 年 11 月 7 日
Hi, you can refer to the example code below, which plots the height and weight of various animals grouped by species and country of origin. (Use the attached demoDataMultiScattered.mat file)
% Load the demo data
load demoDataMultiScattered.mat;
% Create grouping variable 'g'
g = {country, species};
% Declare colors and markers
colors = 'rgbcmyk'; markers = 'hsdp';
% Plot the data
figure('Color','w');
gscatter(height,weight, g,colors,'hsdp',8);
legend('Location','northeastoutside');
title('Multi Grouped Scatter Plot');
xlabel('Height (in cm)');
ylabel('Weight (in Kilograms)');
multiScatter.png
Hope this helps!

Derek Hamilton
Derek Hamilton 2019 年 11 月 7 日
Thank you for this.
I am hoping to do something a bit more complex, though the more I read I am thinking it may require plotting via a loop.
What I want is for say Asia to always be Green and Canada to always be Red, and Reptiles to always be Stars and Mammals to always be Squares, rather than Matlab setting the Marker and Color in order that the Group first appears in the data.
However, as I am realising I may need to Sort the data initially if I use gscatter or use a loop and just create a normal xy plot.
  2 件のコメント
Subhadeep Koley
Subhadeep Koley 2019 年 11 月 7 日
Yes you can use loop. But plotting using a loop is inefficient. As, it generates a huge number of individual graphic objects instead of just one. You can do that without using any loop also.
Refer the code below
% Load the demo data
load demoDataMultiScattered.mat;
% Create grouping variable 'g'
g = {country, species};
% Declare colors and markers
colors = 'rrrrggggbbbbmmmm'; markers = 'hsdp';
% Plot the data
figure('Color','w');
gscatter(height,weight, g, colors, markers, 8);
legend('Location','northeastoutside');
title('Multi Grouped Scatter Plot');
xlabel('Height (in cm)');
ylabel('Weight (in Kilograms)');
multiScatter1.png
Hope this helps!
Marcus Leiwe
Marcus Leiwe 2020 年 8 月 19 日
I realise this is jumping on an old question but what if in one of the cases that group is now empty. I find that gscatter is exluding the group

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by