Python Mayavi.mlab安装使用

Mayavi2安装

Networkx可以使用Mayavi2显示3D图。

pip install mayavi
pip install PyQt5

检查Mayavi2安装成功

$mayavi2

IPython启动GUI事件循环。不然from mayavi import mlab就会卡住不动。

$ipython --gui=qt5

or

$ipython
In [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 activated
using the (pylab/wthread/etc.) command line flags.  GUI toolkits
can now be enabled at runtime and keyboard
interrupts should work without any problems.  The following toolkits
are 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 integration

WARNING:  after any of these has been called you can simply create
an application object, but DO NOT start the event loop yourself, as
we have already handled that.

示例:

import networkx as nx
import numpy as np
from mayavi import mlab
import matplotlib.pyplot as plt

# Cube Grapn
G = nx.cubical_graph()
pos = nx.spring_layout(G)  # positions for all nodes

# nodes
options = {"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)

# edges
nx.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 labels
labels = {}
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 layout
pos = nx.spring_layout(G, dim=3)
# numpy array of x,y,z positions in sorted node order
xyz = np.array([pos[v] for v in sorted(G)])
# scalar colors
scalars = np.array(list(G.nodes())) + 5

pts = 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()

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开源技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值