Exemplo n.º 1
0
def draw_critical_sectors(G, satisfactions,  rep='.', save=True, name='critical_sectors', show=True, flip_axes=False, dpi = 100):
	
	polygons = G.polygons

	if flip_axes:
		polygons={k:flip_polygon(pol) for k,pol in polygons.items()}

	print  'Drawing critical sectors map...'
	fig = plt.figure()
	ax = fig.add_subplot(111)

	min_sat = min(satisfactions.values())
	max_sat = max(satisfactions.values())

	print 'max_sat:', max_sat
	print 'min_sat:', min_sat
	print 'min_sat/max_sat:', min_sat/max_sat
	for n,pol in polygons.items():
		color = linear_mapping(satisfactions[n], min_sat, max_sat)
		patch = PolygonPatch(pol, alpha=0.5, zorder=2, color = color)
		ax.add_patch(patch) 

	plt.plot([0.6475], [43.4922222222222], 'bs', ms = 0.01)

	if save:
		plt.savefig(rep + '/' + name +'.png', dpi = dpi)
		print 'Figure saved in', rep + '/' + name +'.png'
	if show:
		plt.show()

	return fig
Exemplo n.º 2
0
def draw_network_map(G_init, title='Network map', trajectories=[], rep='./', airports=True, 
    load=False, generated=False, add_to_title='', polygons=[], numbers=False, show=True,
    colors=[nice_colors[0]], figsize=(9, 6), flip_axes=False, weight_scale=4., sizes=20., save_file=None,
    dpi=300, already_in_degree=False, alpha=1.):
    """
    Utility used to plot a network and possibly trajectories, sectors, etc.

    Trajectories format: '(n)'

    """

    print "Drawing network..."
    G = deepcopy(G_init)
    polygons_copy = deepcopy(polygons)
    if flip_axes:
        for n in G.nodes():
            G.node[n]['coord']=(G.node[n]['coord'][1], G.node[n]['coord'][0])
        if polygons_copy!=[]:
            polygons_copy=[flip_polygon(pol) for pol in polygons_copy]

    nodes = G.nodes()[:]
    if len(colors)==1:
        colors = [colors[0] for i in range(len(nodes))]
    else:
        colors = [colors[n] for n in nodes]
    if type(sizes) not in [float, int]:
        sizes = [sizes[n] for n in nodes]

    if not already_in_degree:
        x_min = min([G.node[n]['coord'][0]/60. for n in G.nodes()])-0.5
        x_max = max([G.node[n]['coord'][0]/60. for n in G.nodes()])+0.5
        y_min = min([G.node[n]['coord'][1]/60. for n in G.nodes()])-0.5
        y_max = max([G.node[n]['coord'][1]/60. for n in G.nodes()])+0.5
    else:
        x_min = min([G.node[n]['coord'][0] for n in G.nodes()])-0.5
        x_max = max([G.node[n]['coord'][0] for n in G.nodes()])+0.5
        y_min = min([G.node[n]['coord'][1] for n in G.nodes()])-0.5
        y_max = max([G.node[n]['coord'][1] for n in G.nodes()])+0.5

    # #print x_min,y_min,x_max,y_max
    
    fig = plt.figure(figsize=figsize)#*(y_max-y_min)/(x_max-x_min)))#,dpi=600)
    gs = gridspec.GridSpec(1, 2, width_ratios=[6.,1.])
    ax = plt.subplot(gs[0])
    # #ax.set_aspect(1./0.8)
    ax.set_aspect(figsize[0]/float(figsize[1]))

    
    if generated:
        def m(a,b):
            return a,b
        y, x = [G.node[n]['coord'][0] for n in nodes], [G.node[n]['coord'][1] for n in nodes]
    else:
        m = draw_zonemap(x_min,y_min,x_max,y_max,'i')
        x, y = list(zip(*[G.node[n]['coord'] for n in nodes]))
        #x, y = split_coords(G, nodes, r=0.08)
    
    for i,pol in enumerate(polygons_copy):
        patch = PolygonPatch(pol,alpha=0.5, zorder=2, color=_colors[i%len(_colors)])
        ax.add_patch(patch) 

    if load:
        sze = [(np.average([G.node[n]['load'][i][1] for i in range(len(G.node[n]['load'])-1)],\
        weights=[(G.node[n]['load'][i+1][0] - G.node[n]['load'][i][0]) for i in range(len(G.node[n]['load'])-1)])
        /float(G.node[n]['capacity'])*800 + 5) for n in nodes]
    else:
        sze = sizes
        
    coords = {n:m(y[i], x[i]) for i,n in enumerate(nodes)}
    
    ax.set_title(title)
    sca = ax.scatter([coords[n][0] for n in nodes],[coords[n][1] for n in nodes], marker='o', zorder=100, s=sze, c=colors, lw=0, alpha=alpha)#,s=snf,lw=0,c=[0.,0.45,0.,1])
    #sca = ax.scatter([coords[n][0] for n in nodes],[coords[n][1] for n in nodes], marker='o', zorder=100, s=200, c=colors)#,s=snf,lw=0,c=[0.,0.45,0.,1])
    if airports:
        scairports = ax.scatter([coords[n][0] for n in G.airports],[coords[n][1] for n in G.airports],marker='o', zorder=6, s=20., c='r')#,s=snf,lw=0,c=[0.,0.45,0.,1])

    # if 1:
    #     for e in G.edges():
    #         plt.plot([coords[e[0]][0],coords[e[1]][0]],[coords[e[0]][1],coords[e[1]][1]],'k-',lw=0.5)#,lw=width(G[e[0]][e[1]]['weight'],max_wei),zorder=4)
          
    #weights={n:{v:0. for v in G.neighbors(n)} for n in G.nodes()}
    weights = {n:{} for n in nodes}
    for path in trajectories:
        try:
            #path=f.FPs[[fpp.accepted for fpp in f.FPs].index(True)].p
            for i in range(0,len(path)-1):
                #print path[i], path[i+1]
                #weights[path[i]][path[i+1]]+=1.
                weights[path[i]][path[i+1]] = weights[path[i]].get(path[i+1], 0.) + 1.
        except ValueError: # Why?
            pass
        except:
            print "weights[path[i]]:", weights[path[i]]
            raise
    
    max_w = np.max([w for vois in weights.values() for w in vois.values()])
     
    for n,vois in weights.items():
        for v,w in vois.items():
            plt.plot([coords[n][0],coords[v][0]],[coords[n][1],coords[v][1]],'k-',lw=w/max_w*weight_scale)#,lw=width(G[e[0]][e[1]]['weight'],max_wei),zorder=4)

    if numbers:
        for n in G.nodes():
            plt.text(G.node[n]['coord'][0], G.node[n]['coord'][1], ster(n))

    plt.grid(True)
    if save_file!=None:
        plt.savefig(save_file, dpi=dpi)# + 'network_flights' + add_to_title + '.png',dpi=300)
    
    if show:
        plt.show()