Venn图用来看几个集合相交的情况,通常下,2-4个集合看Venn图是比较直观的,但是集合数目再多就不适合了,需要引入另外一种图。
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)
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)
可以更直观的看出几个set的的交集情况
api
https://upsetplot.readthedocs.io/en/stable/api.html
from upsetplot import generate_counts
example = generate_counts()
example
这个数据可以通过对dataframe groupby获得(类似数据透视表)
from upsetplot import plot
plot(example)
from upsetplot import plot
plot(example,show_counts='%d')