jupyter lab下用pyecharts生成细胞分类桑基图

①加载依赖&文件

import pandas as pd

from pyecharts.charts import Sankey
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.globals import CurrentConfig, NotebookType  
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB

df = pd.read_csv('data/sankey.csv')

jupyter lab下用pyecharts生成细胞分类桑基图_第1张图片

②格式转换

for idx, cell_type in enumerate(set(df['Normal Mapping'])):
    if idx == 0:
        buff = df.loc[df['Normal Mapping'] == cell_type, 'Overlap mapping'].value_counts()
        buff_dic = {'Normal Mapping' : cell_type + '__N', 'Overlap mapping' : buff.index + '__O', 'counts' : buff.values}
        df_new = pd.DataFrame(buff_dic)
    else:
        buff = df.loc[df['Normal Mapping'] == cell_type, 'Overlap mapping'].value_counts()
        buff_dic = {'Normal Mapping' : cell_type + '__N', 'Overlap mapping' : buff.index + '__O', 'counts' : buff.values}
        df_buff = pd.DataFrame(buff_dic)
        df_new = pd.concat((df_new, df_buff), ignore_index=True)

jupyter lab下用pyecharts生成细胞分类桑基图_第2张图片

③制作nodes

nodes = []

for idx, i in enumerate(range(2)):
    values = df_new.iloc[:,i].unique()
    if idx == 0:
        values_all = list(values)
        for value in values:
            dic = {}
            dic['name'] = value
            nodes.append(dic)
    else:
        for value in values:
            if value not in values_all:
                dic = {}
                dic['name'] = value
                nodes.append(dic)
                values_all.append(value)

nodes

jupyter lab下用pyecharts生成细胞分类桑基图_第3张图片

④制作links

linkes = []

for i in df_new.values:
    dic = {}
    dic['source'] = i[0]
    dic['target'] = i[1]
    dic['value'] = int(i[2])
    linkes.append(dic)

linkes

jupyter lab下用pyecharts生成细胞分类桑基图_第4张图片

⑤绘图

pic = (
    Sankey()
    .add('', #图例名称
         nodes,    #传入节点数据
         linkes,   #传入边和流量数据
         #设置透明度、弯曲度、颜色
         linestyle_opt=opts.LineStyleOpts(opacity = 0.3, curve = 0.5, color = "source"),
         #标签显示位置
         label_opts=opts.LabelOpts(position="right"),
         #节点之前的距离
         node_gap = 15,
    )
    .set_global_opts(title_opts=opts.TitleOpts(title = '细胞分类对比'))
)
pic.load_javascript()
pic.render_notebook()

注意:load_javascript() 和 render_notebook()在两个cell中运行

jupyter lab下用pyecharts生成细胞分类桑基图_第5张图片

参考:
①:https://pyecharts.org/#/zh-cn/basic_charts?id=sankey%ef%bc%9a%e6%a1%91%e5%9f%ba%e5%9b%be
②:https://www.jianshu.com/p/dde9d4072e31

如果无法显示:
jupyter lab下用pyecharts生成桑基图显示空白

你可能感兴趣的:(Bioinformation,jupyter,分类,python)