简体   繁体   English

在底图上使用 NetworkX 绘制图形

[英]Drawing a graph with NetworkX on a Basemap

I want to plot a graph on a map where the nodes would be defined by coordinates (lat, long) and have some value associated.我想在地图上绘制一个图表,其中节点由坐标(纬度、经度)定义并具有一些关联的值。

I have been able to plot points as a scatterplot on a basemap but can't seem to find how to plot a graph on the map.我已经能够在底图上将点绘制为散点图,但似乎无法找到如何在地图上绘制图形。

Thanks.谢谢。

EDIT : I have added code on how I plotted the points on a basemap.编辑:我添加了关于如何在底图上绘制点的代码。 Most of it has been adapted from code in this article.它的大部分是改编自代码在这个文章。

from mpl_toolkits.basemap import Basemap
from shapely.geometry import Point, MultiPoint
import pandas as pd
import matplotlib.pyplot as plt

m = Basemap(
        projection='merc',
        ellps = 'WGS84',
        llcrnrlon=-130,
        llcrnrlat=25,
        urcrnrlon=-60,
        urcrnrlat=50,
        lat_ts=0,
        resolution='i',
        suppress_ticks=True)

# Create Point objects in map coordinates from dataframe lon
# and lat values
# I have a dataframe of coordinates
map_points = pd.Series(
                [Point(m(mapped_x, mapped_y)) 
                 for mapped_x, mapped_y in zip(df['lon'],
                                               df['lat'])])
amre_points = MultiPoint(list(map_points.values)) 

plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111, axisbg='w', frame_on=False)
fig.set_size_inches(18.5, 10.5)

# Create a scatterplot on the map
dev = m.scatter(
            [geom.x for geom in map_points],
            [geom.y for geom in map_points],
            20, marker='o', lw=.25,
            facecolor='#33ccff', edgecolor='w',
            alpha=0.9,antialiased=True,
            zorder=3)

m.fillcontinents(color='#555555')

I get this image:我得到这个图像:地图

Here is one way to do it:这是一种方法:

import networkx as nx
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap as Basemap
m = Basemap(
        projection='merc',
        llcrnrlon=-130,
        llcrnrlat=25,
        urcrnrlon=-60,
        urcrnrlat=50,
        lat_ts=0,
        resolution='i',
        suppress_ticks=True)

# position in decimal lat/lon
lats=[37.96,42.82]
lons=[-121.29,-73.95]
# convert lat and lon to map projection
mx,my=m(lons,lats)

# The NetworkX part
# put map projection coordinates in pos dictionary
G=nx.Graph()
G.add_edge('a','b')
pos={}
pos['a']=(mx[0],my[0])
pos['b']=(mx[1],my[1])
# draw
nx.draw_networkx(G,pos,node_size=200,node_color='blue')

# Now draw the map
m.drawcountries()
m.drawstates()
m.bluemarble()
plt.title('How to get from point a to point b')
plt.show()

在此处输入图片说明

As of today there is a nice alternative to basemap.截至今天,底图有一个不错的替代方案。 Mplleaflet is a library inspired by mpld3. Mplleaflet是一个受 mpld3 启发的库。 It plots faster than basemap, is more easy to use and allows to visualizing geographic data on beautiful interactive openstreetmap.它比底图绘制速度更快,更易于使用,并允许在漂亮的交互式 openstreetmap 上可视化地理数据。 The input can be longitude and latitude the library automatically projects the data properly.输入可以是经度和纬度,库会自动正确投影数据。

Input dictionary pos, where the node (country) is the key and long lat are saved as value.输入字典pos,其中节点(国家)为key,long lat保存为value。

 pos = {u'Afghanistan': [66.00473365578554, 33.83523072784668],
 u'Aland': [19.944009818523348, 60.23133494165451],
 u'Albania': [20.04983396108883, 41.14244989474517],
 u'Algeria': [2.617323009197829, 28.158938494487625],
 .....

Plotting is as easy as:绘图非常简单:

import mplleaflet

fig, ax = plt.subplots()

nx.draw_networkx_nodes(GG,pos=pos,node_size=10,node_color='red',edge_color='k',alpha=.5, with_labels=True)
nx.draw_networkx_edges(GG,pos=pos,edge_color='gray', alpha=.1)
nx.draw_networkx_labels(GG,pos, label_pos =10.3)

mplleaflet.display(fig=ax.figure)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM