问题描述
我想知道是否可以在图形的二维表示上绘制垂直条。假设我有一棵树,我想将任何节点与一个可以表示为垂直条的“潜力”相关联。
解决方法
NetworkX 可以使用 matplotlib 绘图工具做到这一点,因为结果是一个 matplotlib 图形,您可以在该图形上使用 matplotlib 在图形的 networkx 绘图之上绘制您想要的任何其他内容。
nx.draw(G)
mpl.plot([xpt,xpt],[ymin,ymax],'--b')
mpl.show()
,
这是一个最小的例子,它满足了我的要求(在 Python 中):
import networkx as nx
import random
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
degree = 3
N = 10
g = nx.random_regular_graph(degree,N)
fig = plt.figure(figsize=(10,7))
ax = Axes3D(fig)
for i,j in enumerate(g.edges()):
x = np.array((positions[j[0]][0],positions[j[1]][0]))
y = np.array((positions[j[0]][1],positions[j[1]][1]))
ax.plot(x,y,c='black',alpha=0.5)
for key,value in positions.items():
xi = value[0]
yi = value[1]
# Scatter plot
ax.scatter(xi,yi,c= 'red')
ax.bar3d(xi,0.01,random.random(),shade=False)
ax.set_axis_off()
它生成这种图,可用于表示图形上的附加信息