How to create a color-coded categorical tile chart

21 Ansichten (letzte 30 Tage)
Gabriel Stanley am 3 Mai 2023
Context: I'm working with a M-by-3 dataset that I've categorized into 4 different groups. As part of the visualization of this data, I'm creating a MechE-style 4 view drawing
Problem: In creating the "side" view, I want to have a visualization of which of the 4 groups (color-coded red, green, yellow, and orange) is most prevalent in any given 2-D grid section. I've got my majority vote mask, but...
Question: What is the best graphics function to create a tiled view of categorical data, akin to binscatter or histogram2(__, 'DisplayStyle','tile') but with the colors of each tile defined by a categorical mask?
datO = [[1000000.*rand(4000,1)],[10000.*rand(4000,1)],[250 + 500.*rand(4000,1)]]; %dummy data
LG = false(4000,1); LG([1:4:4000]) = true;
LO = false(4000,1); LO([2:4:4000]) = true;
LY = false(4000,1); LY([3:4:4000]) = true;
LR = false(4000,1); LR([4:4:4000]) = true;
xbins = [min(datO(:,1)):12500:max(datO(:,1))]; ybins = [min(datO(:,3)):12:max(datO(:,3))]; %hist bins
x1 = zeros(numel(xbins)-1,numel(ybins)-1,4); %preallocation for best practice
x1(:,:,1) = histcounts2(datO(LG,1),datO(LG,3),xbins,ybins); %Green counts
x1(:,:,2) = histcounts2(datO(LO,1),datO(LO,3),xbins,ybins); %orange counts
x1(:,:,3) = histcounts2(datO(LY,1),datO(LY,3),xbins,ybins); %yellow counts
x1(:,:,4) = histcounts2(datO(LR,1),datO(LR,3),xbins,ybins); %red counts
[~,x2] = max(x1,[],3); x2(~any(x1,3)) = 0; %majority category by bin & recover empty bins
%Now how best to plot this? AFAIK, I cannot directly control the tile
%colors in histogram2 or binscatter.
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

LeoAiE am 4 Mai 2023
You can achieve this using the imagesc function in MATLAB. First, create a colormap for the categories, and then use the imagesc function to display the 2D grid with the colors defined by the categorical mask. This will create a plot of the 2D grid with each tile's color defined by the x2 categorical mask. The colormap is set to match the colors of your categories (black for empty bins, green, orange, yellow, and red for the other categories). The colorbar shows the correspondence between colors and categories.
% Dummy data
datO = [1000000.*rand(4000,1), 10000.*rand(4000,1), 250 + 500.*rand(4000,1)];
% Dummy logical masks for categorization
LG = false(4000,1); LG(1:4:4000) = true;
LO = false(4000,1); LO(2:4:4000) = true;
LY = false(4000,1); LY(3:4:4000) = true;
LR = false(4000,1); LR(4:4:4000) = true;
% Histogram bins
xbins = [min(datO(:,1)):12500:max(datO(:,1))];
ybins = [min(datO(:,3)):12:max(datO(:,3))];
% Preallocation for best practice
x1 = zeros(numel(xbins)-1,numel(ybins)-1,4);
% Counts for each category
x1(:,:,1) = histcounts2(datO(LG,1),datO(LG,3),xbins,ybins); % Green counts
x1(:,:,2) = histcounts2(datO(LO,1),datO(LO,3),xbins,ybins); % Orange counts
x1(:,:,3) = histcounts2(datO(LY,1),datO(LY,3),xbins,ybins); % Yellow counts
x1(:,:,4) = histcounts2(datO(LR,1),datO(LR,3),xbins,ybins); % Red counts
% Majority category by bin & recover empty bins
[~,x2] = max(x1,[],3);
x2(~any(x1,3)) = 0;
% Create a colormap for the categories (assuming 0 is for empty bins)
custom_colormap = [0 0 0; 0 1 0; 1 0.5 0; 1 1 0; 1 0 0]; % [black; green; orange; yellow; red]
% Plot the 2D grid using imagesc
figure;
imagesc(x2);
colormap(custom_colormap);
colorbar('Ticks', 0:4, 'TickLabels', {'Empty', 'Green', 'Orange', 'Yellow', 'Red'});
axis equal;
colormap(colormap);
colorbar('Ticks', 0:4, 'TickLabels', {'Empty', 'Green', 'Orange', 'Yellow', 'Red'});
axis equal;
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Gabriel Stanley am 4 Mai 2023
That looks great. I'll accept once I get it working on my end (presently working with a largely predefined tiledlayout, so I'll need to figure out how to redefine the axes I'm placing this view in.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by