plot - Multiple y axes in MATLAB (axis alignment issue when printing) -
here sample of strange problem. i'd plot curves multiple y axes , use common method in matlab.
function plot_test() clear; savefile = 1; scrsz = get(0,'screensize'); figure('position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]); hold on; box on; x = 0:1:10; h1 = plot(x, x.^2 , 'r', 'linewidth', 2.5); %axis label xlabel('xlabel', 'fontsize', 20, 'interpreter', 'latex'); ylabel('ylabel', 'fontsize', 20, 'interpreter', 'latex'); set(gca, 'fontsize', 20, 'linewidth', 3); ax1 = gca; ax2 = axes('position',get(ax1,'position'),'xaxislocation','top','yaxislocation','right','color','none','xcolor','none','ycolor','k'); linkaxes([ax1 ax2],'x'); hold on box on; h2 = plot(x, x, 'b', 'parent', ax2, 'linewidth', 2.5); ylabel('second ylabel', 'fontsize', 20, 'interpreter', 'latex'); set(gca, 'fontsize', 20, 'linewidth', 3); hl=legend([h1 h2],{'first line','second line'}); set(hl,'fontsize',15,'location','northwest', 'orientation','vertical') %save pdf if savefile % backup previous settings prepapertype = get(gcf,'papertype'); prepaperunits = get(gcf,'paperunits'); preunits = get(gcf,'units'); prepaperposition = get(gcf,'paperposition'); prepapersize = get(gcf,'papersize'); % make changing paper type possible set(gcf,'papertype','<custom>'); % set units same set(gcf,'paperunits','inches'); set(gcf,'units','inches'); % save pdf print -dpdf test.pdf; % restore previous settings set(gcf,'papertype',prepapertype); set(gcf,'paperunits',prepaperunits); set(gcf,'units',preunits); set(gcf,'paperposition',prepaperposition); set(gcf,'papersize',prepapersize); end
the objective print pdf of figure , save in same folder test.pdf. accomplished axes misaligned. on windows machine looks horrible while on mac looks okay (but if closely, y-axes indeed misaligned @ bottom).
this happens when use second axis. without that, runs perfectly. idea why?
okay, found way: trick use plotyy. sample code below
function plot_test2() clear; savefile = 1; scrsz = get(0,'screensize'); figure('position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]); hold on; box on; x=(0:1:10); y1=x; y2=x.^2; [hax, hline1, hline2] = plotyy(x,y1,x,y2); %axis label xlabel(hax(1),'xlabel', 'fontsize', 20, 'interpreter', 'latex', 'color', 'k'); ylabel(hax(1),'ylabel', 'fontsize', 20, 'interpreter', 'latex', 'color', 'k'); ylabel(hax(2),'second ylabel', 'fontsize', 20, 'interpreter', 'latex'); set(hax,{'ycolor'},{'k';'k'}) set(hax,{'fontsize'},{20;20}, {'linewidth'}, {3;3}) set(hline1,'linewidth', 3) set(hline2,'linewidth', 3) set(hline1,'color', 'r') set(hline2,'color', 'b') hl=legend([hline1 hline2],{'first line','second line'}); set(hl,'fontsize',15,'location','northwest', 'orientation','vertical') %save pdf if savefile % backup previous settings prepapertype = get(gcf,'papertype'); prepaperunits = get(gcf,'paperunits'); preunits = get(gcf,'units'); prepaperposition = get(gcf,'paperposition'); prepapersize = get(gcf,'papersize'); % make changing paper type possible set(gcf,'papertype','<custom>'); % set units same set(gcf,'paperunits','inches'); set(gcf,'units','inches'); % save pdf print -dpdf test.pdf; % restore previous settings set(gcf,'papertype',prepapertype); set(gcf,'paperunits',prepaperunits); set(gcf,'units',preunits); set(gcf,'paperposition',prepaperposition); set(gcf,'papersize',prepapersize); end
Comments
Post a Comment