Plot only specific categorical values / scatterplot
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Simon Schmidt
il 28 Apr 2021
Commentato: Adam Danz
il 29 Apr 2021
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/599875/image.bmp)
Unfortunately I have not found a solution for this-
Help is much appreciated!
3 Commenti
Risposta accettata
Adam Danz
il 28 Apr 2021
Modificato: Adam Danz
il 29 Apr 2021
>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'})
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Distribution Plots 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!