How to use boxchart()?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am trying to use a boxchart for the first time as opposed to a boxplot, as it looks to have better functionality for what I want to do.
But, it's not working and I can't understand what's wrong...
On the x axis I would like to have 22 different dates, represented on the y by a box plot of 22*144 data points.
boxchart(data_dB1,dates_concatenated)
Error using boxchart
Expected ydata to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
Error in boxchart (line 95)
validateattributes(ydata,{'numeric'},{'2d','real'},mfilename,'ydata');


採用された回答
Cris LaPierre
2020 年 11 月 13 日
編集済み: Cris LaPierre
2020 年 11 月 13 日
I think the error is because you are using the syntax boxchart(xgroupdata,ydata), but your inputs are reversed. You put the grouping variable second. Also, your grouping data is not valid for use as xgroupdata.
boxchart(___,'GroupByColor',cgroupdata)
Therefore, try changing your code to
boxchart(data_dB1,'GroupByColor',dates_concatenated)
12 件のコメント
Louise Wilson
2020 年 11 月 16 日
Thanks Chris. I am sorry, I see now I was getting it wrong and my question wasn't as clear as it could be. I realise that I do not want to group by date, but by three different categories. I can do this:
h=figure(7);
subplot(5,1,1)
set(h,'WindowStyle','docked')
boxchart(data_dB1);
hold on
boxchart(data_dB2);
boxchart(data_dB3);
ylim([80 150]);
xticklabels(datestr(dates_concatenated1,'dd-mm-yy'));
xtickangle(45);
legend(["50-24,000 Hz","50-5000Hz","50-1500 Hz"]);
title(site,'FontSize',14);
xlabel('Date','FontSize',14); ylabel('SPL_{rms}', 'FontSize',14);
...which will plot a boxchart for each different category on top of each other within one xtick. But, what I want to do for clarity is to have three boxcharts side-by-side within one x-tick, so they can be seen clearly. So, the question is, how do I group these data correctly? Each 'group' is 144*22 double matrix and belongs to one of three categories.
Cris LaPierre
2020 年 11 月 16 日
With hold on, it is doing exactly what it should - placing the plots on top of each other. You will need to include all the data in a single boxchart command.
Louise Wilson
2020 年 11 月 16 日
Yes, I understand it is doing as it should, just that what it should do is not my ideal solution. I have tried to use boxchart as it has legend whereas boxplot does not. I have concatenated all of my data into two arrays:
C=cell(432,22); %labels for each category
C(1:144,1:22)={'50-24,000'};
C(145:288,1:22)={'50:5000'};
C(289:432,1:22)={'50:1500'};
dB_values=[dB1; dB2; dB3]
b=boxchart(dB_values,'GroupByColor',C)
but I get the error:
Error using matlab.graphics.chart.primitive.BoxChart
The name 'GroupByColor' is not an accessible property for
an instance of class
'matlab.graphics.chart.primitive.BoxChart'.
Error in boxchart (line 128)
H = matlab.graphics.chart.primitive.BoxChart('Parent',
cax,...
From the example in the documentation using the patients data, this is what I should do, but it is not working?
Cris LaPierre
2020 年 11 月 16 日
Share your data. Place your variables in a mat file and attach them using the paperclip icon.
Louise Wilson
2020 年 11 月 16 日
Thanks
Cris LaPierre
2020 年 11 月 16 日
What are data_dB1, data_dB2, and data_dB3?
Louise Wilson
2020 年 11 月 16 日
They are here, and concatenated vertically in dB_values. They are dB values.
Cris LaPierre
2020 年 11 月 17 日
編集済み: Cris LaPierre
2020 年 11 月 17 日
I had to do some investigation myself first. I found this example extremely helpful. Basically, in order to create the plot you want using boxchart, you need to convert your inputs into vectors. All the data needs to be in a single column. To work, you also need to do the following
- create an xgroup vector that indicates which column the corresponding value originally came from.
- create a cgroupdata vector that indicates which data set the corresponding value came from.
I came up with something like this.
% load your data
load data_dB1.mat
load data_dB2.mat
load data_dB3.mat
% Convert each data set into a column vector.
% the (:) basically stacks all columns on top of each other. First #1, the 2 under it, then 3, ...
dB1 = data_dB1(:);
dB2 = data_dB2(:);
dB3 = data_dB3(:);
% Combine all 3 data sets into a single column vector
dB = [dB1;dB2;dB3];
% Create a corresponding x-vector, indicating which column each row came from
x=ones(size(data_dB1,1),1)*(1:size(data_dB1,2));
x = [x(:);x(:);x(:)];
% convert to dates
d=datetime(2019,8,20):days(1):datetime(2019,9,10);
d=categorical(d(x));
% Create a group vector, indicating which data set each row came from
G = ones(size(dB1));
G = [G;G*2;G*3];
% Create boxchart
boxchart(d,dB,'GroupByColor',G)
ylim([80 150]);
legend(["50-24,000 Hz","50-5000Hz","50-1500 Hz"]);
xlabel('Date','FontSize',14); ylabel('SPL_{rms}', 'FontSize',14);

Louise Wilson
2020 年 11 月 17 日
Hi Chris, this is fantastic. Thank you so much for your help, this is exactly what I was looking for. I hadn't seen that example either.
Ioannis Vourvachakis
2021 年 12 月 16 日
Hello. How can I use boxchart? My version of Matlab is 2019a ans sais Undefined function or variable 'boxchart'.
Cris LaPierre
2021 年 12 月 16 日
boxchart was introduced in R2020a. If you have the Statistics and Machine Learning toolbox, you can use boxplot instead. Otherwise, you will need to update your MATLAB version to at least R2020a.
Fuqiang Guo
2021 年 12 月 23 日
Thank you very much, your advice is very helpful, I'll update my software version
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Annotations についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
