Another option is to create new right-axis tick positions to match the tick positions on the left:
x1=[200, 300, 250, 520, 340];
x2=[0.3, 0.2, 0.12, 0.4, 0.22];
figure;
yyaxis left
bar(x1)
ytl = get(gca, 'YTick');
yyaxis('right');
plot(x2)
ytr = get(gca, 'YTick');
ytrv = linspace(min(ytr), max(ytr), numel(ytl));
ytrc = compose('%.2f',ytrv);
set(gca, 'YTick',ytrv, 'YTickLabel',ytrc)
grid on
producing:
.
EDIT — (1 Dec 2020 at 17:48)
To get ticks and tick labels with more rational spacing:
x1=[200, 300, 250, 520, 340];
x2=[0.3, 0.2, 0.12, 0.4, 0.22];
figure;
yyaxis left
bar(x1)
ytl = get(gca, 'YTick');
yyaxis('right');
plot(x2)
ytr = get(gca, 'YTick');
tinc = diff(ylim)/numel(ytl);
ytrv = min(ylim):tinc:(tinc*(numel(ytl)+1));
ylim([min(ytrv) max(ytrv)])
ytrc = compose('%.2f',ytrv);
set(gca, 'YTick',ytrv, 'YTickLabel',ytrc)
grid on
producing:
.
Best Answer