在python中保存3D NetworkX图形,以供日后使用paraview查看 [英] Save a 3D NetworkX graph in python to view it later with paraview
问题描述
我编写了此脚本,该脚本使用python中的NetworkX绘制了随机的3D图形.该脚本的输出是一个3D图形,我可以在其中围绕图结构旋转相机.
I wrote this script which draws a random 3D graph using NetworkX in python. The output of this script is a 3D figure where I can rotate the camera around the graph structure.
import networkx as nx
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import random
import pickle
def gen_random_3d_graph(n_nodes, radius):
pos = {i: (random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)) for i in range(n_nodes)}
graph = nx.random_geometric_graph(n_nodes, radius, pos=pos)
return graph
def plot_3d_network(graph, angle):
pos = nx.get_node_attributes(graph, 'pos')
with plt.style.context("bmh"):
fig = plt.figure(figsize=(10, 7))
ax = Axes3D(fig)
for key, value in pos.items():
xi = value[0]
yi = value[1]
zi = value[2]
ax.scatter(xi, yi, zi, edgecolor='b', alpha=0.9)
for i, j in enumerate(graph.edges()):
x = np.array((pos[j[0]][0], pos[j[1]][0]))
y = np.array((pos[j[0]][1], pos[j[1]][1]))
z = np.array((pos[j[0]][2], pos[j[1]][2]))
ax.plot(x, y, z, c='black', alpha=0.9)
ax.view_init(30, angle)
pickle.dump(fig, open('FigureObject.fig.pickle', 'wb'))
plt.show()
if __name__ == '__main__':
graph01 = gen_random_3d_graph(15, 0.6)
plot_3d_network(graph01, 0)
我想保存此图以便以后使用paraview进行查看.我尝试了 pickle
,但没有成功.反正可以在paraview中查看3D图形吗?
I want to save this graph to view it later using paraview. I tried pickle
but it did not work.
Is there anyway to view the 3D graph in paraview?
推荐答案
NetworkX和Paraview之间没有通用的数据类型.
There isn't a common data type between NetworkX and Paraview.
通过将vtk导入python环境并使用vtkLines创建图形,您可以非常容易地编写VTK文件(请查看以下示例:
You could write a VTK file very easily by importing vtk into your python environment and creating the graph using vtkLines (check out this example: https://lorensen.github.io/VTKExamples/site/Python/GeometricObjects/ColoredLines/)
或者您可以从NetworkX导出到JSON并在Paraview中编写Python可编程源以读取自定义数据结构(请参见以下示例 https://www.paraview.org/Wiki/Python_Programmable_Filter )
Or you could export from NetworkX into JSON and write a Python Programmable Source in Paraview for reading the custom data structure (see these examples https://www.paraview.org/Wiki/Python_Programmable_Filter)
这篇关于在python中保存3D NetworkX图形,以供日后使用paraview查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!