Plot only specific categorical values / scatterplot

Hello,
I want to create a scatterplot of selected values (in this case A, B, D; without C & E) of a categorical attribute (see image).
Unfortunately I have not found a solution for this-
Help is much appreciated!

3 件のコメント

Adam Danz
Adam Danz 2021 年 4 月 28 日
The scatter function supports categorical inputs. Plus when I search for "matlab scatter categorical" in google I get lots and lots of hits. What specificially are you having trouble with?
Simon Schmidt
Simon Schmidt 2021 年 4 月 28 日
Hi Adam,
im having trouble with the selection. As described above I only want to plot some specific values of shop_name ("A", "B", "D")
Chad Greene
Chad Greene 2021 年 4 月 28 日
gscatter might also be useful.

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

 採用された回答

Adam Danz
Adam Danz 2021 年 4 月 28 日
編集済み: Adam Danz 2021 年 4 月 29 日

2 投票

>I only want to plot some specific values of shop_name ("A", "B", "D")
Using idexing. But with categories you also need to set which categories should appear on the axes (last line in demo). Otherwise, even with indexing, all categories will display in the plot, even if they do not contain y-values due to indexing.
Demo 1
T = table(repelem(categorical(num2cell(char(65:70))),1,2)', randi(10,12,1),'VariableNames',{'Shop','Sales'})
T = 12×2 table
Shop Sales ____ _____ A 5 A 5 B 4 B 6 C 7 C 10 D 3 D 3 E 5 E 2 F 8 F 7
selection = categorical({'A','B','D'});
idx = ismember(T.Shop, selection);
scatter(T.Shop(idx), T.Sales(idx),90,'filled')
ax = gca();
ax.XAxis.Categories = selection; % Set which categories appear
Demo 2
After adding the answer, I realized that indexing isn't really required but if you have many data points, indexing might help to speed up the process and reduce the size of the figure in memory since it only plots the data you want rather than plotting all of the data and then making only some of it visible like this approach does below.
T = table(repelem(categorical(num2cell(char(65:70))),1,2)', randi(10,12,1),'VariableNames',{'Shop','Sales'})
scatter(T.Shop, T.Sales,90,'filled')
ax = gca();
ax.XAxis.Categories = categorical({'A','B','D'});
If you want to change the visible categories you don't need to reproduce the plot, you just need to call this last line again with the selected categories.

4 件のコメント

Simon Schmidt
Simon Schmidt 2021 年 4 月 29 日
Awesome! This is exactly what I was looking for! Really appreciate your effort! <3
Adam Danz
Adam Danz 2021 年 4 月 29 日
Thanks @Simon Schmidt. After looking at my answer again, I added a second demo which is simpler but I'm undecided which method is most efficient because the second method stores all of the figure data that isn't plotted in memory. If you don't have a huge amount of points to plot, I think I'd go with the second method.
Simon Schmidt
Simon Schmidt 2021 年 4 月 29 日
Its about 2,000,000 rows of data- so i think your first solution is more handy. Thanks again!
Adam Danz
Adam Danz 2021 年 4 月 29 日
Agreed. Glad I could help.

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021a

質問済み:

2021 年 4 月 28 日

コメント済み:

2021 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by