Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accurate color blending in Matplotlib-Venn

With the following code:

from matplotlib import pyplot as plt
from matplotlib_venn import venn2
from collections import OrderedDict

named_sets = {'x1': set(['foo','foo','bar',"pax"]), "x3" : set(['foo','qux','bar',"zoo"])}
od = OrderedDict(sorted(named_sets.iteritems()))

circlenm = ()
circlels = []
for k,v in od.iteritems():
    circlenm = circlenm + (k,)
    circlels.append(v)


c = venn2(subsets = circlels,set_labels = circlenm)
c.get_patch_by_id('10').set_color('red')
c.get_patch_by_id('01').set_color('blue')
c.get_patch_by_id('10').set_edgecolor('none')
c.get_patch_by_id('01').set_edgecolor('none')
c.get_patch_by_id('10').set_alpha(0.4)
c.get_patch_by_id('01').set_alpha(0.4)
plt.show()

I can get the following figure:

enter image description here

Here I'd like to blend circles of 'blue' with 'red'. Notice that the result of the blending is brown.

But the actual value supposed to be light magenta (figure below is created using default matplotlib_venn.venn3):

enter image description here

How can I achieve that correctly?

like image 512
neversaint Avatar asked Sep 28 '22 05:09

neversaint


1 Answers

Add these 3 lines to set the color and display properties of the intersection:

c.get_patch_by_id('11').set_color('magenta')
c.get_patch_by_id('11').set_edgecolor('none')
c.get_patch_by_id('11').set_alpha(0.4)

If you want an exact color then you can set like this:

c.get_patch_by_id('11').set_color('#e098e1')

The patch id is a bitmask showing which circles the area is inside.

like image 51
samgak Avatar answered Oct 20 '22 07:10

samgak