Python 2.7,matplotlib 1.5.1,Win 7,x64
我试图绘制节点和节点之间的最短距离。使用Dijkstra算法在图中使用几何最近邻(即不是最近的连通邻居)。
算法运行正常但是当涉及绘图时,当我绘制某些节点时,matplotlib的缩放会变得怪异。
代码段:
setTimeout(getPages, 0);
例如,我只使用import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Find the paths between a given node & its nearest neighbours
def plotPath(pathStart, pathEnd, pointCol='b'):
shortPath = graph.dijkstra(pathStart, pathEnd) # this calculates the shortest path
pathNodesIdx = [i-1 for i in shortPath] # Algorithm returns 1 indexed whilst Python uses 0 indexed
pathCoords = L3.nodes[pathNodesIdx, 1:4] # retrieves the coordinate for the nodes on the path
ax.scatter(pathCoords[1:-1,0], pathCoords[1:-1,1], pathCoords[1:-1,2], s=240, c=pointCol, marker='o')
startNode = pathCoords[0]
endNode = pathCoords[-1]
ax.scatter(startNode[0], startNode[1], startNode[2], s=240, c='g', marker='o')
ax.scatter(endNode[0], endNode[1], endNode[2], s=240, c='r', marker='o')
for node in pathCoords[1:]:
ax.plot([startNode[0], node[0]], [startNode[1], node[1]], [startNode[2], node[2]], color=pointCol, linewidth=2.0)
startNode = node
return pathCoords
pointCol = 'b'
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
pathStart = 1 # given node
graph=Graph(L3.trabGraph) # L3.trabGraph is list conataining the edge/node/cost information for the graph
# Return indices for nearest neighbours
nearest = [i+1 for i in L3.nodeNeighbours(pathStart, numNeighs=6, method='brute')[1:]]
绘制到第二个最近邻居的路径:
但如果我使用
添加其他最近的邻居plotPath(1, nearest[2])
我明白了:
供参考每种情况的节点坐标:
p0 = plotPath(1, nearest[0])
p1 = plotPath(1, nearest[1])
p2 = plotPath(1, nearest[2])
p3 = plotPath(1, nearest[3])
p4 = plotPath(1, nearest[4])
对于我的生活,我不明白为什么matplotlib这样做?有人知道吗?
我遗漏了Dijkstra算法的实现(来自Rosetta代码FYI)&为了简洁起见,创建了图表。事实上,我不能自由地分享图表信息。
答案 0 :(得分:1)
如果这里的问题确实是"如何在matplotlib中设置3d图的限制?"然后答案是:
就像你在第二种情况下那样:
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
ax.set_zlim([zmin, zmax])
查找各个案例的最小值和最大值当然可以自动化。