Discussion:
Plotting and Animating in Octave (vs. Matlab)
Jeff
2013-07-16 01:15:47 UTC
Permalink
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?

Here's what I'm starting with (from Matlab). First, a plain old plot:
try close(2); end
figure(2); h=axes; hold on;
col=1;

plot(Hn_f(:,col),'b-');
if N/2 >= 4, plot(Hn_f(:,col+2),'r-'); end
if N/2 >= 6, plot(Hn_f(:,col+4),'g-'); end
if N/2 >= 8, plot(Hn_f(:,col+6),'k-'); end

axis tight;
strTitle = ['Energy per Mode over Time (%d Masses, Beta=%.2f, %s BC)'];
strTitle=sprintf(strTitle, N, beta, boundary);
title(strTitle,'fontweight','b','FontSize',12);
xlabel('Time','fontweight','b');
ylabel('Energy','fontweight','b');
legend('Mode 1','Mode 3','Mode 5','Mode 7');
legend('Location','North');
hold off;

And an animation. I can get a few successive text-based plots from this
one, but then it just stops executing. In Matlab, I create a movie
uncommenting the lines related to structure "M". Can I at least create a
movie in Octave?
rnge=(round(max(max(abs(U)))*10)+1)/10;
xs=[0, N+1, 0, N+1, -rnge, rnge];
txt=sprintf('%dx%d Lattice, %s BC, beta=%.2f', N, N, boundary, beta);
strXlbl='Mass No.'; % Also Ylabel.
strZlbl='Displacement';

clear M;
figure(2);
% M = struct('cdata', cell(1,T+1), 'colormap', cell(1,T+1));

for tt=0:T
mesh(vertcat(zeros(1,N+2), horzcat(zeros(N,1), ...
reshape(U(tt+1,:),N,N), zeros(N,1)), zeros(1,N+2)));
axis(xs);
title(sprintf([txt ', t=%d (of %d)'],tt, T));
drawnow;

% M(tt+1)=getframe(h);
end
hold off;
% txt=sprintf('N=%02dx%02d_Beta=%.2f.avi', N, N, beta)
% tic; movie2avi(M, txt); toc;

Thanks for your help.
PhilipNienhuis
2013-07-16 17:37:34 UTC
Permalink
Post by Jeff
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?
try close(2); end
getframe.m <http://octave.1599824.n4.nabble.com/file/n4655707/getframe.m>
figure(2); h=axes; hold on;
col=1;
plot(Hn_f(:,col),'b-');
if N/2 >= 4, plot(Hn_f(:,col+2),'r-'); end
if N/2 >= 6, plot(Hn_f(:,col+4),'g-'); end
if N/2 >= 8, plot(Hn_f(:,col+6),'k-'); end
axis tight;
strTitle = ['Energy per Mode over Time (%d Masses, Beta=%.2f, %s BC)'];
strTitle=sprintf(strTitle, N, beta, boundary);
title(strTitle,'fontweight','b','FontSize',12);
xlabel('Time','fontweight','b');
ylabel('Energy','fontweight','b');
legend('Mode 1','Mode 3','Mode 5','Mode 7');
legend('Location','North');
hold off;
And an animation. I can get a few successive text-based plots from this
one, but then it just stops executing. In Matlab, I create a movie
uncommenting the lines related to structure "M". Can I at least create a
movie in Octave?
rnge=(round(max(max(abs(U)))*10)+1)/10;
xs=[0, N+1, 0, N+1, -rnge, rnge];
txt=sprintf('%dx%d Lattice, %s BC, beta=%.2f', N, N, boundary, beta);
strXlbl='Mass No.'; % Also Ylabel.
strZlbl='Displacement';
clear M;
figure(2);
% M = struct('cdata', cell(1,T+1), 'colormap', cell(1,T+1));
for tt=0:T
mesh(vertcat(zeros(1,N+2), horzcat(zeros(N,1), ...
reshape(U(tt+1,:),N,N), zeros(N,1)), zeros(1,N+2)));
axis(xs);
title(sprintf([txt ', t=%d (of %d)'],tt, T));
drawnow;
% M(tt+1)=getframe(h);
end
hold off;
% txt=sprintf('N=%02dx%02d_Beta=%.2f.avi', N, N, beta)
% tic; movie2avi(M, txt); toc;
The last Octave version (for Windows) that had a somewhat functional[1]
video package included was 3.4.3 for MinGW. A function getframe() is still
lacking so I clumsily used a getframe.m (attached) comprising plotting to
file, reading that using imread, clipping to even numbers (plus some more
apparently required polishing) and adding the result to the movie using
addframe().
It didn't look quite as sleek as movies from Matlab :-(
Yet I still have 3.4.3 for MinGW around, as I occasionaly need / want to
make movies.

