Bar graph with Two Y-axes

In the code'
x = 1:length(A); bar(x,A) set(gca,'XTick',x,'XTickLabel',B)
If you add another column of data to (A) which requires a different scale, how do you get a second Y-Axis? Plotyy only seems to work with numeric data for X and Y axes.

Answers (1)

Jan
Jan on 15 Mar 2011
PLOTYY is the correct way: I'd expect, that this works as wanted:
plotyy(1:10, rand(1, 10), (1:10) - 0.1, rand(1,10), ...
@bar, @bar);
Do problems occur with this method? Which problems? What does "PLOTYY seems to work with numeric data only" means exactly? I guess, that your problem is concealed in this sentence. Please edit your question and add these necessary details.
To adjust the functionality of PLOTYY look in the code to see, how the 2nd AXES object is added there. Example (please adjust this for your BAR plots by yourself):
figure('nextplot', 'add');
Axes1H = axes;
plot(1:100, rand(1, 100), 'Parent', Axes1H);
Axes2H = axes('Color', 'none', ...
'XGrid', 'off', 'YGrid', 'off', 'box', 'off', ...
'Position', get(Axes1H, 'Position'), ...
'XLim', get(Axes1H, 'XLim'), ...
'YAxisLocation', 'right');
plot(1:100, rand(1, 100), 'Color', 'r', 'Parent', Axes2H);

1 Comment

Zeeshan Ahmad
Zeeshan Ahmad on 28 Sep 2017
Edited: Zeeshan Ahmad on 28 Sep 2017
Can you update this answer now considering plotyy is deprecated?

Sign in to comment.

Categories

Asked:

on 15 Mar 2011

Edited:

on 28 Sep 2017

Community Treasure Hunt

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

Start Hunting!