[Python actual combat basis] How to draw a relationship grid diagram to show the relationship between 8 nodes

Table of contents

1. The actual combat scene

2. Main points of knowledge

file read and write

Basic grammar

String handling

file generation

data construction

Three, rookie combat

1. Create a python file

2. Running results


1. The actual combat scene

Actual combat scenario: how to draw a relationship grid diagram to show the relationship between 8 nodes

2. Main points of knowledge

  • file read and write

  • Basic grammar

  • String handling

  • file generation

  • data construction

Three, rookie combat

Schedule now!

1. Create a python file

"""
Author: 菜鸟实战
实战场景:  如何绘制关系网格图展示8个节点间的关系
"""

# 导入系统包
import platform
from flask import Flask, render_template
from pyecharts import options as opts
from pyecharts.charts import *

print("Hello,菜鸟实战")
print("实战场景:  如何绘制关系网格图展示8个节点间的关系 \n")

web = Flask(__name__)

# 数据构建
nodes = [
    {"name": "结点1", "symbolSize": 10},
    {"name": "结点2", "symbolSize": 20},
    {"name": "结点3", "symbolSize": 30},
    {"name": "结点4", "symbolSize": 40},
    {"name": "结点5", "symbolSize": 50},
    {"name": "结点6", "symbolSize": 40},
    {"name": "结点7", "symbolSize": 30},
    {"name": "结点8", "symbolSize": 20},
]
links = []
for i in nodes:
    for j in nodes:
        links.append({"source": i.get("name"), "target": j.get("name")})


def graph_charts() -> Graph():
    # 实例化对象
    graph = Graph()
    graph.add("", nodes, links, repulsion=8000)
    # 全局置标题、标签
    graph.set_global_opts(
        title_opts=opts.TitleOpts(title="如何绘制关系网格图展示8个节点间的关系", subtitle="菜鸟实战,坚持学习!"),
        legend_opts=opts.LegendOpts(type_="scroll", pos_top="5%")
    )
    return graph


# 获取对象
p = graph_charts()
# 绘制图形,生成HTML文件的
p.render('./templates/graph_charts.html')


# 添加路由显示图表
@web.route('/')
def index():
    return render_template('graph_charts.html')


if __name__ == "__main__":
    # 运行项目
    web.run(debug=False)

print("Python 版本", platform.python_version())

2. Running results

Hello, rookie combat Actual
combat scenario: How to draw a relationship grid diagram to show the relationship between 8 nodes

 * Serving Flask app 'py037' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

Rookie combat, keep learning!   

Guess you like

Origin blog.csdn.net/qq_39816613/article/details/125732814