publiplots.venn¶
- publiplots.venn(sets, labels=None, colors=None, alpha=None, figsize=None, ax=None, fmt='{size}', color_labels=True)[source]¶
Create a Venn diagram for 2-5 sets.
This function creates true Venn diagrams that show all possible intersections. Each region (petal) is labeled with the size of the intersection by default.
- Parameters:
sets (list of sets or dict) – Either a list of 2-5 sets, or a dictionary mapping labels to sets. Example: [set1, set2, set3] or {‘A’: set1, ‘B’: set2, ‘C’: set3}
labels (list of str, optional) – Labels for each set. If sets is a dict, labels are taken from keys. Default: [‘Set A’, ‘Set B’, ‘Set C’, …]
colors (list of str, str, or None, optional) – Colors for each set. Can be: - List of color names/codes for each set - String name of a publiplots palette or seaborn palette - None (uses ‘pastel’ palette)
alpha (float, default=0.3) – Transparency of set regions (0=transparent, 1=opaque).
figsize (tuple, default=(10, 6)) – Figure size as (width, height) in inches.
ax (Axes, optional) – Matplotlib axes object. If None, creates new figure.
fmt (str, default='{size}') – Format string for region labels. Can include: - {size}: number of elements in the intersection - {logic}: binary string representing the intersection - {percentage}: percentage of total elements
color_labels (bool, default=True) – Whether to color the set labels with the same color as the petals.
- Returns:
fig (Figure) – Matplotlib figure object.
ax (Axes) – Matplotlib axes object.
- Raises:
ValueError – If the number of sets is not between 2 and 5 Consider using upset plot instead.
TypeError – If sets is not a list of sets or dict of sets
- Return type:
Examples
Simple 2-way Venn diagram:
>>> set1 = {1, 2, 3, 4, 5} >>> set2 = {4, 5, 6, 7, 8} >>> fig, ax = pp.venn([set1, set2], labels=['Group A', 'Group B'])
3-way Venn with custom colors:
>>> sets_dict = {'A': set1, 'B': set2, 'C': set3} >>> colors = ['red', 'blue', 'green'] >>> fig, ax = pp.venn(sets_dict, colors=colors)
4-way Venn with colormap:
>>> fig, ax = pp.venn([set1, set2, set3, set4], colors='Set1')5-way Venn diagram with percentage labels:
>>> fig, ax = pp.venn( ... [set1, set2, set3, set4, set5], ... fmt='{size} ({percentage:.1f}%)' ... )