如何用Python可视化图形的(交互式)3D?

4 投票
2 回答
4152 浏览
提问于 2025-04-16 21:24

我在Python中有一个图形,像这样:

# Each element is a tuple with coordinates (x,y,z). 
# The index is the id of the vertex
vertexList = [(0,0,0),(1,0,0),(1,1,0),(0,1,0), 
               (0,0,1),(1,0,1),(1,1,1),(0,1,1)]

# Each element is a tuple with the vertex-ids and a weight (vertexId1, vertexId2, weight)
edgeList = [(0,1,1), (1,2,1), (2,3,1), (3,0,1),
               (0,4,1), 
               (4,5,1), (5,6,1), (6,7,1), (7,4,1)]
graph = (vertexList, edgeList)

这是一个小例子。我写的应用程序使用的图形大约有100个顶点和300条边。

我想用Python来可视化这个图形,最好是能在Ubuntu上使用的库。如果能在3D可视化中移动图形,那就更好了。

到目前为止我做了什么

目前我在使用UBIGRAPH。它的可视化和交互效果很好,但我无法为顶点指定坐标:

def visulizeGraph(Graph):
    vertexList, edgeList = Graph
    server_url = 'http://127.0.0.1:20738/RPC2'
    server = xmlrpclib.Server(server_url)
    G = server.ubigraph;

    G.clear()
    for identifier, vertex in enumerate(vertexList):
        G.new_vertex_w_id(identifier)
    for vertex1, vertex2, weight in edgeList:
        x1, y1, z1 = vertexList[vertex1]
        x2, y2, z2 = vertexList[vertex2]

        G.new_edge(vertex1, vertex2)

matplot

我发现了matplotlib,但它的体积很大。我没有找到一个符合我需求的例子,但可能是我没找到。这个库在Ubuntu上是可以用的。

vtk

和matplot一样的问题。如果你能给我一些有效的例子,那可能是最好的解决方案。

2 个回答

2

当你查看matplotlib的时候,有没有注意到mplot3d这个东西?这可能正是你需要的。

MPlot3D

如果没有,那我很抱歉,我可能帮不了你更多了。

2

我自己还没用过这个(除了运行一些示例脚本),但是mayavi2看起来很不错。它是和enthought的Python发行版一起提供的。

另外,虽然这不是你问题中的内容,但如果你在Python中处理图形的话,networkx也非常好用。

希望这些对你有点帮助。

撰写回答