Plot with bar side by side and with two y axes
Show older comments
I have this script to obtain subplot with bar, but I would rather that the bars were side by side and with two y axes. How can I do? I have in this case the bars one above the other. The result is shown in figure below.
% Create figure
figure1 = figure('NumberTitle','off','Name','Figure','Color',[1 1 1]);
%bar plot e scatter 2011
subplot1=subplot(3,2,1,'Parent',figure1)
x1 = (1:5)';
y1 = tweetsvsnewsS3.somma_1_2;
y2 = tweetsvsnewsS3.tweets_1;
yyaxis left
p1 = bar(x1,y1, 'BarWidth', 0.25);
p1(1).FaceColor = [0.56 0.10 0.74];
yyaxis right
p12=bar(x1,y2,'BarWidth', 0.25)
p12(1).FaceColor = [0.41 0.28 0.79];
box(subplot1,'on');
hold(subplot1,'off');
set(subplot1,'XGrid','on','XMinorGrid','on','XMinorTick','on','YGrid','on',...
'YMinorGrid','on','YMinorTick','on');
set(gca,'xtick',1:12,...
'xticklabel',{'Nov 24','Nov 25','Nov 26','Nov 27','Nov 28'})
ylabel('count');
title('Data','FontSize',12)

I tried also this script but I didn't have the second y axis. In figure there is the result.
% Create figure
figure1 = figure('NumberTitle','off','Name','Figure','Color',[1 1 1]);
%bar plot e scatter 2011
subplot1=subplot(3,2,1,'Parent',figure1)
x1 = (1:5)';
y1 = tweetsvsnewsS3.somma_1_2;
y2 = tweetsvsnewsS3.tweets_1;
p1 = bar(x1,[y1,y2],'BarWidth', 0.80);
p1(1).FaceColor = [0.56 0.10 0.74];
p1(2).FaceColor = [0.41 0.28 0.79];
box(subplot1,'on');
hold(subplot1,'off');
set(subplot1,'XGrid','on','XMinorGrid','on','XMinorTick','on','YGrid','on',...
'YMinorGrid','on','YMinorTick','on');
set(gca,'xtick',1:12,...
'xticklabel',{'Nov 24','Nov 25','Nov 26','Nov 27','Nov 28'})
ylabel('count');
title('Data','FontSize',12)

Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!