Plot y axis on left and right, displaced x axis

17 views (last 30 days)
Martin
Martin on 2 Dec 2015
Answered: Yuebin Zhou on 7 Dec 2015
Hi
I want to display on a plot on both sides the y axis labels. I went through some advices found here and it works on the small preview plotted by Matlab. But when I want to maximize the window to my screen size, the x axis are displaced. How can I fix the position of the two x axis, so that they dont move when the size of the window is modified?
This shows the result given by Matlab, when the window is not resized.
This is the result when I resize it to my screen size.
Here is the code that I used to try this:
figure(1);
x_vect=linspace(7,22,16);
ax1= gca;
ax1.XLim=[7 22];
ax1.YLim=[0 sum_av_dist_sorted(end,1)];
ax1.YTick=sum_av_dist_sorted;
ax1.YTickLabel=Libelle_sort;
ax1.XTick=x_vect;
ax1.FontSize =8.5;
ax1.TitleFontSizeMultiplier = 1.3;
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,'XAxisLocation','bottom','YAxisLocation','right','Color','none');
set(ax2,'XLim',get(ax1,'XLim'),'YLim',get(ax1,'YLim'));
set(ax2,'XTick',get(ax1,'XTick'),'YTick',get(ax1,'YTick'));
set(ax2,'XTickLabel',get(ax1,'XTickLabel'),'YTickLabel',get(ax1,'YTickLabel'));
ax2.FontSize =8.5;
hold on
for i=1:92
% some more code ...
plot(time_bus,dist_bus,'Color',[0.00596 0.40861 0.88284]);
hold on
end
Thanks for any idea to solve this.

Answers (1)

Yuebin Zhou
Yuebin Zhou on 7 Dec 2015
There is a built-in MATLAB function 'plotyy' which would enable y axes on both sides.

Community Treasure Hunt

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

Start Hunting!