关于pyecharts可视化进阶中国经济、人口等数据

2022-03-11 14:32:05 浏览数 (2)

pyecharts提供了一系列图表功能,如Calendar:日历图,Funnel:漏斗图,Gauge:仪表盘,Graph:关系图,Liquid:水球图,Parallel:平行坐标系,Pie:饼图,Polar:极坐标系,Radar:雷达图,Sankey:桑基图,Sunburst:旭日图,ThemeRiver:主题河流图,WordCloud:词云图。

其中仪表盘、水球图,可视化功效大于实效,适合宏观指标展示;漏斗图功能较单一,比较适合数据的层层递进;平行坐标系相比桑基图可视化效果差很多,桑基图比较适合多维度分析,总量不变,看不同维度数据的交叉情况;旭日图适合层级数据展现;河流图适合按日期的KPI波动展示;饼图适合少量数据的定性分析,不过其环饼图和玫瑰环图还是挺有意思的;词云图适合可视化文本展示;关系图适合知识图谱类、人物关系类的呈现。

基本还是沿用了之前的中国经济、人口数据,不过很多场景不适合这类数据,花了不少精力在构造数据上,字典、元组、列表、数组在不停转换,也缺少优化,仅供参考。

代码如下:

代码语言:javascript复制
import cx_Oracle
import pandas as pd
import os
import numpy as np
import random
from collections import OrderedDict
import json

from pyecharts import options as opts
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode
from pyecharts.globals import SymbolType
from pyecharts.globals import ThemeType

from pyecharts.charts import Funnel,Gauge,Graph,Liquid,Parallel,Pie,Polar,Radar,Sankey,Sunburst,ThemeRiver,WordCloud,Gauge, Page


# theme 定制图表样式,共内置15种
# ThemeType.WHITE|ThemeType.LIGHT|ThemeType.DARK|ThemeType.CHALK|ThemeType.ESSOS|
# ThemeType.INFOGRAPHIC|ThemeType.MACARONS|ThemeType.PURPLE_PASSION|ThemeType.ROMA|ThemeType.ROMANTIC|
# ThemeType.SHINE|ThemeType.VINTAGE|ThemeType.WALDEN|ThemeType.WESTEROS|Type.WONDERLAND

# 全局配置项
# InitOpts:初始化配置项
# TitleOpts:标题配置项
# AnimationOpts:Echarts 画图动画配置项
# ToolBoxFeatureOpts:工具箱工具配置项
# ToolboxOpts:工具箱配置项
# BrushOpts:区域选择组件配置项
# DataZoomOpts:区域缩放配置项
# LegendOpts:图例配置项
# VisualMapOpts:视觉映射配置项
# TooltipOpts:提示框配置项
# AxisLineOpts: 坐标轴轴线配置项
# AxisTickOpts: 坐标轴刻度配置项
# AxisPointerOpts: 坐标轴指示器配置项
# AxisOpts:坐标轴配置项
# SingleAxisOpts:单轴配置项
# GraphicGroup:原生图形元素组件

# 系列配置项
# ItemStyleOpts:图元样式配置项
# TextStyleOpts:文字样式配置项
# LabelOpts:标签配置项
# LineStyleOpts:线样式配置项
# SplitLineOpts:分割线配置项
# MarkPointItem:标记点数据项
# MarkPointOpts:标记点配置项
# MarkLineItem:标记线数据项
# MarkLineOpts:标记线配置项
# MarkAreaItem: 标记区域数据项
# MarkAreaOpts: 标记区域配置项
# EffectOpts:涟漪特效配置项
# AreaStyleOpts:区域填充样式配置项
# SplitAreaOpts:分隔区域配置项

