How to animate scatter colorbar plot?

7 views (last 30 days)
Sara
Sara on 11 Jun 2017
Commented: Sara on 15 Jun 2017
I have my original scatterplot with a colorbar that works really great to distinguish different months based on color of the dots. I would like to animate it in some way.
%%original:
figure(21)
hold on, box on, grid on;
scatter(Q,Ca_est,110,dates_asnum,'o','filled');
ylabel('Estimated Ca [ug/L]');
xlabel('Q [m^3/sec]');
c = colorbar;
datetick(c,'y');
I could not figure out how to properly animate a scatterplot with a colorbar. These are my attempts:
%%Attempt 1 plots only a static picture of my original plot:
figure(22)
hold on, box on, grid on;
scatter(Q,Ca_est,110,dates_asnum,'o','filled');
ylabel('Estimated Ca [ug/L]');
xlabel('Q [m^3/sec]');
c = colorbar;
datetick(c,'y');
F = getframe(gcf);
movie(F);
%%Attempt 2, simply doesn't do anything:
figure(23)
hold on, box on, grid on;
fig = scatter(Q,Ca_est,110,dates_asnum,'o','filled');
c = colorbar;
datetick(c,'y');
M(fig)=getframe;
%%Attempt 3, using comet3 plot: This comet plot works really well as a moving line with a single color, but I would really prefer animated dots colored by month.I have tried adding a colorbar to the comet plot itself, but could not accomplish this.
%%comet line plot, no color:
figure(24)
hold on, box on, grid on;
comet3(Q,Ca_est,dates_asnum);
Any insight is appreciated. Thank you!
  2 Comments
KSSV
KSSV on 12 Jun 2017
Animate scatter plot, huumh..you want to add point by point and animate?
Sara
Sara on 15 Jun 2017
Yes! I would like it to animate by datetime, but keep my colorbar for each month.

Sign in to comment.

Answers (1)

KSSV
KSSV on 12 Jun 2017
clc; clear all ;
N = 100 ;
filename = 'test.gif';
data = rand(N,2) ;
figure(1)
axis([min(data(:,1)) max(data(:,1)) min(data(:,2)) max(data(:,2))])
hold on
for i = 1:N
scatter(data(i,1),data(i,2)) ;
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end

Categories

Find more on Animation 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!