Python 3.4:使用unstack和mplleaflet

时间:2015-07-08 15:24:52

标签: python matplotlib contour geopandas

我在使用等高线图显示在mplleaflet地图上时遇到问题。我相当肯定这是因为我不知道如何告诉mplleaflet在地图上定位轮廓图的位置。所以,问题是关于如何做到这一点。

下面我根据此处的代码构建了以下简单示例: 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文档。

最佳,

马特

P.S。我在Windows 7中运行Python 3.4,我也无法在https://github.com/jwass/mplleaflet/blob/master/examples/contour.py工作时获得示例。

1 个答案:

答案 0 :(得分:0)

使用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