Python 3.4:使用unstack和mpl传单在动态地图上显示等高线



我有一个问题,得到一个等高线图显示在一个plpl传单地图的顶部。我很确定这是因为我不确定如何告诉plplile我的等高线在地图上的位置。所以,问题是如何做到这一点。

下面我基于这里的代码构造了下面的简单示例:https://github.com/jwass/mplleaflet/blob/master/examples/contour.py。

虽然这个例子的作者已经确定了他的坐标参考系,但我却无法这样做。例子:

import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd
# Fictional lon/lat data in pandas data frame:
df= {'Lat': pd.Series([40.0,40.0,40.0,41.0,41.0,41.0,42.0,42.0,42.0]),
     'Lon': pd.Series([-69.0,-70.0,-71.0,-69.0,-70.0,-71.0,-69.0,-70.0,-71.0]),
     'Val': pd.Series([0,1,2,0,1,0,2,2,1])}
 df=pd.DataFrame(df)
#Change indices, unstack, and sort:
df.set_index(['Lat','Lon'],inplace=True)
df=df.unstack()
df.sort_index(axis=0,inplace=True)
df.sort_index(axis=1,inplace=True)
#Extract data, make a contour plot
g=df['Val']
plt.contour(g.columns.values,g.index.values,g)
#Define the crs - completely wrong I know, but what to do?
crs= {'lon_0': -105.0,
      'lat_ts': 60.0,
      'R': 6371200,
      'proj': 'stere',
      'units': 'm',
      'lat_0': 90.0}

现在,毫不奇怪,当我执行命令:

mplleaflet.show(crs=crs)
一个空白的世界地图弹出-我的等高线图无处可找。当然,这很可能是因为我的crs定义不正确。有人知道怎么做吗,或者甚至弄清楚如何在python中设置参数。我还要补充一点,实际上没有关于如何做到这一点的Python文档。

,

马特

注。我在Windows 7中运行Python 3.4,并且我也无法在https://github.com/jwass/mplleaflet/blob/master/examples/contour.py上获得示例。

使用geopandas创建正确的投影:

import geopandas
from shapely.geometry import Point
s = geopandas.GeoSeries([Point(4.42, 50.4), Point(4.43, 50.2)])
s.crs = {'init': 'epsg:4326', 'no_defs': True}
s.to_crs(epsg=31370)
结果:

0     POINT (153643.9201303074 121012.188949639)
1    POINT (154373.4245113489 98766.94731544051)
dtype: object

最新更新