Hi, I need to plot a40x40 squared heatmap animation, Something like the attached figure but a dynamic one where the color keep changing "randomly".
FYI, I have a system with 5 states 0, 1, 2, 3, and 4. Its a conditional system (Markov Chain) which can transit from one state to another. I need to present this concept in a visibly attractive way. That is why I want to plot this animation. Looking for suggestions and help. Thanks

 채택된 답변

jonas
jonas 2018년 10월 22일
편집: jonas 2018년 10월 22일

1 개 추천

You could also use scatter with a superlarge markersize.
figure;
colormap([1 1 1;1 1 0;1 0 0;0 0 1;0 1 0]);
[X,Y] = meshgrid(1:40,1:40);
h = scatter(X(:),Y(:),50,randi([1 4],numel(X),1),'s','filled','markeredgecolor',[.5 .5 .5])
ax = gca;
set(ax,'visible','off')
ax.Position = ax.Position ./ [1 0.6 1.2 1.2];
axis equal
for j = 1:100;
h.CData = randi([1 5],numel(X),1);
pause(0.1)
end

댓글 수: 12

Thank you. Almost what I wanted. But I am getting lot of colors in my plot (as seen in the
figure below. How do I restrict it to only 5 colors? Also, can you please suggest, how do I add smooth transitioning (I am changing the "pause" value but that is not what I am looking for ... the transition needs to me smooth so that the viewer can appreciate the transition). Thank you in advance.
jonas
jonas 2018년 10월 22일
편집: jonas 2018년 10월 22일
I think you ran the code before my edit where I fixed that. Try again and note the use of randi and the colormap.
Sorry ... my bad. Thank for the editing. Its exactly what I needed. A smooth transition of the colors would be just the perfect. Thanks again.
jonas
jonas 2018년 10월 22일
What do you mean smooth transition? Over time or space?
jonas
jonas 2018년 10월 22일
That is a bit more complicated but Ill give it a try.
Thanks a lot for your help.
jonas
jonas 2018년 10월 22일
편집: jonas 2018년 10월 22일
Try this, and see my comments below.
figure;
cmap = nan(400,3);
cmap(1,:) = [1 0 0];
cmap(100,:) = [1 1 0];
cmap(200,:) = [0 1 0];
cmap(300,:) = [0 0 1];
cmap(400,:) = [1 1 1];
cmap=fillmissing(cmap,'linear');
colormap(cmap);
[X,Y] = meshgrid(1:40,1:40);
h = scatter(X(:),Y(:),50,randi([1 4],numel(X),1),'s','filled','markeredgecolor',[.5 .5 .5]);
ax = gca;
set(ax,'visible','off');
ax.Position = ax.Position ./ [1 0.6 1.2 1.2];
axis equal
ax.CLim = [1 5];
n = 30;
C = nan(numel(X),n);
for j = 1:2
C(:,1) = h.CData;
C(:,end) = randi([1 5],numel(X),1);
C = fillmissing(C','linear')';
for jj = 1:n
h.CData = C(:,jj);
drawnow
pause(0.1)
end
pause(5)
C = nan(numel(X),n);
end
The difficult part is building the colormap so that the transition between colors are smooth.
The colorbar currently looks like this:
The colors are interpolated linearly between each set of random colors. This means that a square going from blue to white will appear blue-> light blue -> white, whereas a color going from red to white will transition through the entire spectrum. I don't know a different way to approach this though.
Thanks a lot. Unfortunately I have R2015b installed in my system currently. So "fillmissing" is not working. I shall upgrade and update you. Anyways, thanks a lot for your help. A quick query: how do I save the animation in gif format?
jonas
jonas 2018년 10월 23일
편집: jonas 2018년 10월 23일
My pleasure! You dont really need fillmissing but its a very good function. interp1 would work equally well, or you could use this FEX function
For gifs I have used this FEX function
Also, please don't forget to accept the answer. I feel we are drifting away from the original scope of the question, and its better to post a new thread if you have additional questions. Answering new questions in the comment section is not optimal as the answers are less visible to others facing similar issues.
Thanks a lot. Happily Accepted :)
jonas
jonas 2018년 10월 23일
Thanks! Always happy to help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2018년 10월 22일

0 개 추천

What about pcolor?

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

질문:

2018년 10월 22일

댓글:

2018년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by