The video package could use some attention...

Alternatively, you might write successive GIF files and use 3rd party tools
to combine them into an animated GIF.

Philip

[1] That it builds and has been included in some binaries doesn't imply that
it really works




--
View this message in context: http://octave.1599824.n4.nabble.com/Plotting-and-Animating-in-Octave-vs-Matlab-tp4655680p4655707.html
Sent from the Octave - General mailing list archive at Nabble.com.
PhilipNienhuis
2013-07-16 17:40:37 UTC
Permalink
Post by Jeff
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?
try close(2); end
getframe.m <http://octave.1599824.n4.nabble.com/file/n4655707/getframe.m>
Hmm the link ended somewhere in the quoted text.... (Nabble issue)

Philip



--
View this message in context: http://octave.1599824.n4.nabble.com/Plotting-and-Animating-in-Octave-vs-Matlab-tp4655680p4655708.html
Sent from the Octave - General mailing list archive at Nabble.com.
Ron.Simonson
2013-07-16 17:52:40 UTC
Permalink
Post by Jeff
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?
Have you considered writing the successive images to sequential files
and then use the "animate" program (part of ImageMagick?)
Jeff
2013-07-16 18:27:55 UTC
Permalink
Hi Ron. I have not considered that because 1) I do not know how to write
images to files, 2) I don't know how to write to a "sequential" file (and I
only sort of get what you mean by a "sequential file", and 3) I've never
heard of ImageMagick.

I will research ImageMagick tonight. If it's free (I suspect yes) I will
try it out. Could you give me a hint how to write successive images to a
file? A list of commands I need to read up on should be adequate (a small
code snippet would be ideal).
Post by Jeff
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?
Have you considered writing the successive images to sequential files and
then use the "animate" program (part of ImageMagick?)
______________________________**_________________
Help-octave mailing list
https://mailman.cae.wisc.edu/**listinfo/help-octave<https://mailman.cae.wisc.edu/listinfo/help-octave>
Dmitri A. Sergatskov
2013-07-16 19:23:31 UTC
Permalink
Post by Jeff
Hi Ron. I have not considered that because 1) I do not know how to write
images to files, 2) I don't know how to write to a "sequential" file (and I
only sort of get what you mean by a "sequential file", and 3) I've never
heard of ImageMagick.
I will research ImageMagick tonight. If it's free (I suspect yes) I will
try it out. Could you give me a hint how to write successive images to a
file? A list of commands I need to read up on should be adequate (a small
code snippet would be ideal).
Here is an example:

t=linspace(0,10,1001);
for ii=1:100
w = 2*pi*0.1*ii;
plot(t,sin(w*t),";;")
fname = sprintf("anim_%04i.png", ii);
tstring = sprintf("Frequency = %4.2f Hz", 0.1*ii);
title(tstring)
xlabel("Time")
ylabel("Amplitude")
print (fname, "-dpngcairo")
endfor

====

After you ran this code in octave in your current directory you will have
100 files
with names like
anim_0001.png
to
anim_0100.png
Those are sequential frames of your animation.

You can now either look at them with
animate -delay 10 anim_*.png

or create some stand-alone movie file with e.g. 'mencoder'

mencoder mf://anim_*.png -mf fps=15:type=png -ovc copy -o anim_mng.avi

Dmitri.
--
Francesco Potortì
2013-07-17 08:19:19 UTC
Permalink
Post by Ron.Simonson
Post by Jeff
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?
Have you considered writing the successive images to sequential files
and then use the "animate" program (part of ImageMagick?)
On the same line, I have used this program of mine to generate a movie:


===File /home/work/math/octavelib/utils/movie.m=============
## Copyright (C) 2008, 2009 Francesco Potortì

