Help making a histogram-like bar graph
4 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to compile data from 3 different variables (ST, SM, and SV) onto 1 stacked bar graph. I want my x-axis to be my variables of interest, and the y-axis to be like a histogram. I want to use the bar function so I have some more flexibility. My adviser helped me put together some code but I think I'm still unclear on how the bar graph function works. Here is an example of my code:
%%cycle through all subjects, importing data from Excel for iSubj = 1:length(SUBJECTS)
%%import relevant data from Excel
qthres = xlsread(xlsfile, iSubj,'J3:J16');
scalae = xlsread(xlsfile, iSubj, 'C3:C16');
%finds scalae
[STi, STj, ST] = (find(scalae == 1 & qthres > 0));
[SMi, SMj,SM] = (find(scalae == 2 & qthres > 0));
[SVi, SVj,SV] = (find(scalae == 3 & qthres > 0));
thresST = qthres(STi);
thresSM = qthres(SMi);
thresSV = qthres(SVi);
%store scalae info for each subject
allST = cat(1, allST, thresST);
allSM = cat(1, allSM, thresSM);
allSV = cat(1, allSV, thresSV);
end
%%plot histogram%%
figure(1);
BIN = 3;
ST = hist(allST,[min(allST):BIN:max(allST)]);
SM = hist(allSM,[min(allST):BIN:max(allST)]);
SV = hist(allSV,[min(allST):BIN:max(allST)]);
barplotST = [ST'];
hbarST = bar([min(allST):BIN:max(allST)],barplotST, 'stacked');
set(hbarST, 'FaceColor', 'k', 'EdgeColor', 'k');
hold all;
barplotSM = [SM'];
hbarSM = bar([min(allST):BIN:max(allST)],barplotSM, 'stacked');
set(hbarSM, 'FaceColor', 'g', 'EdgeColor', 'g');
barplotSV= [SV'];
hbarSV = bar([min(allST):BIN:max(allST)],barplotSV, 'stacked');
set(hbarSV, 'FaceColor', 'b', 'EdgeColor', 'b');
I separated everything out so I could make each variable its own color. Perhaps there is a better way to do this and to get the y-axis as a "frequency" or "count." The reason I ask is when I do this with another variable of interest, certain vectors end up empty. I think it has to do with how I"m plotting the y-axis.
Any help to simplify this? Thanks!
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!