【问题标题】:Multiple y axes in MATLAB (axis alignment issue when printing)MATLAB 中的多个 y 轴(打印时的轴对齐问题)
【发布时间】:2016-04-19 08:16:12
【问题描述】:

这是一个奇怪问题的示例。我想绘制具有多个 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 轴在底部确实没有对齐)。

这仅在我使用第二个轴时发生。没有它,这一切都会完美运行。知道为什么吗?

【问题讨论】:

  • stackoverflow.com/questions/30524322/… - 我认为这可能是一个常见的“错误”...
  • 为什么不直接保存一个pdf文件,为什么不保存一个*.eps文件然后转换成pdf呢?这当然是一种解决方法,但请尝试一下
  • @16per9 当然,我手动将其保存为 .png,然后暂时使用它。我不确定是否有一种自动的方法可以保存为 png,然后在 MATLAB 中转换为 pdf,这就是我所追求的(最终用户应该能够一键获得输出 pdf)
  • mathworks.com/help/matlab/ref/print.html - 这允许您直接保存您的图。不确定pdf转换

标签: matlab plot


【解决方案1】:

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

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 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2018-11-25
    相关资源
    最近更新 更多