0

Abstract

I learned that in 2-dimensional graphs, arrays can be used to control the transparency of edges and nodes. I wanted to apply this to 3D graphs as well, so I asked the question.

Here is the source code I tried.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import networkx as nx
import numpy as np

def draw_graph(edges):
    G = nx.Graph()
    G.add_edges_from(edges)
    pos = nx.spring_layout(G , dim = 3)
    pos_ary = np.array([pos[n] for n in G])

    fig = plt.figure(figsize = (20,10) , facecolor = "w")
    ax = fig.add_subplot(111 , projection = "3d")

    ax.scatter(
        pos_ary[:, 0],
        pos_ary[:, 1],
        pos_ary[:, 2],
        s = 200,
        )
    
    alphas = [0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.8 , 0.9 , 1]

    for e in G.edges:
        node0_pos = pos[e[0]]
        node1_pos = pos[e[1]]
        xx = [node0_pos[0] , node1_pos[0]]
        yy = [node0_pos[1] , node1_pos[1]]
        zz = [node0_pos[2] , node1_pos[2]]
        ax.draw_networkx_nodes(xx , yy , zz , color = "b" , alpha = alphas)
    plt.show()

edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
draw_graph(edges)

The following error statement was output for such source code.

Traceback (most recent call last):
  File "C:/Program Files/Python36/3d.py", line 35, in <module>
    draw_graph(edges)
  File "C:/Program Files/Python36/3d.py", line 30, in draw_graph
    ax.draw_networkx_nodes(xx , yy , zz , color = "b" , alpha = alphas)
AttributeError: 'Axes3DSubplot' object has no attribute 'draw_networkx_nodes'

I have an think that the error is probably due to the use of a syntax that does not exist in the library Axes3DSubplot. Are there any desirable libraries?

1 Answer 1

0

You can get the desired output like this:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import networkx as nx
import numpy as np

def draw_graph(edges):
    G = nx.Graph()
    G.add_edges_from(edges)
    pos = nx.spring_layout(G , dim = 3)
    pos_ary = np.array([pos[n] for n in G])

    fig = plt.figure(figsize = (20,10) , facecolor = "w")
    ax = fig.add_subplot(111 , projection = "3d")

    ax.scatter(
        pos_ary[:, 0],
        pos_ary[:, 1],
        pos_ary[:, 2],
        s = 200,
        )
    
    alphas = [0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.8 , 0.9 , 1]

    for i, e in enumerate(G.edges):
        node0_pos = pos[e[0]]
        node1_pos = pos[e[1]]



        xx = [node0_pos[0] , node1_pos[0]]
        yy = [node0_pos[1] , node1_pos[1]]
        zz = [node0_pos[2] , node1_pos[2]]
        ax.plot(xx , yy , zz , color = "b", alpha = alphas[i] )
    plt.show()

edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0), (0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
draw_graph(edges)

enter image description here

You could also pass it as an array if you would create a list of the Line2D object and then apply it to them...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.