## -*- texinfo -*-
## @deftypefn {Function File} {} movie (@var{action})
## @deftypefnx {Function File} {} movie (@var{action}, @var{mvf})
## @deftypefnx {Function File} {} movie (@var{action}, @var{mvf}, @var{rate})
## Create a movie from plots
##
## Example usage:
## @example
## figure("visible","off"); movie("init","square.mp4")
## n=100; a=zeros(n,n); a(1:20,41:60)=1;
## for i=1:n; imshow(shift(a,i)); movie("add","square.mp4"); endfor
## movie("close","square.mp4",24); close; system("totem square.mp4")
## @end example
##
## @var{action} takes one of three string values: @code{init},
## @code{add}, @code{close}. For each, @var{mvf} specifies the file name
## produced; the file name suffix sets the type of movie.
##
## If @var{mvf} is missing, it defaults to @file{octave_movie.mp4}. The
## suffix @file{.dir} creates a directory containing a png file per
## frame, the type @file{.zip} archives it using @command{zip}. A
## suffix of @file{.mp4}, @file{.ogg}, @file{.mov}, @file{.mjpeg},
## @file{.avi}, @file{.flv} creates a movie using @command{ffmpeg};
## @file{.mng}, @file{.gif} create a movie using @command{convert}; type
## @file{.swf} creates a movie using @command{png2swf}. You must have
## the relevant program installed when using a given extension; no
## program is required for @file{.dir}.
##
## With the @var{close} action, a third arguments specifies the frame
## rate (defaulting to 5 frames/second).
##
## @end deftypefn

## Author: Francesco Potortì <***@isti.cnr.it>
## Revision: 1.12
## License: GPL version 3 or later

function movie (action, mvf='octave_movie.mp4', rate=5)

verbose = false;

actions = {'init' 'add' 'close'};
# gif swf
types = {'.mp4' '.mng' '.gif' '.zip' '.ogg' '.swf' '.mov' ...
'.mjpeg' '.avi' '.flv' '.dir'};
if (nargin < 1 || !ischar(action) || !any(strcmp(action, actions)))
error("first argument must be one of:%s", sprintf(" %s",actions{:}));
endif
if (nargin >= 2 && !ischar(mvf))
error("second arg must be a string");
endif
[mpath mname mtype] = fileparts(mvf);

mdir = fullfile(mpath, [mname '.d']);
ppat = '%06d.png';
mpat = fullfile(mdir, ppat);
mglob = fullfile(mdir, strrep(sprintf(ppat,0),'0','[0-9]'));
fnof = fullfile(mdir, "+frame-number+");

switch (action)
case actions{1} # init a movie
if (isdir(mvf))
cleandir(mvf, verbose)
else
unlink(mvf);
endif
while (!([allgood msg] = mkdir(mdir)))
if (stat(fnof) && load(fnof).frameno == 0)
error("while creating dir '%s': %s", mdir, msg);
else
cleandir(mdir, verbose);
endif
endwhile
frameno = 0; save('-text',fnof,'frameno');
if (verbose) printf("Directory '%s' created.\n", mdir); endif
case actions{2} # add a frame
load(fnof);
mfile = sprintf(mpat, ++frameno);
print(mfile,'-dpng');
save('-text',fnof,'frameno');
if (verbose) printf("Frame '%s' added.\n", mfile); endif
case actions{3} # close the movie
switch (mtype)
case {types{[1 5 7 8 9 10]}} # mp4, ogg, mov, mjpeg, avi, flv
cmd = sprintf("ffmpeg -y -r %d -sameq -i %s %s 2>&1", rate, mpat, mvf);
case {types{[2 3]}} # mng, gif
cmd = sprintf("convert %s -adjoin %s %s 2>&1", mglob, mpat, mvf);
case types{4} # zip
cmd = sprintf("zip -qr9 %s %s 2>&1", mvf, mglob);
case types{6} # swf
cmd = sprintf("png2swf -z -r %d -o %s %s", rate, mvf, mglob);
case types{end} # dir
rename(mdir, mvf); return
otherwise
error("second arg must end with one of:%s", sprintf(" %s",types{:}));
endswitch
[status output] = system(cmd);
if (status != 0)
load(fnof);
error("Creation of movie '%s' containing %d frames failed:\n%s",
mvf, frameno, output);
endif
if (verbose) printf("Movie '%s' contains %d frames:\n%s",
mvf, frameno, output); endif
cleandir(mdir, verbose);
endswitch
endfunction


function cleandir(mdir, verbose)
unwind_protect
save_crr = confirm_recursive_rmdir(false);
[allgood msg] = rmdir(mdir,"s");
if (!allgood)
error("while removing dir '%s': %s", mdir, msg); endif
unwind_protect_cleanup
confirm_recursive_rmdir(save_crr);
end_unwind_protect
if (verbose) printf("Directory '%s' removed\n", mdir); endif
endfunction
============================================================
--
Francesco Potortì (ricercatore) Voice: +39.050.621.3058
ISTI - Area della ricerca CNR Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa Skype: wnlabisti
(entrance 20, 1st floor, room C71) Web: http://fly.isti.cnr.it
Jeff
2013-07-16 18:24:34 UTC
Permalink
Thanks, Philip. I will look at your code later tonight.

