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

如何使用igraph绘制椭圆节点?求自定义节点形状完整示例

Absolutely, let's break down how to create custom node shapes (especially ellipses) in igraph—since the official docs can be a bit sparse on this. I'll walk you through a full, working example with both basic and advanced tweaks, using Python's python-igraph and matplotlib (the most common setup for this kind of customization).

1. First, Set Up Dependencies

Make sure you have the required packages installed:

pip install python-igraph matplotlib
2. Basic Ellipse Nodes: Core Implementation

The key here is defining a custom drawing function that returns a matplotlib Ellipse patch, then passing that function to igraph's plotting engine via the vertex_shape parameter.

Here's a complete runnable example:

import igraph as ig
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

# Step 1: Create a sample graph
g = ig.Graph()
g.add_vertices(5)
g.add_edges([(0,1), (1,2), (2,3), (3,4), (4,0)])

# Step 2: Define the custom ellipse drawing function
def draw_ellipse(vertex, coords, size, **kwargs):
    # coords = (x, y) position of the node
    # size = base size set in igraph's plot params
    x, y = coords
    
    # Adjust width/height to get an ellipse (tweak these multipliers as needed)
    ellipse_width = size * 1.5
    ellipse_height = size * 0.8
    
    # Create and return the Ellipse patch
    return Ellipse(
        (x, y),
        width=ellipse_width,
        height=ellipse_height,
        angle=0,  # Add a value here (e.g., 30) to rotate the ellipse
        **kwargs  # Passes color, edge width, etc., from igraph's plot settings
    )

# Step 3: Plot the graph with custom ellipses
fig, ax = plt.subplots(figsize=(8, 6))

ig.plot(
    g,
    target=ax,
    vertex_shape=draw_ellipse,  # Critical: Use our custom function
    vertex_size=20,  # Base size that our function scales
    vertex_color="#6a994e",  # Fill color for ellipses
    vertex_edge_color="#386641",  # Border color
    vertex_edge_width=2,  # Border thickness
    edge_color="#bc4749",  # Edge color
    edge_width=2,
    vertex_label=["A", "B", "C", "D", "E"],  # Node labels
    vertex_label_size=12,
    vertex_label_color="#ffffff"  # Label text color
)

# Adjust axes to fit the graph neatly
ax.set_xlim(ax.get_xlim()[0] - 5, ax.get_xlim()[1] + 5)
ax.set_ylim(ax.get_ylim()[0] - 5, ax.get_ylim()[1] + 5)
ax.axis('off')  # Hide default axes

plt.show()
3. Advanced: Per-Node Customized Ellipses

Want different ellipses for different nodes? You can attach custom attributes to vertices and use them in your drawing function. Here's how:

# Add custom attributes to each node (width, height, rotation angle)
for idx, vertex in enumerate(g.vs):
    vertex['ellipse_width'] = 25 + idx * 5  # Increase width per node
    vertex['ellipse_height'] = 15 + idx * 3  # Increase height per node
    vertex['ellipse_angle'] = idx * 15  # Rotate each node incrementally

# Updated drawing function that uses vertex attributes
def draw_custom_ellipse(vertex, coords, size, **kwargs):
    x, y = coords
    # Fall back to default values if the attribute doesn't exist
    width = vertex.get('ellipse_width', size * 1.5)
    height = vertex.get('ellipse_height', size * 0.8)
    angle = vertex.get('ellipse_angle', 0)
    
    return Ellipse(
        (x, y),
        width=width,
        height=height,
        angle=angle,
        **kwargs
    )

# Plot with personalized ellipses
fig, ax = plt.subplots(figsize=(8, 6))

ig.plot(
    g,
    target=ax,
    vertex_shape=draw_custom_ellipse,
    vertex_color=["#6a994e", "#f2e8cf", "#bc4749", "#386641", "#a7c957"],
    vertex_edge_color="#000000",
    edge_color="#555555",
    vertex_label=["A", "B", "C", "D", "E"],
    vertex_label_size=12
)

ax.set_xlim(ax.get_xlim()[0] - 5, ax.get_xlim()[1] + 5)
ax.set_ylim(ax.get_ylim()[0] - 5, ax.get_ylim()[1] + 5)
ax.axis('off')

plt.show()
Key Takeaways
  • The vertex_shape parameter accepts any function that returns a matplotlib Patch object—so you're not limited to ellipses; you could draw rectangles, polygons, or even custom paths!
  • Use vertex attributes to make shapes dynamic per node (size, rotation, color, etc.)
  • Always use the target=ax parameter to plot onto a matplotlib axis, which gives you full control over the final output.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:36:21