'''
数据情况,考虑到pandas对数据处理的不便,还是使用了标准的数据库方式
------------------------------------------------------
表名称:chinaeconomy 中国各省市自治区1950-2010年总体情况表
REPORTYEAR    年度
PROVINCEID    省市编号
PROVINCENAME    省市名称
GDP    地区国内生产总值(亿元)
EVERAGEGDP    人均国内生产总值(元)
PRIMARYINDUSTRYVALUE    各地区第一产业增加值(亿元)
SECONDINDUSTRYVALUE    各地区第二产业增加值(亿元)
THIRDINDUSTRYVALUE    各地区第三产业增加值(亿元)
IMPORTEXPORTVALUE    进出口总额(万美元)
EXPORTVALUE    出口总额(万美元)
POPULATION    年底总人口数(万人)
BIRTHRATE    人口出生率(‰)
DEADRATE    人口死亡率(‰)
GROWTHRATE    人口自然增长率(‰)
------------------------------------------------------
表名称:provinceinfo 中国各省市自治区对应echart中的省市自治区简写和行政区域
PROVINCENAME    省市名称
ECHARTPROVINCENAME  echart省市名称
AREANAME    行政区划
------------------------------------------------------
基本SQL语句
select b.areaname,b.echartprovincename,a.reportyear,
       a.gdp,a.everagegdp,
       a.primaryindustryvalue,a.secondindustryvalue,a.thirdindustryvalue,
       a.importexportvalue,a.exportvalue,
       a.population,a.birthrate,a.deadrate,a.growthrate
  from chinaeconomy a,proviceinfo b
 where a.provincename=b.provincename
'''

# 初始化
username = 'metadata'
password = 'Wbq197711'
connstr ='127.0.0.1:1521/ORCL'
conn = cx_Oracle.connect(username,password,connstr) #连接数据库

# 柱状图 六大行政区划 进出口数据对比
sql='''
select b.areaname,
       sum(a.importexportvalue) as importexportvalue,sum(a.exportvalue)/10000 as exportvalue,sum(a.importexportvalue-a.exportvalue)/10000 as importvalue
  from chinaeconomy a,proviceinfo b
 where a.provincename=b.provincename
   and a.reportyear=2010
 group by b.areaname
 order by 2
 '''
df = pd.read_sql(sql,conn)  #read_sql读取数据
xdata=df['AREANAME'].tolist()
exportvalue=df['EXPORTVALUE'].round(2).tolist()  #四舍五入,再转list
importvalue=df['IMPORTVALUE'].round(2).tolist()  #四舍五入,再转list
importexportvalue=df['IMPORTEXPORTVALUE'].round(2).tolist()  #四舍五入,再转list
# 漏斗图
def funnel_label_inside() -> Funnel:
    c = (
        Funnel()
        .add(
            "出口",
            [list(z) for z in zip(xdata, exportvalue)],
            sort_="ascending",
            label_opts=opts.LabelOpts(position="inside"),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Funnel-Label(inside)"),
                         legend_opts = opts.LegendOpts(is_show=True,pos_left="right",pos_top="7%",orient='vertical')
                         )
    )
    return c
c=funnel_label_inside()
c.render('funnel_label_inside.html')


# 仪表盘
def gauge_report() -> Gauge:
    c = (
        Gauge()
        .add("A", [("及格率", 28.6),("完成率", 69.6),("成功率", 99.6)],is_selected = True,
             axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(
                    color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30
                )
            ),)
        .set_global_opts(title_opts=opts.TitleOpts(title="Gauge-基本示例"))
    )
    return c
c=gauge_report()
c.render('gauge_report.html')


def pie_base() -> Pie:
    c = (
        Pie(init_opts=opts.InitOpts(        #初始化配置项
                theme=ThemeType.LIGHT        #设置主题
            ))
        .add("", [list(z) for z in zip(xdata, importexportvalue)],
            center=["35%", "50%"],)
        .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"),
                         legend_opts=opts.LegendOpts(pos_left="15%"),)
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    return c
c=pie_base()
c.render('pie_base.html')

# 饼环
def pie_radius() -> Pie:
    c = (
        Pie()
        .add(
            "",
            [list(z) for z in zip(xdata, importexportvalue)],
            radius=["40%", "75%"],  #饼环设置
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="Pie-Radius"),
            legend_opts=opts.LegendOpts(    #图例设置
                orient="vertical", pos_top="15%", pos_left="2%"
            ),
        )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    return c
c=pie_radius()
c.render('pie_radius.html')

# 玫瑰图实例
def pie_rosetype() -> Pie:
    # 是否展示成南丁格尔图,通过半径区分数据大小,有'radius'和'area'两种模式。
    # radius:扇区圆心角展现数据的百分比,半径展现数据的大小
    # area:所有扇区圆心角相同,仅通过半径展现数据大小
    c = (
        Pie()
        .add(
            "",
            [list(z) for z in zip(xdata, importvalue)],
            radius=["30%", "75%"],
            center=["25%", "50%"],
            rosetype="radius",
            label_opts=opts.LabelOpts(is_show=True),
        )
        .add(
            "",
            [list(z) for z in zip(xdata, exportvalue)],
            radius=["30%", "75%"],
            center=["75%", "50%"],
            rosetype="area",
            label_opts=opts.LabelOpts(is_show=True),
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
    )
    return c
c=pie_rosetype()
c.render('pie_rosetype.html')

sql='''
select b.provincename,
        a.birthrate,a.deadrate
  from chinaeconomy a,proviceinfo b
 where a.provincename=b.provincename
   and a.reportyear=2010
 order by 1
 '''

df = pd.read_sql(sql,conn)  #read_sql读取数据
xdata=df['PROVINCENAME'].tolist()
birthrate=df['BIRTHRATE'].round(2).tolist()  #四舍五入,再转list
deadrate=df['DEADRATE'].round(2).tolist()  #四舍五入,再转list
birthratedata = [list(z) for z in zip(birthrate,range(31))]
deadratedata = [list(z) for z in zip(deadrate,range(31))]

def polar_scatter0() -> Polar:
    c = (
        Polar()
        .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=xdata, type_="category",
                                                      axislabel_opts=opts.LabelOpts(font_size=8, interval=0, rotate=45)))   #替换坐标系标签
        .add("", birthratedata, type_="scatter", label_opts=opts.LabelOpts(is_show=False))
        .add("", deadrate, type_="scatter")
        .set_global_opts(title_opts=opts.TitleOpts(title="Polar-Scatter0"))
    )
    return c
c=polar_scatter0()
c.render('polar_scatter0.html')

def polar_angleaxis() -> Polar:
    c = (
        Polar()
        .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=xdata, type_="category",
                                                      axislabel_opts=opts.LabelOpts(font_size=10, interval=0, rotate=45)))   #替换坐标系标签
        .add("出生率", birthratedata, type_="bar", label_opts=opts.LabelOpts(is_show=False), stack="stack0")
        .add("死亡率", deadratedata, type_="bar", label_opts=opts.LabelOpts(is_show=False), stack="stack0")
        .set_global_opts(title_opts=opts.TitleOpts(title="Polar-AngleAxis"))
    )
    return c
c=polar_angleaxis()
c.render('polar_angleaxis.html')

sql='''
select b.echartprovincename,
       a.primaryindustryvalue,
       a.secondindustryvalue,
       a.thirdindustryvalue
  from chinaeconomy a,proviceinfo b
 where a.provincename=b.provincename
   and a.reportyear=2010
 order by b.areaname
 '''
df = pd.read_sql(sql,conn)  #read_sql读取数据
xdata=df['ECHARTPROVINCENAME'].tolist()
primaryindustryvalue=[df['PRIMARYINDUSTRYVALUE'].round(2).tolist()]  #四舍五入,再转list
secondindustryvalue=[df['SECONDINDUSTRYVALUE'].round(2).tolist()]  #四舍五入,再转list
thirdindustryvalue=[df['THIRDINDUSTRYVALUE'].round(2).tolist()]  #四舍五入,再转list

dictlist=[]
for i in xdata:
    tempdict={}
    tempdict['name']=str(i)
    tempdict['max']=23014.53
    tempdict['min'] = 68.72
    dictlist.append(tempdict)

