前提
3次元のグラフを作成しました。テキストファイルのトポロジー情報を読み込み3次元のグラフをmatplotlibによって出力するといったものです。
実現したいこと
本プログラムのリンクをグラデーションを加えてみたいと考えています。
該当のソースコード
本ソースコードは前提の項で述べた3次元のグラフを出力するものですが、cmap='ocean'
を追加しています
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import networkx as nx
import numpy as np
import tkinter.filedialog as fd
path = fd.askopenfilename()
# 3.2_トポロジー情報(.txt)の読み込み
with open(path, "r") as tf:
line = tf.read().split()
ran = int(len(line) / 2)
# 3.3_エッジのリストを読み込む
G = nx.read_edgelist(path,nodetype=int)
edge_size = nx.number_of_edges(G) # リンク数
node_size = nx.number_of_nodes(G) # ノード数
# spring_layout アルゴリズムで、3次元の座標を生成する
pos = nx.spring_layout(G, dim=3)
# 辞書型から配列型に変換
pos_ary = np.array([pos[n] for n in G])
# ここから可視化
fig = plt.figure()
ax = Axes3D(fig)
# 各ノードの位置に点を打つ
ax.scatter(
pos_ary[:, 0],
pos_ary[:, 1],
pos_ary[:, 2],
s=200,
)
# ノードにラベルを表示する
for n in G.nodes:
ax.text(*pos[n], n)
# エッジの表示
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.plot(xx, yy, zz, cmap='ocean')
# 出来上がった図を表示
plt.show()
以下は読み込むべきテキストファイル(トポロジー情報)です。
1 10
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
エラー文は以下の通りです。
Traceback (most recent call last):
File "C:/Program Files/Python36/質問用.py", line 47, in <module>
ax.plot(xx, yy, zz, cmap='ocean')
File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 1471, in plot
lines = super().plot(xs, ys, *args, **kwargs)
File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_axes.py", line 1743, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py", line 273, in __call__
yield 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 j in range(max(ncx, ncy))]
File "C:\Users\████\AppData\Roaming\Python\Python36\site-packages\matplotlib\axes\_base.py", line 419, in <listcomp>
for j in 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
raise AttributeError(f"{type(self).__name__!r} object "
AttributeError: 'Line2D' object has no property 'cmap'
グラデーション
という単語関連だとこんな記事が。matplotlibで色をグラデーションにする, 【python】matplotlibでグラフに色を指定する方法【カラーマップ】, How to illustrate a 3D graph of gradient descent using python matplotlib?