Squared heatmap with random transition; animation
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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

채택된 답변
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.
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
2018년 10월 22일
What do you mean smooth transition? Over time or space?
Time
jonas
2018년 10월 22일
That is a bit more complicated but Ill give it a try.
Thanks a lot for your help.
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?
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
2018년 10월 23일
Thanks! Always happy to help!
추가 답변 (1개)
Jan
2018년 10월 22일
What about pcolor?
카테고리
도움말 센터 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
