Change size of plotyy figure before saving.

2 views (last 30 days)
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

Accepted Answer

Michael Haderlein
Michael Haderlein on 21 Oct 2014
Edited: Michael Haderlein on 21 Oct 2014
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 Comments
Brian
Brian on 21 Oct 2014
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')

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!