You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在igraph网络图中使用权重列表设置线条宽度

Fixing 3D Edge Line Widths with igraph and Plotly

The issue you're running into is that Plotly's Scatter3d trace doesn't support assigning multiple line widths in a single trace—the line.width parameter expects a single numeric value, not a list. To get edge-specific line widths, you need to create a separate Scatter3d trace for each edge, each using its own weight as the line width.

Here's how to adjust your code to make this work:

First, generate a 3D layout for your graph (if you haven't already done this to get Xe/Ye/Ze):

import igraph as ig
import plotly.graph_objects as go

# Your original graph setup
eG = ig.Graph(directed=False)
eG.add_vertices(6)
eG.add_edges([(0,1),(1,2),(2,3),(3,4),(3,5),(5,3)])
eG.es['weight'] = [12,1,2,3,4,1]

# Generate 3D layout (using Kamada-Kawai algorithm for balanced spacing)
layout = eG.layout('kk_3d')
vertex_coords = layout.coords  # List of (x,y,z) tuples for each vertex

Next, create a separate trace for each edge, using the edge's weight to set the line width:

traces = []

# Loop through each edge in the graph
for edge in eG.es:
    source_idx = edge.source
    target_idx = edge.target
    weight = edge['weight']
    
    # Get coordinates for source and target vertices
    x = [vertex_coords[source_idx][0], vertex_coords[target_idx][0]]
    y = [vertex_coords[source_idx][1], vertex_coords[target_idx][1]]
    z = [vertex_coords[source_idx][2], vertex_coords[target_idx][2]]
    
    # Create trace for this edge
    trace = go.Scatter3d(
        x=x, y=y, z=z,
        mode='lines',
        line=dict(color='rgb(125,125,125)', width=weight),
        hoverinfo='none'
    )
    traces.append(trace)

# Add vertex markers (optional but improves readability)
vertex_trace = go.Scatter3d(
    x=[coord[0] for coord in vertex_coords],
    y=[coord[1] for coord in vertex_coords],
    z=[coord[2] for coord in vertex_coords],
    mode='markers',
    marker=dict(size=8, color='rgb(255,0,0)'),
    hoverinfo='text'
)
traces.append(vertex_trace)

# Create and display the figure
fig = go.Figure(data=traces)
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
fig.show()

Key Changes:

  • Separate traces per edge: Each edge gets its own Scatter3d trace, allowing unique line widths for every connection.
  • Direct coordinate mapping: We use the layout coordinates directly to define each edge's start and end points, eliminating the need for concatenated Xe/Ye/Ze lists with NaNs.
  • Optional vertex markers: Adding colored markers for vertices makes the network structure easier to interpret, but you can remove this block if it's not needed.

This approach will correctly render each edge with a line width matching its weight—for example, the edge between vertices 0 and 1 (weight 12) will be significantly thicker than edges with weight 1.

内容的提问来源于stack exchange,提问作者Arun Taneja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:19:54