2016-04-19 7 views
2

これは、奇妙な問題のサンプルです。私は、複数のy軸を持つ曲線をプロットしたいと思います。私はMATLABでかなり一般的な方法を使用します。MATLABの複数のy軸(印刷時の軸合わせの問題)

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上では大丈夫ですが、Mac上では(y軸は真っ直ぐに見えますが、下側が真っ直ぐに見えます)、恐ろしいようです。

これは、2番目の軸を使用する場合にのみ発生します。それがなければ、これはすべて完璧に実行されます。どんな考え?

+0

http://stackoverflow.com/questions/30524322/matlab-add-additional-y-axis-linearly-scaled-to-original-and-print-to-pdf-w?rq以下のサンプル・コード= 1 - これは一般的な "エラー"の可能性があります... – 16per9

+0

なぜ、直接PDFファイルを保存するのではなく、なぜ* .epsファイルを保存してpdfに変換しないのですか?これはもちろん回避策ですが、試してみてください – 16per9

+0

@ 16per9確かに、私は手動で.pngとして保存してから、今使っています。私はpngとして保存してMATLABのpdfに変換する自動化された方法があるかどうか分かりません。それは私が何をしたのかです(エンドユーザはワンクリックで出力PDFを得ることができます) – user1936752

答えて

0

私は方法を見つけました。そのトリックは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 
関連する問題