How to modify the font size in Matplotlib-venn?

To modify the font size in Matplotlib-venn, we can use the set_fontsize() method on the text objects returned by the venn diagram functions. This allows you to customize both set labels and subset labels independently.

Installation

First, ensure you have the required library installed ?

pip install matplotlib-venn

Basic Example

Here's how to create a 3-set Venn diagram with custom font sizes ?

from matplotlib import pyplot as plt
from matplotlib_venn import venn3

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

set1 = {'a', 'b', 'c', 'd'}
set2 = {'a', 'b', 'e'}
set3 = {'a', 'd', 'f'}

out = venn3([set1, set2, set3], ('Set1', 'Set2', 'Set3'))

# Modify set labels font size
for text in out.set_labels:
    text.set_fontsize(25)

# Modify subset labels font size
for text in out.subset_labels:
    text.set_fontsize(12)

plt.show()

Different Font Sizes for Each Label

You can also set different font sizes for individual labels ?

from matplotlib import pyplot as plt
from matplotlib_venn import venn3

set1 = {'python', 'java', 'c++', 'javascript'}
set2 = {'python', 'java', 'go'}
set3 = {'python', 'c++', 'rust'}

out = venn3([set1, set2, set3], ('Languages A', 'Languages B', 'Languages C'))

# Set different font sizes for each set label
font_sizes = [20, 15, 18]
for i, text in enumerate(out.set_labels):
    text.set_fontsize(font_sizes[i])

# Set uniform font size for subset labels
for text in out.subset_labels:
    if text:  # Check if text exists
        text.set_fontsize(10)

plt.title('Programming Languages Venn Diagram', fontsize=16)
plt.show()

Using 2-Set Venn Diagrams

The same approach works with 2-set diagrams using venn2() ?

from matplotlib import pyplot as plt
from matplotlib_venn import venn2

set1 = {'apple', 'banana', 'cherry', 'date'}
set2 = {'banana', 'cherry', 'elderberry'}

out = venn2([set1, set2], ('Fruits A', 'Fruits B'))

# Modify font sizes
for text in out.set_labels:
    text.set_fontsize(20)

for text in out.subset_labels:
    text.set_fontsize(14)

plt.title('Fruit Sets Comparison', fontsize=16)
plt.show()

Key Parameters

Parameter Description Usage
set_labels Labels for each set (outside circles) Main category names
subset_labels Labels for intersections (inside circles) Element counts or names
set_fontsize() Method to change font size Accepts integer values

Conclusion

Use set_fontsize() on the returned text objects to customize font sizes in Matplotlib-venn diagrams. This method works for both set labels and subset labels, allowing precise control over text appearance.

Updated on: 2026-03-25T23:10:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements