Pyecharts Graph 关系图示例

本文通过Python的Pyecharts库详细介绍了如何绘制Graph关系图,内容涵盖数据准备、图表配置及展示,适合数据可视化和分析的学习者参考。

​
#关系可视化
from pyecharts import options as opts
from pyecharts.charts import Graph
from pyecharts.globals import ThemeType

import webbrowser

#生成大量节点
nodes=[]
for i in range(20):
    #node={"name":"节点"+str(i),"symbolsize":20}
    node=opts.GraphNode(name="节点"+str(i),   #节点名称 ★ 不能有重名!
                        symbol_size=10+i,   #节点大小
                        symbol='image://cat_brown.svg'   #★★★★★ 节点样式可选:'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none', 'image://url'
                        )
    nodes.append(node)

   
#节点间连接线样式设置
linestyle_opts=opts.LineStyleOpts(is_show=True,
                                width=1,
                                opacity=0.6,
                                curve=0.3,
                                type_="solid", 
                                color="red" 
                                )

#连接所有节点
links = []
for i in nodes:
    for j in nodes:    
        source_node_name=i.get("name")
        target_node_name=j.get("name")        
        links.append({"source":source_node_name,"target":target_node_name,"lineStyle":linestyle_opts})
        
#主要设置
#InitOpts:初始化配置项(在图形创建开始时即可设置)
init_opts=opts.InitOpts(width="100%",   #图宽
                        height="900px", #图高
                        renderer="canvas", #渲染模式 svg 或 canvas,即 RenderType.CANVAS 或 RenderType.SVG
                        page_title="Pyecharts Graph关系图",    #网页标题
                        theme=ThemeType.DARK,  #主题风格可选:WHITE,LIGHT,DARK,CHALK,ESSOS,INFOGRAPHIC,MACARONS,PURPLE_PASSION,ROMA,ROMANTIC,SHINE,VINTAGE,WALDEN,WESTEROS,WONDERLAND
                        #bg_color="#333333",    #背景颜色
                        js_host=""  #js主服务位置 留空则默认官方远程主服务
                        )

#ToolboxOpts:工具栏配置(可实现图片保存等功能)
toolbox_opts=opts.ToolboxOpts(is_show=True, #是否显示工具栏
                            orient="vertical",  #工具栏工具摆放方向
                            pos_left="right")   #工具栏左边位置
                            

#★★★★★       
#通过opts.ItemStyleOpts,设置node节点样式(颜色、大小、透明度)     
itemstyle_opts=opts.ItemStyleOpts(color="orange", #节点颜色
                                    border_color="red", #节点边线颜色
                                    border_width=1,    #节点边线宽度
                                    opacity=0.9)    #节点透明度
                                    

c = (
    Graph(init_opts)
    .add("", nodes, links, repulsion=8000,itemstyle_opts=itemstyle_opts,is_draggable=True)
    .set_global_opts(title_opts=opts.TitleOpts(title="Pyecharts Graph 示例"),toolbox_opts=toolbox_opts)
    .render("graph.html")
)

#---------------------------------------









#---------------------------------------

from flask import Flask,g,request,render_template,Response,stream_with_context
import threading
import time


#---------------------------------------
app=Flask(__name__,static_url_path='',template_folder='',static_folder='')

@app.route('/')
def index():
    return render_template("graph.html")
    
#---------------------------------------

def run_app():
    print("应用启动")
    if __name__ == '__main__':
        app.run("0.0.0.0")

def open_browser():
    time.sleep(1)
    print("打开浏览器")
    webbrowser.open("http://localhost:5000")
    
def main():
    #主要程序
    # 创建线程
    thread1 = threading.Thread(target=open_browser)
    thread2 = threading.Thread(target=run_app)
    # 启动线程
    thread1.start()
    thread2.start()

if __name__ == '__main__':
    main()


​

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值