Y-axis lim with Bar/Line overlay
5 views (last 30 days)
Show older comments
Hello,
I've made a 2 series bar graph with a line graph overlay. For the purposes of the data, both Y-axis need to be the same. I am able to set the line graph axis to the bar graph YLim but not the other way around.
Sometimes the line graph has larger Y values than the bar graph and it therefore gets cutoff. I would like to be able to detect which YLim should be used and make them both the same. How can this be done?
Current code is below.
Thank you!
figure;
h_bar = bar(_data(:,1),Y);
set(gca, 'XTick',_data(:,1));
set(gca, 'XTickLabel', _data(:,1));
h1 = gca;
h2 = axes('Position',get(h1,'Position'));
plot(_data(:,1),Y2,'LineWidth',2,'Color','red')
set(h2,'YAxisLocation','right','Color','none','XTickLabel',[])
set(h2,'XLim',get(h1,'XLim'),'Layer','top')
set(h2,'YLim',get(h1,'YLim'),'Layer','top')
0 Comments
Answers (1)
Matt Tearle
on 13 Oct 2011
figure;
h_bar = bar(_data(:,1),Y);
set(gca, 'XTick',_data(:,1));
set(gca, 'XTickLabel', _data(:,1));
h1 = gca;
h2 = axes('Position',get(h1,'Position'));
plot(data,Y2,'LineWidth',2,'Color','red')
set(h2,'YAxisLocation','right','Color','none','XTickLabel',[])
set(h2,'XLim',get(h1,'XLim'),'Layer','top')
% Query both axes' y-limits
yl = [get(h1,'YLim');get(h2,'YLim')];
% Find the lower and upper limits of both
yl = [min(yl(:,1)),max(yl(:,2))];
% Set y-limits for both axes
set(h1,'YLim',yl)
set(h2,'YLim',yl)
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!