boxchart - different box width according to number of data points
15 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am looking for a way to set the width of a boxplot according to the number of datapoints within a boxchart.
See attachet an example how it looks like in R. When I try this in matlab, I get an error, because matlab accepts only scalars and no vectors. I would prefer to do all my statistics with matlab, so this function would be very helpful.
Thank you
Markus
1 件のコメント
dpb
2024 年 8 月 24 日
編集済み: dpb
2024 年 8 月 24 日
Submit it as an enhancement request.
You could try to make it work with boxchart by use of hold on and plotting each bar invidvidually. This would take some doing to manage to get the positions correct and whether could be managed or not I don't know for sure, but other than drawing one from basic primitives, it's all that comes to mind at present.
採用された回答
dpb
2024 年 8 月 25 日
編集済み: dpb
2024 年 8 月 25 日
I'll add a shorter, cleaner version as well...go ahead and accept the answer since it is the basis for the result desired if you would...
D=readtable("Data.csv");
D.Lagen=categorical(D.Lagen);
D.D_Lage=categorical(D.D_Lage);
[G,ixG]=findgroups(D.D_Lage); % find the grouping in the D_Lage variable
rows=histc(G,unique(G)); % count how many of each
bw=rows/sum(rows); % compute the relative size fractions
figure
hold on
for i=1:length(rows)
ix=(D.D_Lage==ixG(i)); % logical addressing each by the grouping id vector
bc(i)=boxchart(D.D_Lage(ix),D.ft0(ix),'BoxWidth',bw(i));
if i>1, bc(i).BoxFaceColor=bc(1).BoxFaceColor; end
end
is much shorter code-wise...while if you look it up you'll find that histc has been deprecated, for such counting as this it works flawlessly and I've had issues with how histcounts wants to define bin boundaries that it doesn't always put things into the desired bin. accumarray is the old-timers solution, but its syntax is somewhat more arcane for newcomers.
0 件のコメント
その他の回答 (1 件)
dpb
2024 年 8 月 24 日
編集済み: dpb
2024 年 8 月 25 日
With sufficient perserverence it looks as though should be able to make boxchart work...the doc had an easy example to illustrate the idea...
tbl = readtable('TemperatureData.csv');
tbl.Month = categorical(tbl.Month,monthOrder);
hBxCh=boxchart(tbl.Month(iJan),tbl.TemperatureF(iJan),'GroupByColor',tbl.Year(iJan)) % base boxplot; note are two objects, one for each month
iJan=tbl.Month=="January"; % so, pick only one month
figure
hBxCh=boxchart(tbl.Month(iJan),tbl.TemperatureF(iJan),'GroupByColor',tbl.Year(iJan)) % chart it
hBxCh(1).BoxWidth=0.25; % now can set its width
Although it's bound to be tedious, it looks as though one could manage to arrange the complete data such that one could plot it a variable at a time and thereby to get additional objects that could then have their individual bar widths set.
With the by grouping option, it certainly is a reasonable enhancement request to be able to control the barwidth by group as well.
ERRATUM:
Well, pooh! The supplied data file isn't available on this platform...it's
>> which -all temperaturedata.csv
C:\MLR2021b\examples\graphics\data\TemperatureData.csv
>>
on desktop install...first time I've tried to use a demo example and the data haven't been here...I attached the image of what it looks like here.
4 件のコメント
dpb
2024 年 8 月 27 日
編集済み: dpb
2024 年 8 月 29 日
Glad to help; kinda' an interesting challenge of what would appear to be a logical ability already...anyways, the only real wart I see with this is the whisker line is also proportional to the bar width so for the narrower ones it is almost invisible. Unfortunately, the WhiskerLines hidden property is currently still just a placeholder; there is no way to get access to the line handles in order to change their length; one would have to add code to add another line where should be of a given length to create the effect.
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!