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

採用された回答
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.
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
2018 年 10 月 22 日
What do you mean smooth transition? Over time or space?
Bidyut Bikash Goswami
2018 年 10 月 22 日
Time
jonas
2018 年 10 月 22 日
That is a bit more complicated but Ill give it a try.
Bidyut Bikash Goswami
2018 年 10 月 22 日
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.
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?
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
2018 年 10 月 23 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
