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

35 views (last 30 days)
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)')

Answers (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 Comment
Zhan An
Zhan An on 4 Jan 2023
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!

Sign in to comment.


Voss
Voss on 4 Jan 2023
@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 Comments
Voss
Voss on 16 Jan 2023
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!

Sign in to comment.

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!