Stacked bar graph with secondary y-axis line graph

Hi,
I'm trying to achieve something like this from my excel file (attached) using MATLAB plot but I'm having a hard time. See below graph generated from excel and I wanted to regenerate it using MATLAB. Data is in excel sheet as well. Thank you.
Capture.JPG

 채택된 답변

dpb
dpb 2019년 6월 29일
편집: dpb 2019년 6월 29일
[~,~,r]=xlsread('MLQ.xlsx'); % just read the spreadsheet to see what was in it...
MLQopt=detectImportOptions('MLQ.xlsx'); % will need some help to skip over the first two lines for data
MLQ=readtable('MLQ.xlsx',MLQopt); % and read the data into table
MLQ.Properties.VariableNames=cellfun(@(s)strrep(s,' ','_'),r(2,1:7),'uni',0); % get rid of blanks for v names
hB=bar(MLQ.Hour,MLQ{:,[4 3 5 6]},'stacked'); % start with the bar plot
hold on % prepare to add LH axis line
hLL=plot(MLQ.Hour,MLQ.Load,'r-','LineWidth',3); % and do so...
yyaxis right % get ready to RH axis line
hLR=plot(MLQ.Hour,MLQ.BT_SOC,'k-','LineWidth',3); % and plot them, too...
xlim([0.5 20.5])
xticks(1:20)
is the starting point for final cleanup and labelling--

댓글 수: 4

Thank you. However, it seems the graph formed is different. For the primary y axis, there should be both the stacked bar graph as well as that one line graph (Load). As well, there should only be one line graph corresponding to the secondary axis which is the BT SOC.
dpb
dpb 2019년 6월 29일
편집: dpb 2019년 6월 29일
Just noticed the load is on LH axis...was updating then saw the comment. See revised (altho the attached figure doesn't have the xlim and xticks stuff but just default)
It worked. Thanks so much. :)
dpb
dpb 2019년 6월 29일
Oh. If you do set x axis parameters, remember both axes need same...or to be linked (and I don't know otomh if the new(ish) yyaxis does that automagically or not.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2019년 6월 29일

댓글:

dpb
2019년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by