How to Combine Line and Bar Charts with multiple groups in the same figure?

8 Ansichten (letzte 30 Tage)
I found below code to combine 1 group of line and bars
days = 0:5:35;
conc = [515 420 370 250 135 120 60 20];
temp = [29 23 27 25 20 23 23 17];
yyaxis left
b = bar(days,temp);
yyaxis right
p = plot(days,conc);
Also, found below code to display group of bars:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)
Wondering if it's possible to combine both in just one figure in order to use the same axis in all the groups and named each bar differently as below image:
Thanks.
  2 Kommentare
dpb
dpb am 19 Jul. 2019
The image is essentially
hF=figure;
hF.Color='w';
for i=1:4
hAx(i)=subplot(1,4,i); % create subplot
end
excepting the figure window will needs must be wide and short to mimic the shape as compared to the default figure.
Also, it isn't possible to change the axis line color or visibility independently of the axis tick label color so if going to show the y-axis tick values, the axis line is going to be visible, too.
Alternatively, one can draw the labels independently with text().
Francisco Javier Hernandez Santiago
Bearbeitet: Francisco Javier Hernandez Santiago am 19 Jul. 2019
In my default image, first I plotted each figure independently and paste all of them together in paint. Your code essentially does my default image. Thanks for that.
Considering I was able to plot different bar groups, I just wanted to know if it was possible to include different lines for each group as below image. Note here that I was pretending to leave both y-axis (left and right side) in a visible way. Main idea is to remove repetitive y-axis to make the figure clearer.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 20 Jul. 2019
Oh...didn't follow what were after...thought it was to create the image-like figure...
The following will get you started --
hB=bar(y); % the grouped bar--save handle, we need some data
xoff=hB(1).XOffset % the offset of bars in each group (hidden property)
dx=[xoff 0 -xoff].'; % XOffset value is negative, this is the three loc's relative center
yyaxis right % make a new axis
hL=plot(1+dx,10*[2 2 3],'*-'); % just made up one set of data
ylim([15 50]) % and adjusted limits to make show where wanted
for the general case, create the array of x,y data for each group as above except each group is centered around 1:4, of course, not the hardcoded '1' I used above.
To draw smooth curve you'll have to increase the number of points between the limits (linspace is perfect for this job) and evaluate the curve at enough points to make the smooth curve; the above just gives two straight lines, of course between three points.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by