konstantint / matplotlib-venn

Area-weighted venn-diagrams for Python/matplotlib
MIT License
519 stars 68 forks source link

Retrieve venn subset lists #31

Closed guillermomarco closed 7 years ago

guillermomarco commented 7 years ago

Dear dev,

I'm working on a 3-way venn diagram following the example in the documentation:

set1 = set(['A', 'B', 'C', 'D'])
set2 = set(['B', 'C', 'D', 'E'])
set3 = set(['C', 'D',' E', 'F', 'G'])
v  = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))

Is it possible to retrieve all the 7 subset set calcuated to plot the areas? I was able to retrieve the areas iterating through the patches [i for i in v.patches], however I would like to retrieve the elmenets representated in each region of the plot and not the areas.

If not, is it possible then to calculate all the 7 subsets with other method?

Regards, Guillermo

konstantint commented 7 years ago

matplotlib-venn is only focused on plotting and does not therefore provide a specific method for subset computation.

The venn3 method which takes sets as inputs is a just a utility wrapper, which uses the following straightforward internal method to compute subset sizes:

def compute_venn3_subsets(a, b, c):
    if not (type(a) == type(b) == type(c)):
        raise ValueError("All arguments must be of the same type")
    set_size = len if type(a) != Counter else lambda x: sum(x.values())   # We cannot use len to compute the cardinality of a Counter
    return (set_size(a - (b | c)),  # TODO: This is certainly not the most efficient way to compute.
        set_size(b - (a | c)),
        set_size((a & b) - c),
        set_size(c - (a | b)),
        set_size((a & c) - b),
        set_size((b & c) - a),
        set_size(a & b & c))

Assuming you do not need the extra input check and are using set types as input, you can easily adapt this to your needs into something as simple as:

def compute_venn3_subsets_full(a, b, c):
    return (a - (b | c), b - (a | c), (a & b) - c, c - (a | b), (a & c) - b, (b & c) - a, a & b & c)
guillermomarco commented 7 years ago

Got it. Thanks!