2
votes

I need to draw a custom plot with in Matlab, actually, I will combine 4 different graphs in one, with the following scenario. Let say, I run an application for 4 times, the values go to X axis will always be same, so for each run, x1=x2=x3=x4=[1 2 3 4];

For every x point, there will be a y point, an element of set A=('a','b','c'),

Let say for each run the corresponding Y values are: y1=['a' 'b' 'a' 'c']; y2=['a' 'a' 'b' 'c']; y3=['c' 'a' 'a' 'a']; and y4=['a' 'b' 'c' 'a']; with these values, I want to draw a figure which combines all 4 runs in one chart. I want to represent the Y values as a 1 unit tall (actually its height is not that much matter) colored vertical line, instead of just points. The following image demonstrated the figure I want to draw, anyone knows a way to achieve this? Thanks

The custom figure

1

1 Answers

2
votes

Here is my suggestion for this:

% orginal data:
x = 1:4;
y1 = 'abac';
y2 = 'aabc';
y3 = 'caaa';
y4 = 'abca';
Y = [y1;y2;y3;y4];
% convert to numeric:
y = Y-'a'+1;
y = rot90(y,3);
% create an "image" of the data:
res = 10;
ymesh = nan(size(y,1)*res,(size(y,2)+1)*res);
for ii = 1:size(y,1)
    for jj = 1:size(y,2)
        ymesh(res*(jj-1)+1:res*jj,res*ii) = y(ii,jj);
    end
end
% set the boundries between y categories
ymesh(res:res:size(ymesh,1)-res,:) = 4;
% plotting:
abcCol = [1 1 1;0 0.7 0;0.8 0 0;1 0.8 0;0 0 0];
xtic = res:res:length(ymesh)-1;
xticlable = num2str((1:size(y,1)).');
ytic = res/2:res:size(ymesh,1)-res/2;
yticlable = {'4^{th} run','3^{ed} run','2^{nd} run','1^{st} run'};
f = figure('Colormap',abcCol);
imagesc(ymesh)
set(gca,'Parent',f,'CLim',[0 4],...
    'YTickLabel',yticlable,'YTick',ytic,...
    'XTickLabel',xticlable,'XTick',xtic)
% add the legend on anoother 'fake' axes
legax = axes('Parent',f);
p = plot(nan(2,3),'Linewidth',3);
set(p,{'Color'},mat2cell(abcCol(2:end-1,:),[1 1 1].',3));
legend (unique(Y))
axis off

Which will create this:

Custom plot

The code above is quite specific to your problem but can be generalized easily. The main idea is to create an "image" of the data, and the use raster techniques to plot it, instead of drawing every line separately.