How can we use Matlab to draw Venn diagrams?
Download the Matlab code for this exercise from venn.zip.
imshow(a) |
Matlab code for creating logical images a and b
xp = linspace(-1,1,256); x = ones(size(xp))'*xp; y = xp'*ones(size(xp)); a = sqrt((x+0.3).^2+y.^2)<0.6; b = sqrt((x-0.3).^2+y.^2)<0.6; r1 = max(find(xp<-0.7)); r2 = min(find(xp>0.7)); a = a(r1:r2,:); b = b(r1:r2,:);
If you do not have the Matlab Image Processing toolbox, you could use the following commands to show logical images:
imagesc(a); axis equal;
Simple Venn diagrams can be displayed using the logical images just obtained:
imshow(xor(a,b)) |
The following Matlab code will generate an RGB image of a Venn diagram with a white (FALSE) background yellow (TRUE) foreground.
z = xor(a,b); p = ones(size(z)); rgb = cat(3,p,p,and(p,~z)); imshow(rgb)
The problem with this image is that the diagram background blends with the page background.
The following Matlab code creates some border images
% generate outlined box sz = size(a); u = ones(sz); u(1:2,:)=0; u(:,1:2)=0; u(sz(1)-(0:1),:)=0; u(:,sz(2)-(0:1))=0; % generate edge images se = strel('disk',2); ae = a - imerode(a,se); be = b - imerode(b,se); ue = double(and(u,~(ae|be)));
This code uses some simple morphological image processing operations, and does require the Image Processing toolbox.
imshow(u); | imshow(be); | imshow(ue); |
The following code produces the final Venn diagram
% generate example venn diagram z = xor(a,b); rgb = cat(3,ue,ue,and(ue,~z)); imshow(rgb);
Maintained by John Loomis, last updated 29 May 2004