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

28 ビュー (過去 30 日間)
Zhan An
Zhan An 2022 年 12 月 15 日
コメント済み: Voss 2023 年 1 月 16 日
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 件のコメント
Zhan An
Zhan An 2023 年 1 月 16 日
Thanks alot @Voss! You really are a big help for this!
Voss
Voss 2023 年 1 月 16 日
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by