Venn Diagram Gallery¶
Venn diagrams are used for visualizing relationships between sets, highlighting intersections and differences. They're perfect for analyzing customer overlap, product category relationships, and market segment intersections.
Venn diagrams excel at:
- Set Relationships: Visualize overlaps between different customer groups or categories
- Proportional Analysis: Compare relative sizes of segments and their intersections
- Market Insights: Understand shared characteristics across business segments
- Data Overlap: Identify unique and shared elements across categories
In [ ]:
Copied!
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import venn
import matplotlib.pyplot as plt
import pandas as pd
from pyretailscience.plots import venn
Two-Set Venn Diagram¶
Create a basic two-set Venn diagram. The DataFrame requires 'groups' and 'percent' columns where groups are tuples indicating set membership.
In [ ]:
Copied!
# Create sample customer overlap data for two segments
two_set_data = pd.DataFrame(
{
"groups": [(1, 0), (0, 1), (1, 1), (0, 0)],
"percent": [35, 28, 22, 15],
},
)
venn.plot(
two_set_data,
labels=["Premium Customers", "Frequent Buyers"],
title="Customer Segment Overlap",
)
plt.show()
# Create sample customer overlap data for two segments
two_set_data = pd.DataFrame(
{
"groups": [(1, 0), (0, 1), (1, 1), (0, 0)],
"percent": [35, 28, 22, 15],
},
)
venn.plot(
two_set_data,
labels=["Premium Customers", "Frequent Buyers"],
title="Customer Segment Overlap",
)
plt.show()
Three-Set Venn Diagram¶
Extend to three sets by adding a third dimension to the groups tuples and providing three labels.
In [ ]:
Copied!
# Create sample data for three product categories
three_set_data = pd.DataFrame(
{
"groups": [
(1, 0, 0),
(0, 1, 0),
(0, 0, 1), # Only in one category
(1, 1, 0),
(1, 0, 1),
(0, 1, 1), # In two categories
(1, 1, 1),
(0, 0, 0), # In all three or none
],
"percent": [18, 15, 12, 20, 14, 11, 8, 2],
},
)
venn.plot(
three_set_data,
labels=["Electronics", "Home & Garden", "Apparel"],
title="Product Category Customer Overlap",
)
plt.show()
# Create sample data for three product categories
three_set_data = pd.DataFrame(
{
"groups": [
(1, 0, 0),
(0, 1, 0),
(0, 0, 1), # Only in one category
(1, 1, 0),
(1, 0, 1),
(0, 1, 1), # In two categories
(1, 1, 1),
(0, 0, 0), # In all three or none
],
"percent": [18, 15, 12, 20, 14, 11, 8, 2],
},
)
venn.plot(
three_set_data,
labels=["Electronics", "Home & Garden", "Apparel"],
title="Product Category Customer Overlap",
)
plt.show()
Euler Diagram Mode¶
Use vary_size=True to create Euler diagrams where circle sizes are proportional to the actual values.
In [ ]:
Copied!
# Create data with varied segment sizes
euler_data = pd.DataFrame(
{
"groups": [(1, 0), (0, 1), (1, 1), (0, 0)],
"percent": [45, 20, 15, 20], # Different proportions
},
)
venn.plot(
euler_data,
labels=["Mobile Users", "Email Subscribers"],
vary_size=True,
title="Customer Channel Preferences (Proportional)",
)
plt.show()
# Create data with varied segment sizes
euler_data = pd.DataFrame(
{
"groups": [(1, 0), (0, 1), (1, 1), (0, 0)],
"percent": [45, 20, 15, 20], # Different proportions
},
)
venn.plot(
euler_data,
labels=["Mobile Users", "Email Subscribers"],
vary_size=True,
title="Customer Channel Preferences (Proportional)",
)
plt.show()
Custom Label Formatting¶
Use subset_label_formatter to customize how the values are displayed in each segment.
In [ ]:
Copied!
# Create sample market research data
market_data = pd.DataFrame(
{
"groups": [(1, 0), (0, 1), (1, 1), (0, 0)],
"percent": [32, 28, 25, 15],
},
)
venn.plot(
market_data,
labels=["Urban Markets", "High Income"],
subset_label_formatter=lambda value: f"{value}%",
title="Market Segment Analysis",
)
plt.show()
# Create sample market research data
market_data = pd.DataFrame(
{
"groups": [(1, 0), (0, 1), (1, 1), (0, 0)],
"percent": [32, 28, 25, 15],
},
)
venn.plot(
market_data,
labels=["Urban Markets", "High Income"],
subset_label_formatter=lambda value: f"{value}%",
title="Market Segment Analysis",
)
plt.show()