Accurate color blending in Matplotlib-Venn

Question:

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?

Asked By: neversaint

||

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.

Answered By: samgak

Pass the colors directly to venn2 when creating the diagram via the set_colors argument, then it will do the color blending automatically:

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"])}

circlenm = ()
circlels = []

for k,v in named_sets.items():
    circlenm = circlenm + (k,)
    circlels.append(v)

c = venn2(
    subsets = circlels,
    set_labels = circlenm,
    set_colors=("red", "blue")
)
plt.show()

colored venn diagram

Answered By: M. Gruber