I did forget to mention that I am using GNU Octave, version 3.4.3 on Gentoo
Linux (but I will have to research MinGW to determine if I'm using that or
not).
Post by PhilipNienhuis
Post by Jeff
I'm trying to make some plots and animations in Octave on a non-graphical
cluster. Is this even possible? With the first code, I can get a rough,
text-based plot. Is that the best I can do? Is there a way to animate (my
second code)? If not animate, can I at least create a movie?
try close(2); end
getframe.m <http://octave.1599824.n4.nabble.com/file/n4655707/getframe.m
figure(2); h=axes; hold on;
col=1;
plot(Hn_f(:,col),'b-');
if N/2 >= 4, plot(Hn_f(:,col+2),'r-'); end
if N/2 >= 6, plot(Hn_f(:,col+4),'g-'); end
if N/2 >= 8, plot(Hn_f(:,col+6),'k-'); end
axis tight;
strTitle = ['Energy per Mode over Time (%d Masses, Beta=%.2f, %s BC)'];
strTitle=sprintf(strTitle, N, beta, boundary);
title(strTitle,'fontweight','b','FontSize',12);
xlabel('Time','fontweight','b');
ylabel('Energy','fontweight','b');
legend('Mode 1','Mode 3','Mode 5','Mode 7');
legend('Location','North');
hold off;
And an animation. I can get a few successive text-based plots from this
one, but then it just stops executing. In Matlab, I create a movie
uncommenting the lines related to structure "M". Can I at least create a
movie in Octave?
rnge=(round(max(max(abs(U)))*10)+1)/10;
xs=[0, N+1, 0, N+1, -rnge, rnge];
txt=sprintf('%dx%d Lattice, %s BC, beta=%.2f', N, N, boundary, beta);
strXlbl='Mass No.'; % Also Ylabel.
strZlbl='Displacement';
clear M;
figure(2);
% M = struct('cdata', cell(1,T+1), 'colormap', cell(1,T+1));
for tt=0:T
mesh(vertcat(zeros(1,N+2), horzcat(zeros(N,1), ...
reshape(U(tt+1,:),N,N), zeros(N,1)), zeros(1,N+2)));
axis(xs);
title(sprintf([txt ', t=%d (of %d)'],tt, T));
drawnow;
% M(tt+1)=getframe(h);
end
hold off;
% txt=sprintf('N=%02dx%02d_Beta=%.2f.avi', N, N, beta)
% tic; movie2avi(M, txt); toc;
The last Octave version (for Windows) that had a somewhat functional[1]
video package included was 3.4.3 for MinGW. A function getframe() is still
lacking so I clumsily used a getframe.m (attached) comprising plotting to
file, reading that using imread, clipping to even numbers (plus some more
apparently required polishing) and adding the result to the movie using
addframe().
It didn't look quite as sleek as movies from Matlab :-(
Yet I still have 3.4.3 for MinGW around, as I occasionaly need / want to
make movies.
The video package could use some attention...
Alternatively, you might write successive GIF files and use 3rd party tools
to combine them into an animated GIF.
Philip
[1] That it builds and has been included in some binaries doesn't imply that
it really works
--
http://octave.1599824.n4.nabble.com/Plotting-and-Animating-in-Octave-vs-Matlab-tp4655680p4655707.html
Sent from the Octave - General mailing list archive at Nabble.com.
_______________________________________________
Help-octave mailing list
https://mailman.cae.wisc.edu/listinfo/help-octave
PhilipNienhuis
2013-07-16 18:36:35 UTC
Permalink
Please don't top-post. Answer below the mail so we can easier follow the
discussion.
Read on...
Post by Jeff
Thanks, Philip. I will look at your code later tonight.
I did forget to mention that I am using
*
Post by Jeff
GNU Octave, version 3.4.3 on Gentoo
Linux
*
Post by Jeff
(but I will have to research MinGW to determine if I'm using that or
not).
No MinGW only applies to Windows.

An example of how to write graphics files is shown in the getframe.m I
posted.

After creating a plot, just do (in Octave)

help print

to find out how to proceed.

Philip



--
View this message in context: http://octave.1599824.n4.nabble.com/Plotting-and-Animating-in-Octave-vs-Matlab-tp4655680p4655715.html
Sent from the Octave - General mailing list archive at Nabble.com.
Loading...