MATLAB中的多个y轴(打印时轴对齐问题)

时间:2016-04-19 08:16:12

标签: matlab plot

以下是一个奇怪问题的示例。我想绘制具有多个y轴的曲线,我在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 to all be the same
    set(gcf,'PaperUnits','inches');
    set(gcf,'Units','inches');


    % Save the pdf
    print -dpdf Test.pdf;

    % Restore the previous settings
    set(gcf,'PaperType',prePaperType);
    set(gcf,'PaperUnits',prePaperUnits);
    set(gcf,'Units',preUnits);
    set(gcf,'PaperPosition',prePaperPosition);
    set(gcf,'PaperSize',prePaperSize);    
end

目标是打印图形的PDF并将其保存在与Test.pdf相同的文件夹中。这已完成,但轴未对齐。在我的Windows机器上它看起来很糟糕,而在Mac上它看起来几乎没问题(但如果仔细观察,y轴确实在底部未对齐)。

仅当我使用第二轴时才会发生这种情况。没有它,所有这一切都完美无缺。知道为什么吗?

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了一个方法:诀窍是使用plotyy。下面的示例代码

cat ~/.ssh/id_watcher.pub | ssh -p 29418 host_ip gerrit create-account --ssh-key - watcher