help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Saving a resized plot


From: blindcurrent
Subject: Saving a resized plot
Date: Thu, 26 Mar 2020 09:54:21 +0000 (UTC)

Hello Octave users,

I am trying to save a plot as a graphics file. This works in general, but not 
correctly when I resize the plot.

The code I have written loads the data to be plotted from separate files, 
creates a plot and changes some of its properties before saving it as a 
graphics file. I copied the code below, except the part where the data is 
assigned to the variables and manipulated.

The part which I think may create the trouble is the command
set(gcf,'position',[get(gcf,'position')(1:3) 250]);
with which I want to set the figure height to 250. So when I run the code, the 
figure window that is created looks like in the attached file window1.png. 
Resizing the figure obviously worked, but chart is not resized to fit into the 
smaller window.

Then when I drag the figure window by its title bar with the mouse, the chart 
changes size at once and looks like in window2.png. This is exactly what I 
want. Unfortunately, executing the command,
print('-dpng','-r600',imagefilename)
either before or after dragging the figure window, yields pngfile.png in both 
cases. The complete plot is saved (unlike shown in the figure window before 
moving it), but it has the standard height, not as depicted in window2.png.

I have used the command
get(gcf,'position')
and got
300  203  560  417
for the originally created figure window (no idea where the 417 comes from), and
308  208  560  250
after dragging the window, which what I expect. The problem is that the saved 
png file has not the right aspect ratio and seems to be entirely unaffected by 
the 250 pixel height.

Does anyone have an idea why that is and how to fix it?


The code:

col_re=[255 0 0]/255;  % definitions of colors
col_bl=[0 112 192]/255;
col_gr=[0 176 80]/255;

col1=col_bl;
col2=col_re;
col3=col_gr;

tmin=0;
tmax=1200;

i_div=200;
i_max=1000;
v_div=100;
v_max=500;

caption='caption';
imagefilename='pngfile.png';


% [part that loads data to the variables i1, i2, i3 and c1, c2, c3]


figure
[AX,Ha,Hb]=plotyy([tmin tmax],[0 0], [tmin tmax],[0 0]);
set(gcf,'position',[get(gcf,'position')(1:3) 250]);    % this is where the plot 
is resized
set(AX,'position',[0.12 0.17 0.76 0.7]);     % axes are set so that the labels 
and caption fit into the plot
set(Ha,'color','k')
set(Hb,'color','k')
hold on
grid on

H1=plot(i1(:,1)*1e9,i1(:,2),'parent',AX(1),'color',col1,'linestyle',':','linewidth',2);
H2=plot(i2(:,1)*1e9,i2(:,2),'parent',AX(1),'color',col2,'linestyle',':','linewidth',2);
H3=plot(i3(:,1)*1e9,i3(:,2),'parent',AX(1),'color',col3,'linestyle',':','linewidth',2);

plot(c3(:,1)*1e9,c3(:,2),'parent',AX(2),'color',col3,'linewidth',2);
plot(c2(:,1)*1e9,c2(:,2),'parent',AX(2),'color',col2,'linewidth',2);
plot(c1(:,1)*1e9,c1(:,2),'parent',AX(2),'color',col1,'linewidth',2);

axes(AX(1)); axis([tmin tmax 0 i_max])
axes(AX(2)); axis([tmin tmax 0 v_max])
set(AX(1),'Ytick',0:i_div:i_max)
set(AX(2),'Ytick',0:v_div:v_max)
set(AX,'tickdir','out')
set(AX,'linewidth',1.5)
set(AX,'gridalpha',.2)
set(AX,'box','off')
set(AX(1),'Xticklabel',[])
xlabel('t/ns')
set(get(AX(1),'Ylabel'),'String','i_C/A')
set(get(AX(2),'Ylabel'),'String','v_{CE}/V')
fontsize=12;
set(AX(1),'ycolor','k','FontName','Arial','FontSize',fontsize)
set(AX(2),'ycolor','k','FontName','Arial','FontSize',fontsize)
set(findall(gcf,'type','text'),'FontName','Arial','FontSize',fontsize,'fontWeight','bold')
title(AX(1),caption)

print('-dpng','-r600',imagefilename)    % is either executed in the script or 
manually in the command window



Some technical information:
I am using Octave 5.2.0, installed using the 64 bit installer file, in Windows 
7. However, I had version 4.2.0 installed until yesterday and the same thing 
happened there.

Attachment: window1.PNG
Description: PNG image

Attachment: window2.PNG
Description: PNG image

Attachment: pngfile.png
Description: PNG image


reply via email to

[Prev in Thread] Current Thread [Next in Thread]