I am using the matplotlib_venn library for creating the Venn diagram, and the shading issue seems to appear whenever I try to evaluate a complement of a set or expression.
My code:
import matplotlib.pyplot as plt
from itertools import product
from matplotlib_venn import venn3, venn3_circles
#Initialize
universal_set = set()
empty_set = set()
complement = set()
expression = ""
setA = set()
setB = set()
setC = set()
def evaluate_expression(formula, empty_set):
for x in product([False, True], repeat=3):
x_dict = dict(zip('xyz', x))
x_dict['universal_set'] = universal_set
x_dict['empty_set'] = empty_set
x_dict['complement'] = complement
if 'empty_set' in formula:
formula = formula.replace('empty_set', 'False')
if 'universal_set' in formula:
formula = formula.replace('universal_set', 'True')
if 'complement' in formula:
formula = formula.replace('complement', 'True')
if eval(formula, {}, x_dict):
yield ''.join(str(int(x_i)) for x_i in x)
def plot_diagram(formula, empty_set):
plt.figure(figsize=(6, 6))
v = venn3(subsets=[1] * 7,
set_colors=['white'] * 3,
subset_label_formatter=lambda x: '')
for region in evaluate_expression(formula, empty_set):
if region != '000':
v.get_patch_by_id(region).set_color('#FFC0CB')
else:
ax= plt.axes()
ax.figure.set_facecolor('#FFC0CB')
# Add labels for setA, setB, and setC
v.get_label_by_id('100').set_text(', '.join(map(str, setA - setB - setC)))
v.get_label_by_id('010').set_text(', '.join(map(str, setB - setA - setC)))
v.get_label_by_id('001').set_text(', '.join(map(str, setC - setB - setA)))
# Add labels for intersections
v.get_label_by_id('101').set_text(', '.join(map(str, setA & setC - setB)))
v.get_label_by_id('110').set_text(', '.join(map(str, setA & setB - setC)))
v.get_label_by_id('011').set_text(', '.join(map(str, setB & setC - setA)))
v.get_label_by_id('111').set_text(', '.join(map(str, setB & setC & setA)))
venn3_circles(subsets=[1] * 7, linestyle='solid')
#Create menu
def menu():
print("#1 Enter a number to Set A")
print("#2 Enter a number to Set B")
print("#3 Enter a number to Set C")
print("#4 Evaluate Expression")
print("#5 Display the Venn Diagram")
print("#6 Exit")
#Execute depending on user's choice
while True:
menu ()
choice= input("Enter your number of choice: ")
if choice == '1':
try:
setA.update(map(int, input("Enter a number to set A (separate using space): ").split()))
except ValueError:
print("Error. Input must be an integer")
elif choice == '2':
try:
setB.update(map(int, input("Enter a number to set B (separate using space): ").split()))
except ValueError:
print("Error. Input must be an integer ")
elif choice == '3':
try:
setC.update(map(int, input("Enter a number to set C (separate using space): ").split()))
except ValueError:
print("Error. Input must be an integer")
elif choice == '4':
print("x for set A, y for set B, z for set C")
print("| for union, & for intersection, -complement for complement, universal_set for universal set, empty_set for empty set")
expression = input("Evaluate Expression: ")
elif choice == '5':
if expression:
try:
plot_diagram(expression, empty_set)
plt.show()
except Exception as e:
print("Invalid formula. Error:", str(e))
else:
print("Please evaluate an expression first.")
elif choice =='6':
print("Exiting the program.")
break
else:
print("Invalid input")
I would appreciate any guidance or suggestions on how to ensure that the unshaded parts are consistently displayed in a pure white color.
here's an example of an expression and its output: x-complement
I have attempted to adjust the colors in the Venn diagram, specifically in the plot_diagram function, but I haven't been successful in making the unshaded parts completely white.
Just add
alpha=1.0
to yourvenn3
function call.By default
venn3
generates regions with some transparency (alpha=0.4
) which is what makes the pink background partially visible through the white regions.