簡體   English   中英

在底圖上使用 NetworkX 繪制圖形

[英]Drawing a graph with NetworkX on a Basemap

我想在地圖上繪制一個圖表,其中節點由坐標(緯度、經度)定義並具有一些關聯的值。

我已經能夠在底圖上將點繪制為散點圖,但似乎無法找到如何在地圖上繪制圖形。

謝謝。

編輯:我添加了關於如何在底圖上繪制點的代碼。 它的大部分是改編自代碼在這個文章。

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')

我得到這個圖像:地圖

這是一種方法:

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()

在此處輸入圖片說明

截至今天,底圖有一個不錯的替代方案。 Mplleaflet是一個受 mpld3 啟發的庫。 它比底圖繪制速度更快,更易於使用,並允許在漂亮的交互式 openstreetmap 上可視化地理數據。 輸入可以是經度和緯度,庫會自動正確投影數據。

輸入字典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],
 .....

繪圖非常簡單:

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