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.
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.
What do you mean smooth transition? Over time or space?
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 :)
Thanks! Always happy to help!
更多回答(1 个)
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)
