I created a three-dimensional graph.Read the topology information of the text file and use matplotlib to output a three-dimensional graph.
I would like to add gradation to the link for this program.
This source code outputs the three-dimensional graph described in the assumption section, but adds cmap='ocean'
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes 3D
import networkx as nx
import numpy as np
import tkinter.filedialog as fd
path=fd.askopenfilename()
# 3.2_Read Topology Information (.txt)
with open(path, "r") as tf:
line=tf.read().split()
run=int(len(line)/2)
# 3.3_Load list of edges
G=nx.read_edgelist(path,nodetype=int)
edge_size = nx.number_of_edges(G)# Number of links
node_size = nx.number_of_nodes(G)#Nodes
# The spring_layout algorithm generates three-dimensional coordinates
pos=nx.spring_layout(G,dim=3)
# conversion from dictionary to array
pos_ary=np.array ([pos[n]for n in G])
# Visualize from here
config=plt.figure()
ax=Axes3D(fig)
# dot the positions of each node
ax.scatter(
pos_ary [:,0],
pos_ary [:,1],
pos_ary [:,2],
s = 200,
)
# display a label on a node
for n in G.nodes:
ax.text(*pos[n],n)
# Viewing Edges
for 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.plot(xx,yy,zz,cmap='ocean')
# View Completed Diagram
plt.show()
Below is a text file (topology information) to read.
110
1 11
1 14
1 20
2 9
2 12
2 13
2 15
2 16
3 10
3 11
3 20
4 5
4 8
4 9
5 4
5 9
6 7
6 14
7 6
7 18
8 4
8 11
8 14
8 19
9 2
9 4
9 5
10 1
10 3
11 1
11 3
11 8
11 13
12 2
12 16
13 2
13 11
14 1
14 6
14 8
15 2
15 17
15 21
16 2
16 12
17 15
18 7
18 21
19 8
19 20
20 1
20 3
20 19
20 21
21 15
21 18
21 20
The error statement is as follows:
Traceback (most recent call last):
File "C:/Program Files/Python 36/Question .py", line 47, in<module>
ax.plot(xx,yy,zz,cmap='ocean')
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\mpl_tools\mplot3d\axes3d.py", line 1471, inplot
lines=super().plot(xs, ys, *args, **kwargs)
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_axes.py", line 1743, implot
lines=[*self._get_lines(*args, data=data,**kwargs)]
File "C:\Users\███ App\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py", line 273, in __call__
field from self._plot_args(this,kwargs)
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py", line 419, in_plot_args
for jin range (max(ncx,ncy))]
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py", line 419, in <listcomp>
for jin range (max(ncx,ncy))]
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py", line 312, in_makeline
seg=mlines.Line2D(x,y,**kw)
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\matplotlib\lines.py", line 390, in __init__
self.update(kwargs)
File "C:\Users\███\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py", line 996, in update
raiseAttributeError(f"{type(self).__name__!r}object"
AttributeError: 'Line2D' object has no property'cmap'
"What we want to achieve is to interpret ""change the colors of the 29 sides little by little in order"" and give an example of the description."
Also, the execution environment is macOS13(M1), Python 3.10.8, matplotlib 3.6.2 but I changed this matplotlib because ax=Axes3D(fig)
could not draw it.We also added a legend to make it easier to understand the correspondence between sides and colors.
import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes 3D
from matplotlib import cm# added
# ...
# ax=Axes3D(fig)
ax=fig.add_subplot(projection='3d')
# ...
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,linewidth=5,
color=cm.ocean(i/(edge_size-1)),
label=f'{e[0]:2d} - {e[1]:2d}')
hndls,lbls=ax.get_legend_handles_labels()
ax.legend(handles=hndls, labels=lbls,
loc = 'center left',
bbox_to_anchor= (1.1, 0.5),
fontsize=6)
plt.show()
© 2024 OneMinuteCode. All rights reserved.