python--Venn图及进阶

简介

Venn图用来看几个集合相交的情况,通常下,2-4个集合看Venn图是比较直观的,但是集合数目再多就不适合了,需要引入另外一种图。

venn

3个


from venn import venn

musicians = {
    "Members of The Beatles": {"Paul McCartney", "John Lennon", "George Harrison", "Ringo Starr"},
    "Guitarists": {"John Lennon", "George Harrison", "Jimi Hendrix", "Eric Clapton", "Carlos Santana"},
    "Played at Woodstock": {"Jimi Hendrix", "Carlos Santana", "Keith Moon"}
}
venn(musicians)

python--Venn图及进阶_第1张图片

2-6个

from matplotlib.pyplot import subplots
from itertools import chain, islice
from string import ascii_uppercase
from numpy.random import choice

_, top_axs = subplots(ncols=3, nrows=1, figsize=(18, 5))
_, bot_axs = subplots(ncols=2, nrows=1, figsize=(18, 8))
cmaps = ["cool", list("rgb"), "plasma", "viridis", "Set1"]
letters = iter(ascii_uppercase)

for n_sets, cmap, ax in zip(range(2, 7), cmaps, chain(top_axs, bot_axs)):
    dataset_dict = {
        name: set(choice(1000, 700, replace=False))
        for name in islice(letters, n_sets)
    }
    venn(dataset_dict, fmt="{percentage:.1f}%", cmap=cmap, fontsize=8, legend_loc="upper left", ax=ax)

python--Venn图及进阶_第2张图片

upsetplot

可以更直观的看出几个set的的交集情况

api

https://upsetplot.readthedocs.io/en/stable/api.html

from upsetplot import generate_counts
example = generate_counts()
example 

这个数据可以通过对dataframe  groupby获得(类似数据透视表)

python--Venn图及进阶_第3张图片

from upsetplot import plot
plot(example) 

python--Venn图及进阶_第4张图片

from upsetplot import plot
plot(example,show_counts='%d')

python--Venn图及进阶_第5张图片

你可能感兴趣的:(python绘图)