Matlab - How to plot 2 different columns of excel data for 2 boxplot under 1 x-axis label?

7 次查看(过去 30 天)
I m trying to plot 2 different columns of excelsheet data (.xls) "Data1" & "Data2" under 1 x-axis label "Jan" to compare them side by side visually. How do i do it? Would be really happy if someone could advise me on how to do it.
My excel data:
This is what i m trying to attempt but i do not know what to add in my code to do it:
clear all
clc
surveyData = readtable("surveydata_16Dec2022.xlsx");
surveyData(1:447,["SceneCode", "Data1", "Data2"])
tiledlayout(1,2)
ScoreS = [-3, -2, -1, 0, 1, 2, 3];
mnthOrder = ["Jan","Feb","Mar"];
surveyData.SceneCode = categorical(surveyData.SceneCode, mnthOrder);
surveyData.SceneQuestionCompare = categorical(surveyData.SceneCode, mnthOrder);
% Left axes
ax1 = nexttile
boxchart(surveyData.SceneCode, surveyData.Data1);
xlabel(ax1,'Scenes 1~12')
yt = get(gca, 'YTick');
ytx = linspace(min(yt), max(yt), numel(ScoreS));
ytl = sprintfc('%.0f',ScoreS);
ylabel(ax1,'Qn1')
legend('Physical (N=36)')
% Right axes
ax2 = nexttile
boxchart(surveyData.SceneCode, [surveyData.Data2])
xlabel(ax2,'Scenes 1~12')
yt = get(gca, 'YTick'); % Get Y-Tick Values
ytx = linspace(min(yt), max(yt), numel(ScoreS)); % New Y-Tick Values
ytl = sprintfc('%.0f',ScoreS); % New Y-Tick Labels
set(gca, 'YTick',ytx, 'YTickLabel',ytl)
ylabel(ax2,'Qn2')
legend('VR (N=36)')

回答(2 个)

Chaitanya Krishna Kantambhatla
Hello Zhan
I understand that you are trying to plot 2 different columns under 1 x-axis label using ‘boxchart’.
Please refer to the examples in this documentation or execute the following command to open the example
>> openExample('graphics/UseCombinationOfGroupingVariablesExample')
This example shows how to plot boxchat for 2 variables on same x- axis, considering Temperature data for the years 2015 and 2016.
I hope the above example was helpful.
  1 个评论
Zhan An
Zhan An 2023-1-4
The boxplot function takes in boxplot(xdata, ydata, groupByData). I wanted to put in 2 different columns of data (e.g. cause1, cause2) into the ydata (y-axis) but apparently it only limits me to put in 1 column of data. I do not want the grouping option.
Would appreciate if you have any solution to go around with that!

请先登录,再进行评论。


Voss
Voss 2023-1-4
@Zhan An: This answer is essentially the same as what you're trying to do (plot two boxcharts for each x-tick):
The idea is to create one boxchart per column of data, and specify the x-location of each boxchart so that one can be a little to the left and one can be a little to the right of the x-tick location.
See if you can adapt it to work with your data. If you have problems, upload your data here (using the paperclip icon).
  12 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by