# 雷达图
def radar_air_quality() -> Radar:
    c = (
        Radar()
        .add_schema(schema=dictlist, shape="circle")
        .add("第一产业", primaryindustryvalue, color="#339933")
        .add("第二产业", secondindustryvalue, color="#FFCC33")
        .add("第三产业", thirdindustryvalue,color="#336699")
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(title_opts=opts.TitleOpts(title="Radar-空气质量"))
    )
    return c
c=radar_air_quality()
c.render('radar_air_quality.html')

# 生成一个长度为N的list,数据之和为totalvale
def random_sum(n,totalvalue):
    listrand= [random.randint(0, totalvalue) for i in range(n-1)]
    listrand.sort()
    listresult=[]
    listresult.append(listrand[0])
    for i in range(n-2):
        listresult.append(listrand[i   1] - listrand[i])
    listresult.append(totalvalue - listrand[n-2])
    return listresult

# 定义4层以及每层数量
tier1len=8
tier2len=4
tier3len=7
tier4len=12

# 定义节点的key值
dictname = ['name']

#生成一维list,为后续生成各层关系做准备
tier1=['tier1_' str(i) for i in range(tier1len)]
tier2=['tier2_' str(i) for i in range(tier2len)]
tier3=['tier3_' str(i) for i in range(tier3len)]
tier4=['tier4_' str(i) for i in range(tier4len)]
tiers=tier1 tier2 tier3 tier4
# 生成二维list,为后续生成节点(格式为字典list)
tiers2=[['tier1_' str(i)] for i in range(tier1len)] 
       [['tier2_' str(i)] for i in range(tier2len)] 
       [['tier3_' str(i)] for i in range(tier3len)] 
       [['tier4_' str(i)] for i in range(tier4len)]
# 生成桑基图所需的节点,如果一维list输出有问题
dicts = [dict(zip(dictname, values)) for values in tiers2]
# dicts= [
# {'name': 'tier1_0'}, {'name': 'tier1_1'}, {'name': 'tier1_2'}, {'name': 'tier1_3'}, {'name': 'tier1_4'},
# {'name': 'tier2_0'}, {'name': 'tier2_1'}, {'name': 'tier2_2'}, {'name': 'tier2_3'},
# {'name': 'tier3_0'}, {'name': 'tier3_1'}, {'name': 'tier3_2'}, {'name': 'tier3_3'}, {'name': 'tier3_4'}, {'name': 'tier3_5'}, {'name': 'tier3_6'},
# {'name': 'tier4_0'}, {'name': 'tier4_1'}, {'name': 'tier4_2'}, {'name': 'tier4_3'}, {'name': 'tier4_4'}, {'name': 'tier4_5'}]

def create_links(source,target,combvalue,sumvalue):
    # 定义桑基图关系的字典key
    dicntname=['source','target','value']
    # 将源和目标组合生成笛卡尔集
    cartlist=list([x,y] for x in source for y in target)
    # 对笛卡尔集随机排序
    np.random.shuffle(cartlist)
    # 取出前N个组合,N=combvalue
    comblist=cartlist[:combvalue]
    # 再将生成的N个组合的值取出来,此处有些犯傻
    newsource,newtarget=zip(*comblist)
    # 再生成这N个组合的value值,确保所有值相加等某个数值sumvalue
    data = random_sum(combvalue, sumvalue)

    # 将源、关系、数据等list数据写入一个二维list
    alldata=[]
    for i in range(combvalue):
        alldata.append([newsource[i],newtarget[i],data[i]])
    # 再将这个二维list生成字典list
    dicts = [dict(zip(dicntname, values)) for values in alldata]
    # 再返回当层所需要的关系,关系为字典list格式
    return dicts
#定义第1,2,3层的关系,参数分别为源list,目标list,关系数量,数据总值,返回为一个字典list
link1=create_links(tier1,tier2,20,1000)
link2=create_links(tier2,tier3,15,1000)
link3=create_links(tier3,tier4,30,1000)
linksdata=link1 link2 link3

