ColorEdges 动态有向图的动态渲染

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①有向动态图的创建,②图结构数据的可视化和布局展示

二:代码及注释

python 复制代码
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkIntArray, vtkLookupTable
from vtkmodules.vtkCommonDataModel import vtkMutableDirectedGraph
from vtkmodules.vtkViewsInfovis import vtkGraphLayoutView
from vtkmodules.vtkViewsCore import vtkViewTheme

def main():
    colors = vtkNamedColors()

    # 创建一个动态有向图
    graph = vtkMutableDirectedGraph()

    # AddVertex添加节点(顶点)
    v1 = graph.AddVertex()
    v2 = graph.AddVertex()
    v3 = graph.AddVertex()

    # AddGraphEdge 添加有向边
    graph.AddGraphEdge(v1, v2)
    graph.AddGraphEdge(v2, v3)

    # 创建颜色数组
    edgeColors = vtkIntArray()
    edgeColors.SetNumberOfValues(1)
    edgeColors.SetName('Color')
    edgeColors.InsertNextValue(0)
    edgeColors.InsertNextValue(1)

    # 创建颜色查找表
    lookupTable = vtkLookupTable()
    lookupTable.SetNumberOfTableValues(2)
    lookupTable.SetTableValue(0, colors.GetColor4d("Red"))
    lookupTable.SetTableValue(1, colors.GetColor4d("Lime"))
    lookupTable.Build()

    graph.GetEdgeData().AddArray(edgeColors)

    """
    vtkGraphLayoutView  专门用于图(Graph)结构数据" 的可视化和布局展示
    核心功能是:把一个由节点(Vertices)和边(Edges)组成的图结构,自动排布到 2D 或 3D 空间中,并渲染出来供你交互查看
    封装了布局(layout),显示(rendering),交互(interaction)
    """
    graphLayoutView = vtkGraphLayoutView()
    graphLayoutView.AddRepresentationFromInput(graph)
    # SetLayoutStrategy 设置布局
    graphLayoutView.SetLayoutStrategy('Simple 2D')
    # graphLayoutView.GetLayoutStrategy().SetEdgeWeightField('Graphs')
    # graphLayoutView.GetLayoutStrategy().SetWeightEdges(1)

    # 按属性字段映射边颜色
    graphLayoutView.SetEdgeColorArrayName('Color')
    graphLayoutView.SetEdgeLabelVisibility(1)
    graphLayoutView.ColorEdgesOn()

    """
    vtkViewTheme 
    用于统一设置一个 View(比如 vtkGraphLayoutView、vtkTreeMapView、vtkScatterPlotMatrixView 等)的 配色、字体、线条粗细、背景等整体视觉主题
    """
    theme = vtkViewTheme()
    theme.SetCellLookupTable(lookupTable)

    graphLayoutView.ApplyViewTheme(theme)
    graphLayoutView.ResetCamera()
    graphLayoutView.GetRenderer().GetActiveCamera().Zoom(0.8)
    graphLayoutView.Render()
    # graphLayoutView.GetLayoutStrategy().SetRandomSeed(0)
    graphLayoutView.GetInteractor().Initialize()
    graphLayoutView.GetInteractor().Start()



if __name__ == '__main__':
    main()
相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1234 天前
2026年第38周GitHub趋势周报
python·科技·github
IZero074 天前
Jev 与 Laya
python·语言模型