Esempio n. 1
0
def plotBends(G=None, ax=None, road_color='k', road_width=1):
	"""
	identifies nodes with only two neighbors. IF we make a bend at that node, plots a bezier curve, i.e. a not that sharp curve.

	Sets edge[2]['plotted'] to True if plotted out for the two neighboring edges
	"""
	edges=G.edges(data=True)
	for edge in edges:
		edge[2]['plotted']=False #change later where it applies.
	if len(edges)>1000: return ax #the beziers curve takes time to plot. Too big system.

	for n in G.nodes(data=False):
		if nodeEdgesBendable(G,n):
			neighbors=G.neighbors(n)
			p1=tuple(fun.getPointBetween(neighbors[0], n))
			p2=tuple(fun.getPointBetween(neighbors[1], n))
			verts= [neighbors[0], p1, n, p2, neighbors[1]]
			codes = [Path.MOVETO, Path.LINETO,Path.CURVE3,Path.CURVE3,Path.LINETO]
			#now, if any of the neighbors have degre 2 AND a bend, we should only plot half
			#of the way there.
			if nodeEdgesBendable(G,neighbors[0]):
				verts[0]=p1 #no line to neighbor, only half way
			if nodeEdgesBendable(G,neighbors[1]):
				verts[-1]=p2
			path = Path(verts, codes)
			p=patches.PathPatch(path, facecolor='none', edgecolor=road_color, lw=road_width)
			#ax.add_patch(patches.PathPatch(path, facecolor='none', edgecolor=road_color, lw=road_width))

			#because of bug in matplotlib, p always shows in the back of the plot.
			#Make a line instead. 
			vertices=p.get_verts()
			x=[v[0] for v in vertices]
			y=[v[1] for v in vertices]
			l=Line2D(x,y, color=road_color, linewidth=road_width)
			ax.add_line(l)
			#now, update 'plotted' thing.
			for neigh in neighbors:
				d=G.get_edge_data(n, neigh)
				d['plotted']=True
Esempio n. 2
0
def draw_custom(G=None, ax=None, edge_visits=False, cost=False, weight=False,road_color='k', road_width=1, poly=False):
	assert G != None
	if edge_visits or cost or weight:
		debug_mode=True #means that we do not plot bends that nicely... provides debug info
	else:
		debug_mode=False
	if not ax:
		fig=plt.figure()
		ax=fig.add_subplot(111, aspect='equal')
		ax.set_axis_bgcolor('#ffbb55')
		ax.set_xlabel('x')
		ax.set_ylabel('y')
	if not debug_mode: plotBends(G=G, ax=ax, road_color=road_color, road_width=road_width)
	for e in G.edges(data=True):
		x=e[0][0], e[1][0]
		y=e[0][1], e[1][1]
		pos=fun.getPointBetween([x[0],y[0]], [x[1],y[1]]) #used for edge text..
		if weight:
			ax.text(pos[0],pos[1],' '+'w:%.0f'%e[2]['weight'])
		if cost:
			if e[2]['c']>1e10:
				ax.text(pos[0],pos[1],' '+r'$\infty$')
			else:
				ax.text(pos[0],pos[1]-2,' '+'%.0f'%e[2]['c'])
		if not debug_mode and e[2]['plotted']: continue #already have a nice bending curve
		if edge_visits:
			l = Line2D(x,y, color=road_color, linewidth=road_width+np.log(1+len(e[2]['visited_from_node']))/(np.log(len(G)*0.5)*0.2))
			ax.text(pos[0],pos[1]+2,' '+'%d'%len(e[2]['visited_from_node']))
		else:
			l = Line2D(x,y, color=road_color, linewidth=road_width)
		ax.add_line(l)
	ax.set_xlim(*tuple(G.lim[0:2]))
	ax.set_ylim(*tuple(G.lim[2:4]))


	#plot the polygon
	if poly:
		p = Polygon(G.areaPoly,closed=True, color='none', ec='k',lw=3, ls='solid')
		ax.add_patch(p)
	return ax
Esempio n. 3
0
def middle(p1,p2):
	"""returns the point in the middle"""
	return fun.getPointBetween(p1,p2)