Squared heatmap with random transition; animation

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 件のコメント

Bidyut Bikash Goswami
Bidyut Bikash Goswami 2018 年 10 月 22 日
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.
Bidyut Bikash Goswami
Bidyut Bikash Goswami 2018 年 10 月 22 日
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?
Bidyut Bikash Goswami
Bidyut Bikash Goswami 2018 年 10 月 22 日
Time
jonas
jonas 2018 年 10 月 22 日
That is a bit more complicated but Ill give it a try.
Bidyut Bikash Goswami
Bidyut Bikash Goswami 2018 年 10 月 22 日
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.
Bidyut Bikash Goswami
Bidyut Bikash Goswami 2018 年 10 月 23 日
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.
Bidyut Bikash Goswami
Bidyut Bikash Goswami 2018 年 10 月 23 日
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 ExchangeColor 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