Plot with bar side by side and with two y axes
1 view (last 30 days)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1101435/image.png)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1101440/image.png)
0 Comments
Accepted Answer
dpb
on 18 Aug 2022
You can't have two overlaying axes with the same x value and use bar with only a single variable because then the bars will always be at the same location on the x axis. I suppose one could get very clever about setting the xlim values on the two, but that would be tricky at best...
The solution to your dilemma is the magic of NaN -- create a dummy variable for each of the two variables in turn and plot the both as if they were grouped as your last plot --
x=datetime(2022,11,24:28).';
y1=randi(20,size(x));
y2=randi(500,size(x));
hB1=bar(x,[ y1 nan(size(x))]);
yyaxis right
hB2=bar(x,[nan(size(x)) y2]);
But, while this does what you wanted, I'd contend it is misleading owing to the scaling on separate axes makes the two variables appear to be nearly the same in amplitude.
Sometimes, "just because you CAN do something doesn't mean you SHOULD!"
0 Comments
More Answers (0)
See Also
Categories
Find more on Annotations 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!