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 个评论
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.
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.
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?
Share your data. Place your variables in a mat file and attach them using the paperclip icon.
What are data_dB1, data_dB2, and data_dB3?
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);

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.
Hello. How can I use boxchart? My version of Matlab is 2019a ans sais Undefined function or variable 'boxchart'.
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.
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!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
