Mayavi2安装
Networkx可以使用Mayavi2显示3D图。
pip install mayavipip install PyQt5
检查Mayavi2安装成功
$mayavi2
IPython启动GUI事件循环。不然from mayavi import mlab
就会卡住不动。
$ipython --gui=qt5
or
$ipythonIn [1]: %gui qt5
mayavi.mlab
The mayavi.mlab module, that we call mlab, provides an easy way to visualize data in a script or from an interactive prompt with one-liners as done in the matplotlib pylab interface but with an emphasis on 3D visualization using Mayavi2. This allows users to perform quick 3D visualization while being able to use Mayavi’s powerful features.
Mayavi’s mlab is designed to be used in a manner well-suited to scripting and does not present a fully object-oriented API. It is can be used interactively with IPython.
In [3]: %gui?Docstring:Enable or disable IPython GUI event loop integration.%gui [GUINAME]This magic replaces IPython's threaded shells that were activatedusing the (pylab/wthread/etc.) command line flags. GUI toolkitscan now be enabled at runtime and keyboardinterrupts should work without any problems. The following toolkitsare supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::%gui wx# enable wxPython event loop integration%gui qt4|qt # enable PyQt4 event loop integration%gui qt5# enable PyQt5 event loop integration%gui gtk# enable PyGTK event loop integration%gui gtk3 # enable Gtk3 event loop integration%gui tk# enable Tk event loop integration%gui osx# enable Cocoa event loop integration# (requires %matplotlib 1.1)%gui # disable all event loop integrationWARNING: after any of these has been called you can simply createan application object, but DO NOT start the event loop yourself, aswe have already handled that.
示例:
import networkx as nximport numpy as npfrom mayavi import mlabimport matplotlib.pyplot as plt# Cube GrapnG = nx.cubical_graph()pos = nx.spring_layout(G) # positions for all nodes# nodesoptions = {"node_size": 500, "alpha": 0.8}nx.draw_networkx_nodes(G, pos, nodelist=[0, 1, 2, 3], node_color="r", **options)nx.draw_networkx_nodes(G, pos, nodelist=[4, 5, 6, 7], node_color="b", **options)# edgesnx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)nx.draw_networkx_edges(G,pos,edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],width=8,alpha=0.5,edge_color="r",)nx.draw_networkx_edges(G,pos,edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],width=8,alpha=0.5,edge_color="b",)# some math labelslabels = {}labels[0] = r"$a$"labels[1] = r"$b$"labels[2] = r"$c$"labels[3] = r"$d$"labels[4] = r"$\alpha$"labels[5] = r"$\beta$"labels[6] = r"$\gamma$"labels[7] = r"$\delta$"nx.draw_networkx_labels(G, pos, labels, font_size=16)# 3d spring layoutpos = nx.spring_layout(G, dim=3)# numpy array of x,y,z positions in sorted node orderxyz = np.array([pos[v] for v in sorted(G)])# scalar colorsscalars = np.array(list(G.nodes())) + 5pts = mlab.points3d(xyz[:, 0],xyz[:, 1],xyz[:, 2],scalars,scale_factor=0.1,scale_mode="none",colormap="Blues",resolution=20,)pts.mlab_source.dataset.lines = np.array(list(G.edges()))tube = mlab.pipeline.tube(pts, tube_radius=0.01)mlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))mlab.show()