I'm trying to do some stats homework in python and I wanted to create a Venn diagram of some different outcome spaces. I'm trying to pass two-element tuples to the Venn diagram and it doesn't seem to be too pleased about it. I really appreciate anyone who helps point me in the right direction!
...
combination_storage_container = []
for i in range(1,7):
out = "(" str(i) ", " # making the initial string form
print("\n")
for j in range(1,7):
out = (str(j) ")") # adding the final piece
print(out, end=" , ") # printing the piece
out = out[:4]
combination_storage_container.append(tuple([i,j]))
print("\n\n\n",combination_storage_container)
# IF I was making this a permanent piece of code, id do it in one loop
A_space = []
B_space = []
C_space = []
for combo in combination_storage_container:
if combo[0] combo[1] > 8: # <------ sum condition
A_space.append(combo)
if combo[0] == 2 or combo[1] == 2: # 2 condition
B_space.append(combo)
if combo[0] > 4: #<-------------- green bigger than 4 condition
C_space.append(combo)
print("The A space is:\n", A_space)
print("The B space is:\n", B_space)
print("The C space is:\n", C_space)
venn3([A_space, B_space, C_space], ('A Space', 'B Space', 'C Space'))
plt.show()
...
I get this error: ...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [50], in <cell line: 16>()
13 print("The B space is:\n", B_space)
14 print("The C space is:\n", C_space)
---> 16 venn3([A_space, B_space, C_space], ('A Space', 'B Space', 'C Space'))
17 plt.show()
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\matplotlib_venn\_venn3.py:345, in venn3(subsets, set_labels, set_colors, alpha, normalize_to, ax, subset_label_formatter)
343 subsets = [subsets.get(t, 0) for t in ['100', '010', '110', '001', '101', '011', '111']]
344 elif len(subsets) == 3:
--> 345 subsets = compute_venn3_subsets(*subsets)
347 if subset_label_formatter is None:
348 subset_label_formatter = str
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\matplotlib_venn\_venn3.py:259, in compute_venn3_subsets(a, b, c)
257 raise ValueError("All arguments must be of the same type")
258 set_size = len if type(a) != Counter else lambda x: sum(x.values()) # We cannot use len to compute the cardinality of a Counter
--> 259 return (set_size(a - (b | c)), # TODO: This is certainly not the most efficient way to compute.
260 set_size(b - (a | c)),
261 set_size((a & b) - c),
262 set_size(c - (a | b)),
263 set_size((a & c) - b),
264 set_size((b & c) - a),
265 set_size(a & b & c))
TypeError: unsupported operand type(s) for |: 'list' and 'list'
CodePudding user response:
When you are passing three subsets like that, the first argument in the venn3
function needs to be a list (or a tuple) of sets, so that their membership can be compared with the |
(union) operator. You are currently passing a list of lists, so convert the lists to sets:
venn3([set(A_space), set(B_space), set(C_space)], ('A Space', 'B Space', 'C Space'))
Output: