MATLAB Examples

Data Visualization Contest

I wanted to look at the underlying space of two factors that went into the overall score for the contest, and see how they related to the length of the entry. The main factors are the result, complexity and the CPU time. I chose result, cpu_time and the nodeCount for my visualization. This is perfect for a 3-D scatter.

Here is the result of my visualization, animated to get the full 3-D effect.

Start by loading the contest data.

load('contest_data.mat')

Create the variables I might want to use.

ts = [d.timestamp];
sc = [d.score];
re = [d.result];
nc = [d.nodeCount];
cpu = [d.cpu_time];

minScore = min(sc);
normScore = sc-minScore+1;

minResult = min(re);
normResult = re-min(re)+1;

Plot result, cpu_time, nodeCount

figure('Color',[1 1 1]) %('Renderer','OpenGL')
plot3(normResult, cpu, nc, '.')
set(gca,'XScale','log')
xlabel('Normalized Result')
ylabel('CPU Time')
zlabel('Node Count')
set(gca,'Xlim',[1 100000])
set(gca,'Color','none')
drawnow

Find the leaders

best = d(1).score;
bestIndexList = 1;
for n = 1:length(d),
    if d(n).score < best,
        best = d(n).score;
        bestIndexList = [bestIndexList n];
    end
end

Now plot them in red on top of the previous plot

hold on
plot3(normResult(bestIndexList), cpu(bestIndexList),nc(bestIndexList),...
    'r.-','LineWidth',2)
drawnow
%hold off

Compute the surface of this space

sInd = find(isfinite(normResult)); %remove NaNs and Infs
pointSpace = [normResult(sInd)',cpu(sInd)', nc(sInd)'];
k = convhulln(pointSpace);

outerPoints = unique(k(:));
plot3(pointSpace(outerPoints,1),pointSpace(outerPoints,2),pointSpace(outerPoints,3),...
    'ok','MarkerSize',6)
drawnow

I tried to add a surface of the convex hull, but this didn't really add to the plot. Here is the code in case you want to try.

% hs = trisurf(k,normResult(sInd)',cpu(sInd)', nc(sInd)',...
%     'FaceColor',[0 .9 .3],...
%     'AlphaDataMapping','none','EdgeAlpha',0,'FaceAlpha',.2);

Credit goes to Bob Bemis for providing the snippet of code I needed to get this animated GIF to work.

[http://www.mathworks.com/matlabcentral/fileexchange/21944 Animated GIF]

set(gca,'DataAspectRatioMode','manual', 'clipping','off')
axis vis3d
drawnow
% turn off the axes and labels
axis off
f = getframe(gcf);
[im,map] = rgb2ind(f.cdata,256,'nodither');
im(1,1,1,36) = 0;

for i=1:36
	camorbit(10,0,'data',[0 0 1])
	drawnow
    f = getframe(gcf);
    im(:,:,1,i) = rgb2ind(f.cdata,map,'nodither');
end

Write the GIF file.

fname = 'datavisSP.gif';
imwrite(im,map,fname,'DelayTime',0,'LoopCount',inf)
if exist('html','dir')
    copyfile(fname,['html/' fname])
end