def sankey_base() -> Sankey:
    nodes = dicts
    links = linksdata
    c = (
        Sankey()
        .add(
            "sankey",
            nodes,
            links,
            linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
            label_opts=opts.LabelOpts(position="right"),
            # 线性的桑基图,意义不大
            # levels=[
            #     opts.SankeyLevelsOpts(
            #         depth=0,
            #         itemstyle_opts=opts.ItemStyleOpts(color="#fbb4ae"),
            #         linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            #     ),
            #     opts.SankeyLevelsOpts(
            #         depth=1,
            #         itemstyle_opts=opts.ItemStyleOpts(color="#b3cde3"),
            #         linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            #     ),
            #     opts.SankeyLevelsOpts(
            #         depth=2,
            #         itemstyle_opts=opts.ItemStyleOpts(color="#ccebc5"),
            #         linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            #     ),
            #     opts.SankeyLevelsOpts(
            #         depth=3,
            #         itemstyle_opts=opts.ItemStyleOpts(color="#decbe4"),
            #         linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6),
            #     ),
            # ],
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例"))
    )
    return c
c=sankey_base()
c.render('sankey_base.html')


sql='''
select b.areaname,b.echartprovincename name,a.gdp value 
  from chinaeconomy a,proviceinfo b
 where a.provincename=b.provincename
   and a.reportyear=2010
 order by 1
'''
df = pd.read_sql(sql,conn)  #read_sql读取数据
results = []

for (areaname), bag in df.groupby(["AREANAME"]):
    # areaname= 东北地区
    # bag=   AREANAME ECHARTPROVINCENAME       GDP
    # 0     东北地区                 辽宁  18457.30
    # 1     东北地区                黑龙江  10368.60
    # 2     东北地区                 吉林   8667.58
    contents_df = bag.drop(["AREANAME"], axis=1)
    # contents_df=   ECHARTPROVINCENAME       GDP
    # 0                 辽宁  18457.30
    # 1                黑龙江  10368.60
    # 2                 吉林   8667.58
    children = [OrderedDict(name=row[0],value=row[1]) for i,row in contents_df.iterrows()]
    # children= [
    #   OrderedDict([('name', '辽宁'), ('value', 18457.3)]),
    #   OrderedDict([('name', '黑龙江'), ('value', 10368.6)]),
    #   OrderedDict([('name', '吉林'), ('value', 8667.58)])]
    results.append(OrderedDict([("name", areaname),("children", children)]))
    # result= [
    # OrderedDict([('name', '东北地区'),
    #   ('children',
    #       [OrderedDict([('name', '辽宁'), ('value', 18457.3)]),
    #       OrderedDict([('name', '黑龙江'), ('value', 10368.6)]),
    #       OrderedDict([('name', '吉林'), ('value', 8667.58)])])])]
with open('areaprovincegdp.json', 'w', encoding="utf-8") as outfile:
    outfile.write(json.dumps(results, indent=4))
    # [
    #     {
    #         "name": "u4e1cu5317u5730u533a",
    #         "children": [
    #             {
    #                 "name": "u8fbdu5b81",
    #                 "value": 18457.3
    #             },
    #             {
    #                 "name": "u9ed1u9f99u6c5f",
    #                 "value": 10368.6
    #             },
    #             {
    #                 "name": "u5409u6797",
    #                 "value": 8667.58
    #             }
    #         ]
    #     },
    #    {}]

#  旭日图
def sunburst_official() -> Sunburst:
    with open(os.path.join("", "areaprovincegdp.json"), "r", encoding="utf-8") as f:
        j = json.load(f)

    c = (
        Sunburst(init_opts=opts.InitOpts(width="1000px", height="600px"))
        .add(
            "",
            data_pair=j,
            highlight_policy="ancestor",
            radius=[0, "95%"],
            sort_="null",
            levels=[
                {},
                {
                    "r0": "15%",
                    "r": "35%",
                    "itemStyle": {"borderWidth": 2},
                    "label": {"rotate": "tangential"},
                },
                {"r0": "35%", "r": "70%", "label": {"align": "right","position": "outside", "padding": 3, "silent": False}},
                #{"r0": "35%", "r": "70%", "label": {"align": "right"}),
                {
                    "r0": "70%",
                    "r": "72%",
                    "label": {"position": "outside", "padding": 3, "silent": False},
                    "itemStyle": {"borderWidth": 3},
                },
            ],
        )
        .set_global_opts(title_opts=opts.TitleOpts(title="Sunburst-官方示例"),)
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
    )
    return c

c=sunburst_official()
c.render('sunburst_official.html')


sql='''
select a.reportyear,b.areaname,a.population 
from chinaeconomy a,proviceinfo b
where a.provincename=b.provincename
 '''
df = pd.read_sql(sql,conn)  #read_sql读取数据

res=pd.pivot_table(df,index=["AREANAME"],values=["POPULATION"],
               columns=["REPORTYEAR"],aggfunc=[np.sum])

#获取时间列表
xdata= [colname[2] for colname in res.columns.tolist()] #时间
#获取省市自治区列表
ydata= res.index.tolist()   #省市自治区直辖市
#定义新数据列表
newdata=[]
#获取行列维度,其中行对应省份,列对应时间
row,col=res.values.shape
# 将pandas数据格式化为河流图的(时间,指标,维度)格式
for i in range(row):
    for j in range(col):
        newdata.append([str(xdata[j]),res.values[i,j],ydata[i]])
#河流图,但条目太多
def themeriver_example() -> ThemeRiver:
    data = newdata
    c = (
        ThemeRiver()
        .add(ydata,
             newdata,
             singleaxis_opts=opts.SingleAxisOpts(type_="time", pos_bottom="10%"),
             )
        .set_global_opts(title_opts=opts.TitleOpts(title="ThemeRiver-基本示例"),
                         legend_opts=opts.LegendOpts(pos_right="20%"),
                         xaxis_opts=opts.AxisOpts(name="我是 X 轴",
                                                  axislabel_opts=opts.LabelOpts(font_size=12, interval=0,rotate=45)
                                                 ),
                         yaxis_opts=opts.AxisOpts(name="我是 Y 轴",
                                                  axislabel_opts=opts.LabelOpts(font_size=8, interval=0, rotate=45)
                                                  )
                         )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
    )
    return c
c=themeriver_example()
c.render('themeriver_example.html')



sql='''
select b.echartprovincename,
        (a.primaryindustryvalue a.secondindustryvalue a.thirdindustryvalue) industryvalue
from chinaeconomy a,proviceinfo b
where a.provincename=b.provincename
  and a.reportyear=2010
  and a.population is not null
 '''
df = pd.read_sql(sql,conn)  #read_sql读取数据
echartprovincename=df['ECHARTPROVINCENAME'].tolist()
industryvalue=df['INDUSTRYVALUE'].round(2).tolist()  #
words=[list(z) for z in zip(echartprovincename, industryvalue)]
print(words)
def wordcloud_base() -> WordCloud:
    c = (
        WordCloud()
        .add("", words, word_size_range=[20, 100], shape=SymbolType.DIAMOND)
        .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
    )
    return c
c=wordcloud_base()
c.render('wordcloud_base.html')


def graph_weibo() -> Graph:
    with open(os.path.join("", "weibo.json"), "r", encoding="utf-8") as f:
        j = json.load(f)
        nodes, links, categories, cont, mid, userl = j
        print(len(nodes))
        print(len(links))
    print('j=',j)

    print('nodes=',nodes )
    #  [{
    #             "name": "",
    #             "symbolSize": 5,
    #             "draggable": "False",
    #             "value": 1,
    #             "category": "",
    #             "label": {
    #                 "normal": {
    #                     "show": "True"
    #                 }
    #             }
    #         },
    print('links=', links)
    #  [{
    #             "source": "新浪体育",
    #             "target": "阿根廷人小马"
    #         },
    print('categories=',categories )
    # [{
    #             "name": ""
    #         },
    # print('cont=',cont )
    # print('mid=',mid )
    # print('userl=',userl )
    c = (
        Graph()
        .add(
            "",
            nodes,
            links,
            categories,
            repulsion=50,
            linestyle_opts=opts.LineStyleOpts(curve=0.2),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            title_opts=opts.TitleOpts(title="Graph-微博转发关系图"),
        )
    )
    return c
c=graph_weibo()
c.render('graph_weibo.html')

firstname=['王','李','张','刘','陈','杨','黄','赵','吴','周','徐','孙','马','朱','胡','郭','何','高','林','罗','郑','梁','谢','宋','唐',
           '许','韩','冯','邓','曹','彭','曾','萧','田','董','潘','袁','于','蒋','蔡','余','杜','叶','程','苏','魏','吕','丁','任','沈',
           '姚','卢','姜','崔','钟','谭','陆','汪','范','金','石','廖','贾','夏','韦','傅','方','白','邹','孟','熊','秦','邱','江','尹',
           '薛','阎','段','雷','侯','龙','史','陶','黎','贺','顾','毛','郝','龚','邵','万','钱','严','覃','武','戴','莫','孔','向','汤']
lastname=['彬','斌','兵','波','博','畅','超','晨','成','梅','丹','东','帆','芳','飞','峰','兰','英','刚','花',
          '荣','香','珍','芝','燕','浩','红','霞','华','欢','辉','慧','佳','建','国','军','平','健','杰','洁',
          '凤','晶','静','娟','俊','凯','坤','雷','磊','丽','利','莉','亮','林','琳','玲','柳','龙','璐','敏',
          '明','娜','楠','宁','鹏','萍','倩','强','琴','瑞','帅','涛','婷','伟','文','志','欣','鑫','云','旭',
          '雪','岩','艳','阳','杨','洋','莹','颖','勇','瑜','宇','玉','春','冬','桂','海','金','淑','小','秀']
groupname=['组1','组2','组3','组4','组5','组6','组7','组8','组9','组10']
#生成100个名字
#给100个名字随机分配到10个组中
#给100个名字到100个名字随机生成2000个关系

#生成10000个组
tmpgroupname=groupname*1000
#打乱排序
np.random.shuffle(tmpgroupname)
#取前100个组
groupnames=tmpgroupname[:100]
print(groupnames)

#生成10000个名字
cartnames=[fname mname lname for fname in firstname for mname in lastname for lname in lastname]
#打乱排序
np.random.shuffle(cartnames)
#取前100个名字
usernames=cartnames[:100]

# 生成用户和组名关系表
usernamegroup=list(zip(usernames,groupnames))
# [('高磊丽', '组6'), ('韩畅凯', '组5'), ('韦佳珍', '组10'), ('潘华雷', '组6'), ('向芝梅', '组5')

import itertools
#把这100个名字生成10000个笛卡尔集源和目标关系
cartrelationname=list(itertools.product(usernames,usernames))
#打乱排序
np.random.shuffle(cartrelationname)
#取前1000个关系
relationname=cartrelationname[:1000]
#[('彭娜杨', '贺璐冬'), ('杜欣艳', '向芝梅'), ('刘健璐', '董峰淑'), ('谭丽燕', '韩帆颖'),

nodedictname=['name','category']
linkdictname=['source','target']
groupdictname=['name']

groupnamedict=[]
for tmpgroupname in groupname:
    groupnamedict.append({'name':tmpgroupname})
#groupnamedict=[dict(zip(groupdictname, values)) for values in groupnames]
linkdict=[dict(zip(linkdictname, values)) for values in relationname]
usernamedict=[dict(zip(nodedictname, values)) for values in usernamegroup]
#dicts = [dict(zip(dicntname, values)) for values in alldata]

def graph_relation() -> Graph:
    nodes, links, categories= usernamedict,linkdict,groupnamedict
    c = (
        Graph()
        .add(
            "",
            nodes,
            links,
            categories,
            repulsion=50,
            linestyle_opts=opts.LineStyleOpts(curve=0.2),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False),
            title_opts=opts.TitleOpts(title="人物关系图"),
        )
    )
    return c
c=graph_relation()
c.render('graph_relation.html')

0 人点赞