logo4 Evolution is progress—                          
progress is creativity.        
vline

Venn Diagrams with Python Matplotlib and Django

view blog view wiki view wiki view wiki

Unfortunately there is no proper tool in python to visualize set operations and relations, neither in maplotlib nor in graphviz. The closest solution that I found was a Venn diagram library attaached to maplotlib.

The latest version can be downloaded here. There also sample examples can be found. These however don't work with a browser after some tiny changes I got it running in my web environment too.

The first example shows a fully customized diagram:

def set_figure(fig):    
    import numpy as np
    from matplotlib_venn import venn3, venn3_circles

    ax=fig.add_subplot(1,1,1)
    v = venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'),ax=ax)
    v.get_patch_by_id('100').set_alpha(1.0)
    v.get_patch_by_id('100').set_color('white')
    v.get_label_by_id('100').set_text('Unknown')
    v.get_label_by_id('A').set_text('Set "A"')
    c = venn3_circles(subsets=(1, 1, 1, 1, 1, 1, 1), linestyle='dashed',ax=ax)
    c[0].set_lw(1.0)
    c[0].set_ls('dotted')
    ax.set_title("Sample Venn diagram")
    ax.annotate('Unknown set', xy=v.get_label_by_id('100').get_position() - np.array([0, 0.05]), xytext=(-70,-70),
                ha='center', textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='gray', alpha=0.1),
                arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5',color='gray'))
Venn example 1 (Full customization)

The second example demonstrates subplots:

def set_figure(fig):    
    from matplotlib_venn import venn2, venn2_circles, venn3, venn3_circles
    
    ax1=fig.add_subplot(2,2,1)
    ax2=fig.add_subplot(2,2,2)
    ax3=fig.add_subplot(2,2,3)
    ax4=fig.add_subplot(2,2,4)
    venn2(subsets={'10': 2, '01': 2, '11': 1}, set_labels = ('A', 'B'), ax=ax1)
    venn2_circles((1, 2, 3), ax=ax2)
    venn3(subsets=(1, 1, 1, 1, 1, 1, 1), set_labels = ('A', 'B', 'C'), ax=ax3)
    venn3_circles({'001': 10, '100': 20, '010': 21, '110': 13, '011': 14}, ax=ax4)
Venn example 2 (Subplots)

Overlapping between the three sets is demonstrated in this last example:

def set_figure(fig):    
   import numpy as np
   from matplotlib_venn import venn3, venn3_circles
   # Given three sets ...
   set1 = set(['A', 'B', 'C', 'D','X'])
   set2 = set(['B', 'C', 'D', 'E'])
   set3 = set(['C', 'D',' E', 'F', 'G','X'])

   # Compute the union of all elements
   union = set1.union(set2).union(set3)
    
   # For each element compute its 'indicator'
   # (e.g. an indicator of 110 means element belongs to set1 and set2 but not set3)
   indicators = ['%d%d%d' % (a in set1, a in set2, a in set3) for a in union]
   
   # Use the standard Counter object (Python 2.7+) to count the frequency for each indicator
   from collections import Counter
   subsets = Counter(indicators)

   ax=fig.add_subplot(1,1,1)
   # Provide the resulting dictionary as the subsets parameter to venn3:
   v = venn3(subsets, ('Set1', 'Set2', 'Set3'),ax=ax)
Venn example 3 (Diagram calculation)

My intention was to use these diagrams to visualize set operations and set relations, such as

B \in A

def set_figure(fig):    
   from matplotlib_venn import venn2

   ax=fig.add_subplot(1,1,1)
   v = venn2((3, 0, 1),ax=ax)
   v.get_patch_by_id('10').set_alpha(.50)
   v.get_patch_by_id('10').set_color('r')
   v.get_label_by_id('10').set_text('')
   v.get_label_by_id('A').set_text('Set "A"')

   v.get_patch_by_id('01').set_alpha(.50)
   v.get_patch_by_id('01').set_color('r')
   v.get_label_by_id('01').set_text('')
   v.get_label_by_id('B').set_text('Set "B"')
   
   v.get_patch_by_id('11').set_alpha(.50)
   v.get_patch_by_id('11').set_color('b')
   v.get_label_by_id('11').set_text('')
Set B in set A

Basic set operations are shown on a separate page.

Unfortunately the implementation of Venn diagram is merely basic, for instance such complex operations cannot be visualized.

\limsup_n E_n = \bigcap_{n=1}^\infty \bigcup_{k=k}^\infty E_k

and

\liminf_n E_n = \bigcup_{n=1}^\infty \bigcap_{k=k}^\infty E_k


Tags: Software


Categories: Mathematics Software

 
   

(c) Mato Nagel, Weißwasser 2004-2013, Disclaimer