summaryrefslogtreecommitdiff
path: root/_downloads/35ba6dddd6b6024f6b53a3762807ada7/mayavi2_spring.ipynb
blob: 3406ffcb43adf35300431d4cb2ec1ecc7542afc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Mayavi2\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import networkx as nx\nimport numpy as np\nfrom mayavi import mlab\n\n# some graphs to try\n# H=nx.krackhardt_kite_graph()\n# H=nx.Graph();H.add_edge('a','b');H.add_edge('a','c');H.add_edge('a','d')\n# H=nx.grid_2d_graph(4,5)\nH = nx.cycle_graph(20)\n\n# reorder nodes from 0,len(G)-1\nG = nx.convert_node_labels_to_integers(H)\n# 3d spring layout\npos = nx.spring_layout(G, dim=3, seed=1001)\n# numpy array of x,y,z positions in sorted node order\nxyz = np.array([pos[v] for v in sorted(G)])\n# scalar colors\nscalars = np.array(list(G.nodes())) + 5\n\nmlab.figure()\n\npts = mlab.points3d(\n    xyz[:, 0],\n    xyz[:, 1],\n    xyz[:, 2],\n    scalars,\n    scale_factor=0.1,\n    scale_mode=\"none\",\n    colormap=\"Blues\",\n    resolution=20,\n)\n\npts.mlab_source.dataset.lines = np.array(list(G.edges()))\ntube = mlab.pipeline.tube(pts, tube_radius=0.01)\nmlab.pipeline.surface(tube, color=(0.8, 0.8, 0.8))\nmlab.orientation_axes()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}