NeStack
NeStack

Reputation: 2012

Draw a Venn-diagram in python with one large area encompassing all other

I used so far succesffully this code:

import matplotlib.pyplot as plt
from matplotlib_venn import venn2

# Define the areas for the Venn diagram
area1 = 13552  # Area of the left ellipse
area2 = 7887   # Area of the right ellipse
intersection = 7747  # Area of the intersection

# Calculate the unique areas for each set
only_area1 = area1 - intersection
only_area2 = area2 - intersection

# Create the Venn diagram
plt.figure(figsize=(8, 6))
v = venn2(subsets=(only_area1, only_area2, intersection), set_labels=("", ""))

# Customize the labels
v.get_label_by_id('10').set_text("SDSS-FIRST,\n$i-t_f > 2.5$\nN=13552")
v.get_label_by_id('01').set_text("SDSS-FIRST-\nQuaia-VLASS,\n$G-t > 2.5$\nN=7887")
v.get_label_by_id('11').set_text("$i-t_f > 2.5$,\n$G-t > 2.5$\nN=7747")

# Style the diagram with only margins colored
for subset, color in zip(['10', '01', '11'], ['red', 'blue', 'green']):
    patch = v.get_patch_by_id(subset)
    if patch:
        patch.set_edgecolor(color)
        patch.set_linewidth(2)
        patch.set_facecolor('none')

# Customize the label colors
v.get_label_by_id('10').set_color('red')
v.get_label_by_id('01').set_color('blue')
v.get_label_by_id('11').set_color('green')

plt.show()

And it created this nice plot:

enter image description here

But now I decided I want to encompass all of the areas in the diagram as a part of an all-encompassing area, so I want to get a diagram like this:

enter image description here

How do I code this? It seems like I have to use venn3 but I am confused about how to tell that the black circle should contain everything else. And ChatGPT provides only nonsense. Thanks for any help!


Using the help of tanglef I was able to create this code, that almost does the job:

from collections import Counter
import matplotlib.pyplot as plt

sets = Counter()

sets["100"] = 0.21346
sets["010"] = 0
sets["001"] = 0
sets["110"] = 0.13552
sets["101"] = 0.7887
sets["111"] = 0.7747
sets["011"] = 0
# Create the Venn diagram
plt.figure(figsize=(8, 6))
v = venn3(subsets=sets)

# Customize the labels
v.get_label_by_id('100').set_text("SDSS-FIRST,\n$i < 20.5$")
v.get_label_by_id('101').set_text("SDSS-FIRST,\n$i-t_f > 2.5$\nN=13552")
v.get_label_by_id('110').set_text("SDSS-FIRST-\nQuaia-VLASS,\n$G-t > 2.5$\nN=7887")
v.get_label_by_id('111').set_text("$i-t_f > 2.5$,\n$G-t > 2.5$\nN=7747")

# Style the diagram with only margins colored
for subset, color in zip(['100','110', '101', '111'], ['black','red', 'blue', 'green']):
    patch = v.get_patch_by_id(subset)
    if patch:
        patch.set_edgecolor(color)
        patch.set_linewidth(4)
        patch.set_facecolor('none')

# Customize the label colors
v.get_label_by_id('100').set_color('black')
v.get_label_by_id('110').set_color('red')
v.get_label_by_id('101').set_color('blue')
v.get_label_by_id('111').set_color('green')

enter image description here

My problem now is that I want to get rid off the labels A,B,C and I want to make the area sizes correspond to the numbers provided in sets (= to the numbers in the labels), because as of now the area sizes are wrong. And I wonder if I have maybe chosen the wrong sets.

Upvotes: 0

Views: 56

Answers (2)

KT.
KT.

Reputation: 11440

What about simply adding a yet another circle (unrelated to the venn diagram ones)?

from matplotlib.patches import Circle
# Note that the diagramed is centered at (0, 0) by default.
xmin, xmax = plt.gca().get_xlim()
ymin, ymax = plt.gca().get_ylim()
plt.gca().add_patch(Circle((0, 0), min(xmin, xmax, ymin, ymax)*0.9, fill=None, edgecolor="k"))

Upvotes: 1

tanglef
tanglef

Reputation: 116

You can define and set the parts you don't want by setting them manually to 0 value. For example

from collections import Counter
import matplotlib.pyplot as plt

sets = Counter()
sets["10"] = 2
sets["01"] = 10
sets["11"] = 1
sets["100"] = 30
sets["010"] = 0
sets["001"] = 0
sets["110"] = 3
sets["101"] = 3
sets["111"] = 10
sets["011"] = 0
# Create the Venn diagram
plt.figure(figsize=(8, 6))
v = venn3(subsets=sets)

You should be able now to set your labels and sizes as you need.

Upvotes: 1

Related Questions