【问题标题】:Plotting results onto an actual map with pandas使用 pandas 将结果绘制到实际地图上
【发布时间】:2016-03-05 11:40:43
【问题描述】:

就我的 Python 知识水平而言,这可能是一项不可能完成的任务,但我在想是否有办法(可能是已经存在的程序)将包含 AreaCodes 列和 Test Results 列的数据框的结果映射到该地区的实际地图。

所以我的 df 看起来像这样:

           PostCode Area   Test Result
0                  BN           P
1                  PE           P
2                  SO         PRS
3                  PE           P
4                  PE           F
5                  CW           P
6                  CW           F
7                   S           P
8                   S           F
9                  SW           P
10                 SW           F
11                 CM           P
12                 CM           F

邮政编码区域适用于英国。我可能正在寻找类似于英国热图的东西,其中强度由“P”的计数控制

【问题讨论】:

    标签: python python-2.7 pandas


    【解决方案1】:

    您可以为此使用 cartopy。

    pip install https://github.com/SciTools/cartopy/archive/v0.13.1.tar.gz
    

    你需要geos,shapely und proj4,这是最容易使用anaconda的:

    conda install geos proj4 shapely
    

    我从这里下载了包含英国邮政区域的形状文件: http://www.opendoorlogistics.com/downloads/

    然后您可以使用 cartopy 为区域着色,如下所示:

    from cartopy import crs
    from cartopy.io import shapereader
    
    import matplotlib.pyplot as plt
    from matplotlib.cm import get_cmap
    
    cmap = get_cmap('viridis')
    
    uk_postal_areas = shapereader.Reader('Areas.shp')
    
    ax = plt.axes(projection=crs.Mercator())
    
    values = {
        'BA': 0.5,
        'BN': 0.1,
        'NE': 1.0,
        'IV': 0.7,
        'BT': 0.3,
    }
    
    for record in uk_postal_areas.records():
        name = record.attributes['name']
        ax.add_geometries(
                [record.geometry],
                facecolor=cmap(values.get(name, 0)),
                linewidth=0,
                crs=crs.PlateCarree(),
        )
    
    ax.set_extent([-10, 4, 48, 61], crs=crs.PlateCarree())
    
    plt.show()
    

    结果

    【讨论】:

    • 谢谢,但我一直收到此错误:命令“python setup.py egg_info”在 c:\users\eduar\appdata\local\temp\pip-zluvvv-build\ 中出现错误代码 1 失败- 安装 cartopy 时
    猜你喜欢
    • 2019-04-24
    • 2023-04-06
    • 2021-11-18
    • 2016-03-22
    • 2021-08-31
    • 2013-03-06
    • 2016-07-21
    • 2014-02-01
    • 2019-04-04
    相关资源
    最近更新 更多