필터 지우기
필터 지우기

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
Chaitanya Krishna Kantambhatla 2022년 12월 28일
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
Voss
Voss 2023년 1월 16일
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!

댓글을 달려면 로그인하십시오.

카테고리

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