Related
I am trying to visualize a small simulation I have to do. For the task, it is not necessary but as it quite often happens: I run into the problem and want the solution before I go on.
The simulation is an ultra-simple neural net from the computational neuroscience department with an 'Oja' algorithm. I have the value plotted as scatterplot and wanted to animate the changing weight over the loops. That for itself works fine (figure 2 or f2 handle)
Then I decided to print the difference in the weight vector from one run to another calculated as the norm. I wanted this to be plotted as a line that evolves over time (figure 1 aka f1). But though I always activate figure 2 it switches back to figure 1 and plots it there.
Of course, I searched the internet as well as stackexchange where btw I found lots of fascinating stuff about animations. Just nothing that solved the problem...
Two questions:
why?
and what do I have to change to make it work?
Thanks.
Here is the code:
function [t, w, dw]=weight(X)
w=rand(2,1)*5; %Initialization
%constants
n=1;
alpha=1;
dt=0.01;
T=5;
L=length(X);
w_old=[0; 0];
s=size(w_old)
t=0;
limit=0.001;
%handles for the figures Error and weight animation
f1= figure
set(f1,'DoubleBuffer','on', 'Name','Error');
ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
f2=figure
set(f2, 'Name', 'weights');
%normalizing and plot
X=[X(:,1)-mean(X(:,1)),X(:,2)-mean(X(:,2))];
scatter(X(:,1),X(:,2));
%function handle for the error and the weights animation
herror = animatedline('Marker','.');
hLine = line('XData',w(1), 'YData',w(2), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(w(1), w(2), sprintf('(%.3f,%.3f)',w(1),w(2)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
while (t<T)
for i=1:L
w_old= w;
u=X(i,:);
v=u*w;
w=w+dt*n*(v*u'-alpha*v^2*w); %Oja rule
figure(f2);
hold on;
set(hLine, 'XData',w(1), 'YData',w(2))
set(hTxt, 'Position',[w(1) w(2)], ...
'String',sprintf('(%.3f,%.3f,%.2f)',[w(1) w(2) t]))
drawnow %# force refresh
%#pause(DELAY)
hold off;
dw=norm(w_old-w);
figure(f1)
hold on;
addpoints(herror, (i*t/dt),dw)
drawnow
hold off;
if dw<limit, break; end
end
t=t+dt;
if ~ishandle(hLine), break; end
end
end
You created a new axes ax1 overlapping to the plot hiding the animation.
You also put herror = animatedline('Marker','.'); after f2=figure doing nothing in the first figure before the for loop. This is the working code:
function [t, w, dw]=weight(X)
X = randn(20,2);
w=rand(2,1)*5; %Initialization
close all
%constants
n=1;
alpha=1;
dt=0.01;
T=5;
L=length(X);
w_old=[0; 0];
s=size(w_old);
t=0;
limit=0.001;
%handles for the figures Error and weight animation
f1= figure(1);
hold on;
set(f1,'DoubleBuffer','on', 'Name','Error');
%ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
%function handle for the error and the weights animation
herror = animatedline('Marker','.');
xlabel('t'); ylabel('Error');
f2=figure(2);
hold on;
set(f2, 'Name', 'weights');
xlabel('W1'); ylabel('W2')
%normalizing and plot
X=[X(:,1)-mean(X(:,1)),X(:,2)-mean(X(:,2))];
scatter(X(:,1),X(:,2));
hLine = line('XData',w(1), 'YData',w(2), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(w(1), w(2), sprintf('(%.3f,%.3f)',w(1),w(2)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
while (t<T)
for i=1:L
w_old= w;
u=X(i,:);
v=u*w;
w=w+dt*n*(v*u'-alpha*v^2*w); %Oja rule
set(hLine, 'XData',w(1), 'YData',w(2))
set(hTxt, 'Position',[w(1) w(2)], ...
'String',sprintf('(%.3f,%.3f,%.2f)',[w(1) w(2) t]))
drawnow %# force refresh
dw=norm(w_old-w);
figure(f1);
addpoints(herror, (i*t/dt),dw)
drawnow
if dw<limit; break; end
end
t=t+dt;
if ~ishandle(hLine), break; end
end
end
To me, it seems more natural using just one window and 2 subplots instead of switching window back and forth.
I have a 576x576x150 matrix. Each 576x576 set represents an image. When I want to plot one frame I do it by using the plot command:
figure(1);
imshow(B(:,:,45),[]) % plots frame 45
title('45') % tells frame number
However I would like to add a slider to the plot, so I can move from 1-150 frame within the figure.I've seen examples of people using uicontrol but I don't know how to code it. In addition to that, I would like to have a title on top of the figure telling me the frame number.
Here is how I do it. I like to keep a single function that does the plotting so you don't recycle commands elsewhere. You could replace the first two lines by function test(B) to use you own B matrix. This code is pretty easy to extend. You will also want to play with the layout to suit your purpose.
function test
B=rand(576,576,150);
fig=figure(100);
set(fig,'Name','Image','Toolbar','figure',...
'NumberTitle','off')
% Create an axes to plot in
axes('Position',[.15 .05 .7 .9]);
% sliders for epsilon and lambda
slider1_handle=uicontrol(fig,'Style','slider','Max',150,'Min',1,...
'Value',2,'SliderStep',[1/(150-1) 10/(150-1)],...
'Units','normalized','Position',[.02 .02 .14 .05]);
uicontrol(fig,'Style','text','Units','normalized','Position',[.02 .07 .14 .04],...
'String','Choose frame');
% Set up callbacks
vars=struct('slider1_handle',slider1_handle,'B',B);
set(slider1_handle,'Callback',{#slider1_callback,vars});
plotterfcn(vars)
% End of main file
% Callback subfunctions to support UI actions
function slider1_callback(~,~,vars)
% Run slider1 which controls value of epsilon
plotterfcn(vars)
function plotterfcn(vars)
% Plots the image
imshow(vars.B(:,:,get(vars.slider1_handle,'Value')));
title(num2str(get(vars.slider1_handle,'Value')));
The idea is to use uicontrol() to enable sliding/scrolling.
The following code is for scrolling (created by Evan Brooks, you can modify it to sliding):
function scrollfigdemo
% create new figure window
f = figure;
set(f,'doublebuffer', 'on', 'resize', 'off')
% set columns of plots
cols = 2;
% create 5 data sets to plot
x=0:1e-2:2*pi;
y{1}=sin(x);
y{2}=cos(x);
y{3}=tan(x);
y{4}=x.^2;
y{5}=x.^3;
% determine required rows of plots
rows = ceil(length(y)/cols);
% increase figure width for additional axes
fpos = get(gcf, 'position');
scrnsz = get(0, 'screensize');
fwidth = min([fpos(3)*cols, scrnsz(3)-20]);
fheight = fwidth/cols*.75; % maintain aspect ratio
set(gcf, 'position', [10 fpos(2) fwidth fheight])
% setup all axes
buf = .15/cols; % buffer between axes & between left edge of figure and axes
awidth = (1-buf*cols-.08/cols)/cols; % width of all axes
aidx = 1;
rowidx = 0;
while aidx <= length(y)
for i = 0:cols-1
if aidx+i <= length(y)
start = buf + buf*i + awidth*i;
apos{aidx+i} = [start 1-rowidx-.92 awidth .85];
a{aidx+i} = axes('position', apos{aidx+i});
end
end
rowidx = rowidx + 1; % increment row
aidx = aidx + cols; % increment index of axes
end
% make plots
axes(a{1}), plot(x,y{1}), title('sine'), xlabel('x'), ylabel('sin(x)')
axes(a{2}), plot(x,y{2}), title('cosine'), xlabel('x'), ylabel('cos(x)')
axes(a{3}), plot(x,y{3}), title('tangent'), xlabel('x'), ylabel('tan(x)')
axes(a{4}), plot(x,y{4}), title('x^2'), xlabel('x'), ylabel('x^2')
axes(a{5}), plot(x,y{5}), title('x^3'), xlabel('x'), ylabel('x^3')
% determine the position of the scrollbar & its limits
swidth = max([.03/cols, 16/scrnsz(3)]);
ypos = [1-swidth 0 swidth 1];
ymax = 0;
ymin = -1*(rows-1);
% build the callback that will be executed on scrolling
clbk = '';
for i = 1:length(a)
line = ['set(',num2str(a{i},'%.13f'),',''position'',[', ...
num2str(apos{i}(1)),' ',num2str(apos{i}(2)),'-get(gcbo,''value'') ', num2str(apos{i}(3)), ...
' ', num2str(apos{i}(4)),'])'];
if i ~= length(a)
line = [line,','];
end
clbk = [clbk,line];
end
% create the slider
uicontrol('style','slider', ...
'units','normalized','position',ypos, ...
'callback',clbk,'min',ymin,'max',ymax,'value',0);
That's how I draw two graphics (thanks for guys who helped me to do that):
clear
logsFolder = 'C:\logs\';
stocks = {'log'};
for stock = stocks
filename = [logsFolder stock{1} '.log'];
fLog = fopen(filename);
data = textscan(fLog, '%f:%f:%f:%f %f %f %f');
fclose(fLog);
% hh:min:sec:millisec
secvec = [60*60 60 1 1e-3];
x = [data{1:4}] * secvec';
y = data{5};
yPrice = data{6};
xindays = x / (24*60*60);
figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
set(AX(1),'xtick',[]);
lo1 = min(y);
hi1 = max(y);
lo2 = min(yPrice);
hi2 = max(yPrice);
if (hi2/lo2 > hi1/lo1)
ylim(AX(1),[lo1 hi2/lo2 * lo1]);
ylim(AX(2),[lo2 hi2]);
else
ylim(AX(1),[lo1 hi1]);
ylim(AX(2),[lo2 hi1/lo1 * lo2]);
end
ticklabelformat(AX(2),'y','%g')
ticklabelformat(AX(2),'x',{#tick2datestr,'x','HH:MM:SS'})
title(stock);
% iNeedToDrawThat = data{7}
end
Input file example is available here As you can see my file contains the last column which I also want to display. The range should be from 0 (at the bottom of figure) to the maximum value (at the up of the graph). So I need to draw three graphics somehow. It's ok to omit axis with labels for the third graph as I already have two axis and I have no place to add third one. However it's ok to "overlap" two axis if possible.
I have no idea how to do that so I'm looking for your help.
I've tried that but it doesn't work:
figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
hold on;
volume = data{7};
plot(xindays, volume);
hold off;
I have already mentioned a similar question in the comments, it should give you plenty of ideas...
Anyway, I've put together a solution to plot multiple y axes. Right now the code is a bit involved, but it should be possible to refactor a re-usable function out of it (like the addaxis function on the File Exchange).
The idea is to first plot each curve in a separate axis (all superimposed), and make them transparent (except the bottom one). Next we create copies of this set of axes and shift them along the x-direction. We also make those copies transparent, but now we can show the tick-marks along the y-axis of each. Finally we give them correct z-order, and link the x and y limits so that we can use the pan and zoom functionality.
%# read and parse data from file
fid = fopen('log.log','rt');
C = textscan(fid, '%s %f %f %f', 'CollectOutput',true);
fclose(fid);
dt = datenum(C{1}, 'HH:MM:SS:FFF');
data = C{2};
NUM = size(data,2);
%# create a wider figure
hFig = figure('Position',get(0,'DefaultFigurePosition').*[1 1 1.7 1]);
%# some properties
clr = lines(NUM);
bgClr = get(0,'DefaultFigureColor');
pos = get(0,'DefaultAxesPosition');
pp = 0.1; % shift in normalized units: pos(1)
%# create plot axes (make axis invisible)
hAx = zeros(NUM,1);
for i=1:NUM
hAx(i) = axes('Parent',hFig, 'Color','none', ...
'XColor',bgClr, 'YColor',bgClr, ...
'Units','normalized', 'Position',pos+[(NUM-1)*pp 0 -(NUM-1)*pp 0]);
line(dt, data(:,i), 'Color',clr(i,:), 'Parent',hAx(i))
end
axis(hAx, 'tight') %# tight x/y limits
%# create shifted copies of axes to show y-ticks
hAxx = zeros(size(hAx));
for i=1:NUM
hAxx(i) = copyobj(hAx(i), hFig);
delete(get(hAxx(i),'Children')); %# keep only axis
set(hAxx(i), 'YColor',clr(i,:), ...
'Units','normalized', 'Position',pos+[(NUM-i)*pp 0 -(NUM-i)*pp 0]);
ylabel(hAxx(i), sprintf('Axis %d',i))
end
xlabel(hAxx(1), 'datetime')
title(hAxx(1), 'Log')
datetick(hAxx(1), 'x', 'HH:MM', 'keeplimits')
%# set 1st axis copy as current axis
set(hFig, 'CurrentAxes',hAxx(1))
%# adjust ticks of axes
set(hAx(1), 'Color','w') %# give white bg to 1st axis
set(hAxx(1), 'XColor','k') %# show xticks of 1st axis copy
set(hAxx(2:end), 'XTick',[], 'XTickLabel',[])
set(hAx, 'XTick',[], 'XTickLabel',[], 'YTick',[], 'YTickLabel',[])
%# fix z-order
for i=3:-1:1, uistack(hAxx(i),'top'), end
uistack(hAx(1), 'bottom')
%# link x/y limits so that panning/zooming works
lh = cell(NUM+1,1);
for i=1:NUM
lh{i} = linkprop([hAxx(i);hAx(i)], 'YLim');
end
lh{end} = linkprop([hAxx;hAx], 'XLim');
The result:
The panning/zooming is a bit funny, you have to initiate them by starting to drag from the side (the shifted colored axes). This is because the first one (corresponding to the blue line) is the one on top, thus catches all mouse clicks.
Note: I see you are using a custom function ticklabelformat, which I haven't tested in combination with the above code. I will leave that part to you..
HTH
sample use of hold on
figure;
plot(x1,y1);
hold on
plot(x2,y2);
plot(x3,y3);
hold off
do "figure" and "hold on" only once outside the loop. then plot all the graphs you need
figure;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
hold on;
volume = data{7};
plot(xindays, volume);
hold off;
if you do it the way you suggested using hold on, i.e. use plotyy() first then the axes won't adjust so if you 3rd series is out of the range of your first set of axes then it won't appear. Try just flipping them and see if that produces a result?
volume = data{7};
plot(xindays, volume);
hold on;
[AX,H1,H2] = plotyy(xindays,y,xindays,yPrice);
This way the axes should adjust
For example:
t = 1:10;
x = t*2;
y = t*-2;
z = x + 1000;
Now compare
plot(t,z, 'r')
hold on
plotyy(t,x, t,y)
to
plotyy(t,x, t,y)
hold on
plot(t,z, 'r')
I'm trying to animate the graph of a function but I cant get the program to graph the correct points. I want to plot points between time 0 and 10 and animate this graph. How do I get the plot as a function of time?
clear;
w = 2*pi;
t = 0:.01:10;
y = sin(w*t);
x = cos(w*t);
for j=1:10
plot(x(6*j),y(6*j),'*');
axis square;
grid on;
F(j) = getframe;
end
movie(F,1,1);
I refined the code to:
clear;
w = 2*pi;
for j=2:11
t=j-1;
y = sin(w*t);
x = cos(w*t);
plot(x(t),y(t),'*');
axis square;
grid on;
F(j) = getframe;
end
movie(F);
This should do what I'm trying however now I'm getting an "Index exceeds matrix dimension." How can I solve this?
Here is an example that show an animated point along a circular path, while recording an AVI movie.
To learn more about doing animations and recording movies in MATLAB, check out this guide.
%# some parameters
ERASEMODE = 'normal'; %# normal,xor
RENDERER = 'painters'; %# painters,zbuffer,opengl
%# data
t = linspace(0,2*pi,100)'; %'# adjust number of points here
D = [cos(t) -sin(t)];
%# plot circluar path
figure('DoubleBuffer','on', 'Renderer',RENDERER)
plot(D(:,1), D(:,2), 'Color','b', 'LineWidth',2)
grid on, axis([-1.5 1.5 -1.5 1.5]), axis square
xlabel('x'), ylabel('y'), title('Circle Animation')
%#set(gca, 'DrawMode','fast')
%# moving point
hPoint = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode',ERASEMODE, ...
'Color','r', 'Marker','.', 'MarkerSize',30);
%# moving coordinates text
hTxtCoords = text(D(1,1), D(1,2), sprintf('(%.2f,%.2f)',D(1,:)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, 'EraseMode',ERASEMODE, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
%# angle text
hTxtAngle = text(0, 0, num2str(t(1),'%.02f'), ...
'FontSize',15, 'EraseMode',ERASEMODE, ...
'HorizontalAlignment','center', 'VerticalAlignment','middle');
%# prepare video output
useVideoWriter = ~verLessThan('matlab','7.11');
if useVideoWriter
vid = VideoWriter('vid.avi');
vidObj.Quality = 100;
vid.FrameRate = 30;
open(vid);
else
vid = avifile('vid.avi', 'fps',30, 'quality',100);
end
%# loop
for i=1:numel(t)
set(hPoint, 'XData',D(i,1), 'YData',D(i,2)) %# update point location
set(hTxtAngle, 'String',num2str(t(i),'%.02f')) %# update angle text
set(hTxtCoords, 'Position',D(i,:), ... %# update angle text
'String',sprintf('(%.3f,%.3f)',D(i,:)))
drawnow %# force refresh
if ~ishandle(hPoint), break; end %# if you close the figure
%# capture frame
if useVideoWriter
writeVideo(vid,getframe);
else
vid = addframe(vid, getframe(gcf));
end
end
%# close and save video output
if useVideoWriter
close(vid);
else
vid = close(vid);
end
%# open AVI file using system default player
winopen('vid.avi')
It's doing exactly what you ask it to do. You're subsampling the x and y, so it looks kind of funny.
Try
plot(x,y);
axis square;
ax = axis;
for jx = 1 : length(t),
plot(x(ix), y(ix), '*');
axis(ax); grid on;
F(ix) = getframe;
end
movie(F, 1, 1/(t(2)-t(1)))
I would also use t = 1 : 0.1 : 10; so that it plots at 10 FPS instead of 100. Slowing the frequency down to, say, w = pi; will be smoother as well.
At the end of the day, Matlab is just not a great animation solution.
Answer to refined code question
You'd need to use plot(x,y);, but this will reveal another error - your frame index does not start at 1. It will choke on F(j) in the first iteration, where j = 2. Why not just loop over t = 1 : 10?
I am looking to create a simple log(x) graph within MATLAB in which the model shows the point moving along the curve with time.
The overall aim is to have two of these graphs alongside one another and to apply an algorithm to them. I am really unsure where to start here.
I am relatively new at MATLAB coding so any help would be very useful!
Thanks
Luke
Here is a variation on #Jacob's solution. Instead of redrawing everything at each frame (clf) we simply update the point's location:
%# control animation speed
DELAY = 0.01;
numPoints = 600;
%# create data
x = linspace(0,10,numPoints);
y = log(x);
%# plot graph
figure('DoubleBuffer','on') %# no flickering
plot(x,y, 'LineWidth',2), grid on
xlabel('x'), ylabel('y'), title('y = log(x)')
%# create moving point + coords text
hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...
'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...
'Color',[0.2 0.2 0.2], 'FontSize',8, ...
'HorizontalAlignment','left', 'VerticalAlignment','top');
%# infinite loop
i = 1; %# index
while true
%# update point & text
set(hLine, 'XData',x(i), 'YData',y(i))
set(hTxt, 'Position',[x(i) y(i)], ...
'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))
drawnow %# force refresh
%#pause(DELAY) %# slow down animation
i = rem(i+1,numPoints)+1; %# circular increment
if ~ishandle(hLine), break; end %# in case you close the figure
end
A simple solution is:
x = 1:100;
y = log(x);
DELAY = 0.05;
for i = 1:numel(x)
clf;
plot(x,y);
hold on;
plot(x(i),y(i),'r*');
pause(DELAY);
end
You may want to have a look at the COMET function, which will make an animation of the curve.
For example (using the same numbers as #Jacob)
x = 1:100;
y = log(x);
comet(x,y)
If you want to show the point moving on the line (not 'drawing' it), you simply plot the line before
x = 1:100;
y = log(x);
plot(x,y,'r')
hold on %# to keep the previous plot
comet(x,y,0) %# 0 hides the green tail
a little more complex solution along the same lines as #Jacob. Here I add some optimization using handle graphics and a MATLAB movie object for playback.
x=1:100;
y=log(x);
figure
plot(x,y);
hold on; % hold on so that the figure is not cleared
h=plot(x(1),y(1),'r*'); % plot the first point
DELAY=.05;
for i=1:length(x)
set(h,'xdata',x(i),'ydata',y(i)); % move the point using set
% to change the cooridinates.
M(i)=getframe(gcf);
pause(DELAY)
end
%% Play the movie back
% create figure and axes for playback
figure
hh=axes;
set(hh,'units','normalized','pos',[0 0 1 1]);
axis off
movie(M) % play the movie created in the first part
solution can be this way
x = .01:.01:3;
comet(x,log(x))