フィルターのクリア

Using boxchart, how can I get the boxes to align with my labels?

35 ビュー (過去 30 日間)
Danny Krafft
Danny Krafft 2023 年 7 月 27 日
編集済み: Cris LaPierre 2023 年 7 月 28 日
I am using boxchart to plot 8 datasets on the same plot. I'd like them to alternate in color and specify the x-axis labels. I managed to acheive this using a table, but now when I plot the data, the boxes are getting grouped in pairs and the x-axis labels are not aligned properly.
Here is my current output (code below):
Everything looks good except the labels don't align with the boxes, which are getting grouped in pairs. Does anyone have any ideas on how I can make these labels alignt with the boxes?
roiindx = [1 4 7 8 10]; % pulling specific items from a larger dataset
i=1; % ultimately, this will be in a loop, but I am trying to keep it simple for now
% this is the data, I am pulling 8 ROIs from this data each 5x5pixels
load('extractedROIdata2023.mat')
% simple script to create cell arrays with label data. size = (200x1)
% cats are the 8 different categories
% corrected is really just a logical containing "corrected" & "uncorrected"
[cats, corrected] = generateCategoricalforBoxcharts();
sz = size(right_hduncroi{roiindx(i)});% (5x5 before reshaping)
% reshape selected ROI to be 25x1 before concatenating all into a single
% table which is same size as the cats and corrected label data (200x1)
% right side
rhdunc = double(reshape(right_hduncroi{roiindx(i)},1,sz(1)*sz(2))');
rhdc = double(reshape(right_hdcroi{roiindx(i)},1,sz(1)*sz(2))');
rldunc = double(reshape(right_lduncroi{roiindx(i)},1,sz(1)*sz(2))');
rldc = double(reshape(right_ldcroi{roiindx(i)},1,sz(1)*sz(2))');
% left side
lhdunc = double(reshape(left_hduncroi{roiindx(i)},1,sz(1)*sz(2))');
lhdc = double(reshape(left_hdcroi{roiindx(i)},1,sz(1)*sz(2))');
lldunc = double(reshape(left_lduncroi{roiindx(i)},1,sz(1)*sz(2))');
lldc = double(reshape(left_ldcroi{roiindx(i)},1,sz(1)*sz(2))');
data = [lhdunc; lhdc; lldunc; lldc; rhdunc; rhdc; rldunc; rldc]; % 200x1 double
T = table(cats,data,corrected); % create table with data and labels
dataorder = {'Left HD','Left HD''','Left LD','Left LD''','Right HD','Right HD''','Right LD','Right LD'''};
T.cats = categorical(T.cats,dataorder);
T.corrected = categorical(T.corrected);
boxchart(T.cats,T.data,'GroupByColor',T.corrected)
  2 件のコメント
the cyclist
the cyclist 2023 年 7 月 27 日
移動済み: Voss 2023 年 7 月 27 日
Can you upload the data file? You can use the paper clip icon in the INSERT section of the toolbar.
Danny Krafft
Danny Krafft 2023 年 7 月 27 日
This should be everything you need to run this code. Thanks!

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

採用された回答

Voss
Voss 2023 年 7 月 27 日
編集済み: Voss 2023 年 7 月 27 日
All this is the same as what you already have:
roiindx = [1 4 7 8 10]; % pulling specific items from a larger dataset
i=1; % ultimately, this will be in a loop, but I am trying to keep it simple for now
% this is the data, I am pulling 8 ROIs from this data each 5x5pixels
load('extractedROIdata2023.mat')
% simple script to create cell arrays with label data. size = (200x1)
% cats are the 8 different categories
% corrected is really just a logical containing "corrected" & "uncorrected"
[cats, corrected] = generateCategoricalforBoxcharts();
sz = size(right_hduncroi{roiindx(i)});% (5x5 before reshaping)
% reshape selected ROI to be 25x1 before concatenating all into a single
% table which is same size as the cats and corrected label data (200x1)
% right side
rhdunc = double(reshape(right_hduncroi{roiindx(i)},1,sz(1)*sz(2))');
rhdc = double(reshape(right_hdcroi{roiindx(i)},1,sz(1)*sz(2))');
rldunc = double(reshape(right_lduncroi{roiindx(i)},1,sz(1)*sz(2))');
rldc = double(reshape(right_ldcroi{roiindx(i)},1,sz(1)*sz(2))');
% left side
lhdunc = double(reshape(left_hduncroi{roiindx(i)},1,sz(1)*sz(2))');
lhdc = double(reshape(left_hdcroi{roiindx(i)},1,sz(1)*sz(2))');
lldunc = double(reshape(left_lduncroi{roiindx(i)},1,sz(1)*sz(2))');
lldc = double(reshape(left_ldcroi{roiindx(i)},1,sz(1)*sz(2))');
data = [lhdunc; lhdc; lldunc; lldc; rhdunc; rhdc; rldunc; rldc]; % 200x1 double
T = table(cats,data,corrected); % create table with data and labels
dataorder = {'Left HD','Left HD''','Left LD','Left LD''','Right HD','Right HD''','Right LD','Right LD'''};
T.cats = categorical(T.cats,dataorder);
Then, instead of grouping, you can create two separate boxcharts, one for 'corrected' and one not.
idx = strcmp(T.corrected,'corrected');
boxchart(T.cats(idx),T.data(idx));
hold on
boxchart(T.cats(~idx),T.data(~idx));
  1 件のコメント
Danny Krafft
Danny Krafft 2023 年 7 月 28 日
Oh, you did it! This looks exaclty like I was hoping for, I'll try this approach. Thanks!!

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

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 7 月 27 日
編集済み: Cris LaPierre 2023 年 7 月 27 日
This is a result of your data. You are missing data for some of your categories. If you had data in each category, your boxchart should have a blue and orange box centered on each tick. Your figure starts with orange, which means you have no data for T.cats="Left HD" and T.corrected="false".
Here's how the data should look if you have data for each categorical
cats = ["Left HD";"Left HD'";"Left LD";"Left LD'";"Right HD";"Right HD'";"Right LD";"Right LD'"];
cats = repmat(cats,10,1);
data = rand(length(cats),1);
corrected = rand(length(cats),1)>0.5;
dataorder = {'Left HD','Left HD''','Left LD','Left LD''','Right HD','Right HD''','Right LD','Right LD'''};
T = table(cats,data,corrected);
T.cats = categorical(T.cats,dataorder);
T.corrected = categorical(T.corrected);
boxchart(T.cats,T.data,'GroupByColor',T.corrected)
legend
Here's what it looks like if you are missing data in some categories.
T(T.cats=="Left HD" & T.corrected=="false",:)=[];
T(T.cats=="Left HD'" & T.corrected=="true",:)=[];
T(T.cats=="Left LD" & T.corrected=="false",:)=[];
T(T.cats=="Left LD'" & T.corrected=="true",:)=[];
T(T.cats=="Right HD" & T.corrected=="false",:)=[];
T(T.cats=="Right HD'" & T.corrected=="true",:)=[];
T(T.cats=="Right LD" & T.corrected=="false",:)=[];
T(T.cats=="Right LD'" & T.corrected=="true",:)=[];
figure
boxchart(T.cats,T.data,'GroupByColor',T.corrected)
legend
  2 件のコメント
Danny Krafft
Danny Krafft 2023 年 7 月 28 日
Thanks for the feedback. Each box should have its own xtick label (8 boxes 8 labels). I don't want one tick for two boxes. The 'corrected' data is an output from a NN. So I am comparing ROIs from uncorrected data and corrected data (before and after the correction network) right next to eachother. This is for a paper I am publishing so I need this to be clear.
When you say I am missing datasets, you mean that I'm not representing every case? There are four cases initially: Left High DoLP (HD), Left Low DoLP (LD), Right HD and Right LD. Each is a 5x5 ROI and each are being represented before and after correction (this is where the 8 comes in). The xtick labels have a ' representing the corrected data. So you see I have and uncorrected ROI next to the same ROI, after correction, and I'd like a label communicating this for each box.
If I understand you correctly, my missing data isn't really missing, the function is just expecting those to be there? Is there a better way for me to go about generating this plot or formatting the data such that the output matches what I want?
Do you think I can just have the labels be the initial four cases, have the boxes plotted two to a label and just have a legend saying that red is uncorrected and blue is corrected?
I hope my response was clear,
Thanks again!
Cris LaPierre
Cris LaPierre 2023 年 7 月 28 日
編集済み: Cris LaPierre 2023 年 7 月 28 日
In a grouped boxchart, grouped boxes share a tick. So if you have 8 ticks and 2 groups, there should be 16 boxes. If you wanted to use the grouped option here, you would have 4 ticks and 2 groups, with the legend indicating the category.
roiindx = [1 4 7 8 10]; % pulling specific items from a larger dataset
i=1; % ultimately, this will be in a loop, but I am trying to keep it simple for now
% this is the data, I am pulling 8 ROIs from this data each 5x5pixels
load("extractedROIdata2023.mat")
sz = numel(right_hduncroi{roiindx(i)});
grpnames = categorical(["Left HD","Left HD'","Left LD","Left LD'","Right HD","Right HD'","Right LD","Right LD'"]);
catnames = categorical(["Left HD","Left LD","Right HD","Right LD"]);
c = repelem("corrected",sz,1);
unc = repelem("uncorrected",sz,1);
% reshape selected ROI to be 25x1 before concatenating all into a single
% table which is same size as the cats and corrected label data (200x1)
% right side
rhdunc = right_hduncroi{roiindx(i)}(:);
rhdc = right_hdcroi{roiindx(i)}(:);
rldunc = right_lduncroi{roiindx(i)}(:);
rldc = right_ldcroi{roiindx(i)}(:);
% left side
lhdunc = left_hduncroi{roiindx(i)}(:);
lhdc = left_hdcroi{roiindx(i)}(:);
lldunc = left_lduncroi{roiindx(i)}(:);
lldc = left_ldcroi{roiindx(i)}(:);
data = [lhdunc; lhdc; lldunc; lldc; rhdunc; rhdc; rldunc; rldc]; % 200x1 double
corrected = [unc;c;unc;c;unc;c;unc;c];
cats = repelem(catnames([1 1 2 2 3 3 4 4]),sz,1);
cats = cats(:);
grp = repelem(grpnames([1 2 3 4 5 6 7 8]),sz,1);
grp = grp(:);
T = table(cats,grp,data,corrected); % create table with data and labels
T.corrected = categorical(T.corrected)
T = 200×4 table
cats grp data corrected _______ _______ _______ ___________ Left HD Left HD 0.66466 uncorrected Left HD Left HD 0.5169 uncorrected Left HD Left HD 0.38823 uncorrected Left HD Left HD 0.32834 uncorrected Left HD Left HD 0.40379 uncorrected Left HD Left HD 0.48346 uncorrected Left HD Left HD 0.35578 uncorrected Left HD Left HD 0.31719 uncorrected Left HD Left HD 0.40748 uncorrected Left HD Left HD 0.58592 uncorrected Left HD Left HD 0.43283 uncorrected Left HD Left HD 0.30393 uncorrected Left HD Left HD 0.28204 uncorrected Left HD Left HD 0.39402 uncorrected Left HD Left HD 0.60127 uncorrected Left HD Left HD 0.4974 uncorrected
boxchart(T.cats,T.data,'GroupByColor',T.corrected)
legend('Location','best')
If you want each box to have its own tick and be a different color, the simplest approach is the one Voss shared.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by