Plot venn diagram with pandas and matplotlib_venn

HonzaB picture HonzaB · Jun 9, 2016 · Viewed 13.3k times · Source

I'd like to plot venn diagrams based on my pandas data frame. I understand that matplotlib_venn accepts sets as input. My dataset contain client id and two other columns with information if the client was in campaign or not.

df_dataset = pd.read_csv('...path...',delimiter=',',decimal=',')
campaign_a = df_dataset[(df_dataset['CAM_A'] == 1)] 
campaign_b = df_dataset[(df_dataset['CAM_B'] == 1)]

plt.figure(figsize=(4,4))
set1 = set(campaign_a['CLI_ID'])
set2 = set(campaign_b['CLI_ID'])

venn3([set1, set2], ('Set1', 'Set2'))
plt.show()

However I get an error:

File "C:\Python27\Lib\site-packages\matplotlib_venn_venn3.py", line 44, in compute_venn3_areas areas = np.array(np.abs(diagram_areas), float)

TypeError: bad operand type for abs(): 'set'

Answer

Ignacio Carvajal picture Ignacio Carvajal · Mar 1, 2018

This error is a result of trying to force 2 sets into venn3. You need to import venn2 from the same library.

from matplotlib_venn import venn2

df_dataset = pd.read_csv('...path...',delimiter=',',decimal=',')
campaign_a = df_dataset[(df_dataset['CAM_A'] == 1)] 
campaign_b = df_dataset[(df_dataset['CAM_B'] == 1)]

plt.figure(figsize=(4,4))
set1 = set(campaign_a['CLI_ID'])
set2 = set(campaign_b['CLI_ID'])

venn2([set1, set2], ('Set1', 'Set2'))
plt.show()