Matlab - How to plot 2 different columns of excel data for 2 boxplot under 1 x-axis label?
11 views (last 30 days)
Show older comments
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)')
0 Comments
Answers (2)
Chaitanya Krishna Kantambhatla
on 28 Dec 2022
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.
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):
https://www.mathworks.com/matlabcentral/answers/1888377-i-want-to-move-up-my-boxchart#answer_1141527
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
on 16 Jan 2023
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!