Change size of plotyy figure before saving.

5 ビュー (過去 30 日間)
Brian
Brian 2014 年 10 月 21 日
コメント済み: Michael Haderlein 2014 年 10 月 21 日
Hi, I would like to change the size of a "plotyy" plot and save it as a eps file. Having the following figure:
h = figure(1)
[ax,p1,p2] = plotyy(aElem,z1,aElem,z2,'plot');
set(p1,'LineStyle','*','Color','k');
set(p2,'LineStyle','o','Color','k');
set(ax,{'yColor'},{'k';'k'})
set(ax,'xColor','k')
grid on
xl = xlabel(ax(2),'Crack length [mm]');
I would like to reduce the figure size before saving. The challenge for me is to do it without cutting off some of the x label text. I usually do the following for normal plots with "plot":
figWidth = 10.0; %cm
figHeight = 7.2; %cm
pixelsCm = 1680/47.5; % pixels per cm on my computer
set(gca, 'Units', 'centimeters'); % axes definitions are in cm
lpos = get(gca, 'OuterPosition'); % Gets outer position of the axes
set(gca, 'OuterPosition', [lpos(1)-0.2 lpos(2)-0.2 figWidth-1 figHeight]); % Reduce margins. [left bottom width height] are normalized to [0, 1]. Use negative values for left and bottom to trim off left/bottom margins.
set(gcf, 'PaperPositionMode', 'manual'); % Lets you setup figure size manually so that 'PaperPosition' defines size of exported grapics.
set(gcf, 'Units', 'pixels', 'Position',[10*pixelsCm 10*pixelsCm figWidth*pixelsCm figHeight*pixelsCm]); % position on screen and size of figure. The pixelsCm lets you print out in the true size on screen.
set(gcf, 'Units', 'centimeters','PaperPosition', [1 1 figWidth figHeight]); % Sets the size of the figure for the saved eps file.
and then save the figure with:
saveas(h,'figurename.eps', 'psc2')
But this only changes one of the axes. Does anyone know how to change both the same?
Thanks in advance.
Regards Brian

採用された回答

Michael Haderlein
Michael Haderlein 2014 年 10 月 21 日
編集済み: Michael Haderlein 2014 年 10 月 21 日
You need the handles of both axes. You already use these handles as first output argument of plotyy in a variable you call ax:
[ax,p1,p2] = plotyy(aElem,z1,aElem,z2,'plot');
...
set(ax,{'yColor'},{'k';'k'})
So, later in your code, you should simply use ax instead of gca and that's it.
Edit:
In this line:
lpos = get(gca, 'OuterPosition');
you might want to keep gca or use ax(1). Otherwise you'll get a cell and things get complicated with no need.
  3 件のコメント
Brian
Brian 2014 年 10 月 21 日
I changed some more following your idea and now it works. Thanks for the answer. The code now looks like:
clc; clear all;close all;
h = figure(1);
z1 = 1:100;
z2 = z1.^2;
x = 1:100;
[ax,p1,p2] = plotyy(x,z1,x,z2,'plot');
set(p1,'Marker','*','Color','k');
set(p2,'Marker','o','Color','k');
set(ax,{'yColor'},{'k';'k'})
set(ax,'xColor','k')
grid on
xl = xlabel(ax(2),'x label');
figWidth = 10.0; %cm
figHeight = 7.2; %cm
pixelsCm = 1680/47.5; % pixels per cm on my computer
set(ax, 'Units', 'centimeters'); % axes definitions are in cm
lpos = get(ax(1), 'OuterPosition'); % Gets outer position of the axes
set(ax, 'OuterPosition', [lpos(1)-0.2 lpos(2)+0.5 figWidth-1 figHeight]); % Reduce margins. [left bottom width height] are normalized to [0, 1]. Use negative values for left and bottom to trim off left/bottom margins.
set(h, 'PaperPositionMode', 'manual'); % Lets you setup figure size manually so that 'PaperPosition' defines size of exported grapics.
set(h, 'Units', 'pixels', 'Position',[10*pixelsCm 10*pixelsCm figWidth*pixelsCm figHeight*pixelsCm]); % position on screen and size of figure. The pixelsCm lets you print out in the true size on screen.
set(h, 'Units', 'centimeters','PaperPosition', [1 1 figWidth figHeight]); % Sets the size of the figure for the saved eps file.
saveas(h,'tester.eps', 'psc2')
Michael Haderlein
Michael Haderlein 2014 年 10 月 21 日
You're welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by