How can I efficiently plot a graph where each node represents a point in euclidean space?

Viewed 95

I have an almost fully connected graph in python with roughly 3k nodes and 9M edges. Each node in this graph is represented by a point in R^3 and each edge represents the distance between them. I tried iterating through all of the edges and just plotting them with matplotlib using a 3d projection. Something like this:

fig, ax = plt.subplots(ncols = len(network.positions) - 1, subplot_kw={'projection': '3d'})


    for idx in range(len(network.positions) - 1): 
        x = network.positions[idx][0, :].numpy()
        y = network.positions[idx][1, :].numpy()
        z = network.positions[idx][2, :].numpy()
        ax[idx].scatter(x,y,z, alpha = .5, s=.01, label = "layer " + str(idx + 1), c='r')
        

        x = network.positions[idx + 1][0, :].numpy()
        y = network.positions[idx + 1][1, :].numpy()
        z = network.positions[idx + 1][2, :].numpy()
        ax[idx].scatter(x,y,z, alpha = .5, s=.01, label = "layer " + str(idx + 2), c = 'b')

        for idx_1 in tqdm(range(int(network.positions[idx].shape[1]))):
            for idx_2 in range(int(network.positions[idx + 1].shape[1])):
                x_1, y_1, z_1 = network.positions[idx][:, idx_1]
                x_2, y_2, z_2 = network.positions[idx][:, idx_2]
                ax[idx].plot([x_1, x_2], [y_1, y_2], [z_1, z_2])

However, this is terribly inefficient - because of the many calls to plt.plot. I need a way that I can do something similar but weight the color or transparence of each line by a corresponding weight.

Thanks, Ameet

0 Answers
Related