Who doesn’t have a need, from time to time, to create a nice Venn Diagram?

Recently, my kid had a need to create a diagram of the three branches of the US federal government with examples of their intersections.  Here’s what I came up with to help her out:

Step 1: Install the excellent matplotlib extension, matplotlib-venn

Thanks to examples on the Python Graph Gallery and doing a little bit of searching around, using matplotlib-venn wasn’t all that difficult.

Step 2: Load up the necessary packages


1
2
3
import matplotlib.pyplot as plt
from matplotlib_venn import venn3
import matplotlib.patheffects as path_effects

Step 3: Build your diagram

Here’s the code I came up with:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fig, ax = plt.subplots(figsize=(10, 10))
v = venn3(subsets = (10, 10, 4, 10, 4, 4, 2), set_labels = ('', '', ''), ax=ax)
v.get_label_by_id('100').set_text('Executive')
v.get_label_by_id('010').set_text('Legislative')
v.get_label_by_id('001').set_text('Judicial')
v.get_label_by_id('110').set_text('Example 1')
v.get_label_by_id('011').set_text('Example 2')
v.get_label_by_id('101').set_text('Example 3')
v.get_label_by_id('111').set_text('')
plt.title("The Three Branches of the US Government")

example_text = ('Example 1: The Vice President is considered "President of the Senate" and can vote to break ties.\n'
                'Example 2: The Legislature confirms Supreme Court justices.\n'
                'Example 3: The Executive appoints potential Supreme Court justices.')

text = fig.text(0.0, 0.05, example_text, ha='left', va='bottom', size=14)
text.set_path_effects([path_effects.Normal()])

plt.show()

And the final result: