【问题标题】:Plotting Lat/Long Points Using Basemap使用底图绘制纬度/经度点
【发布时间】:2017-06-11 19:56:32
【问题描述】:

我正在尝试使用 matplotlib 和 Basemap 在地图上绘制点,其中的点代表特定建筑物的纬度/经度。我的地图确实绘制了这些点,但是把它们放在了错误的位置。当我使用相同的数据并使用 Bokeh 而不是 matplotlib 和底图做同样的事情时,我得到了正确的绘图。

这是 Bokeh 中的正确结果: Bokeh Version

这是底图中的错误结果: Basemap Version

我在 StackOverflow 上的其他地方看到过讨论,这表明这可能与 plot() 以某种方式“改变”经度这一事实有关。我已经尝试了那里的建议,其中包括以下行: lons, lats = m.shiftdata(long, lat) 然后使用移位的数据。这没有任何明显的影响。

我在 Basemap 和 Bokeh 中生成两个绘图的完整示例代码在这里:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import pandas as pd

from bokeh.plotting import figure, show
from bokeh.sampledata.us_states import data as states
from bokeh.models import ColumnDataSource, Range1d

# read in data to use for plotted points
buildingdf = pd.read_csv('buildingdata.csv')
lat = buildingdf['latitude'].values
long = buildingdf['longitude'].values

# determine range to print based on min, max lat and long of the data
margin = .2 # buffer to add to the range
lat_min = min(lat) - margin
lat_max = max(lat) + margin
long_min = min(long) - margin
long_max = max(long) + margin

# create map using BASEMAP
m = Basemap(llcrnrlon=long_min,
            llcrnrlat=lat_min,
            urcrnrlon=long_max,
            urcrnrlat=lat_max,
            lat_0=(lat_max - lat_min)/2,
            lon_0=(long_max-long_min)/2,
            projection='merc',
            resolution = 'h',
            area_thresh=10000.,
            )
m.drawcoastlines()
m.drawcountries()
m.drawstates()
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color = 'white',lake_color='#46bcec')
# convert lat and long to map projection coordinates
lons, lats = m(long, lat)
# plot points as red dots
m.scatter(lons, lats, marker = 'o', color='r')
plt.show()


# create map using Bokeh
source = ColumnDataSource(data = dict(lat = lat,lon = long))
# get state boundaries
state_lats = [states[code]["lats"] for code in states]
state_longs = [states[code]["lons"] for code in states]

p = figure(
           toolbar_location="left",
           plot_width=1100,
           plot_height=700,
           )

# limit the view to the min and max of the building data
p.y_range = Range1d(lat_min, lat_max)
p.x_range = Range1d(long_min, long_max)
p.xaxis.visible = False
p.yaxis.visible = False
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None

p.patches(state_longs, state_lats, fill_alpha=0.0,
      line_color="black", line_width=2, line_alpha=0.3)

p.circle(x="lon", y="lat", source = source, size=4.5,
         fill_color='red',
         line_color='grey',
         line_alpha=.25
         )
show(p)

我没有足够的信誉点来发布数据链接或将其包含在此处。

【问题讨论】:

    标签: matplotlib matplotlib-basemap


    【解决方案1】:

    在底图中,散点隐藏在fillcontinents 后面。删除两行

    #m.drawmapboundary(fill_color='#46bcec')
    #m.fillcontinents(color = 'white',lake_color='#46bcec')
    

    会告诉你要点。因为这可能是不受欢迎的,所以最好的解决方案是使用 zorder 参数将散点图放置在地图的其余部分之上。

    m.scatter(lons, lats, marker = 'o', color='r', zorder=5)
    

    这是完整的代码(我想请您在下次提出问题时将这种可运行的最小示例与硬编码数据一起包含在内,因为它可以为每个人节省大量自己发明数据的工作):

    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    import pandas as pd
    import io
    
    u = u"""latitude,longitude
    42.357778,-71.059444
    39.952222,-75.163889
    25.787778,-80.224167
    30.267222, -97.763889"""
    
    # read in data to use for plotted points
    buildingdf = pd.read_csv(io.StringIO(u), delimiter=",")
    lat = buildingdf['latitude'].values
    lon = buildingdf['longitude'].values
    
    # determine range to print based on min, max lat and lon of the data
    margin = 2 # buffer to add to the range
    lat_min = min(lat) - margin
    lat_max = max(lat) + margin
    lon_min = min(lon) - margin
    lon_max = max(lon) + margin
    
    # create map using BASEMAP
    m = Basemap(llcrnrlon=lon_min,
                llcrnrlat=lat_min,
                urcrnrlon=lon_max,
                urcrnrlat=lat_max,
                lat_0=(lat_max - lat_min)/2,
                lon_0=(lon_max-lon_min)/2,
                projection='merc',
                resolution = 'h',
                area_thresh=10000.,
                )
    m.drawcoastlines()
    m.drawcountries()
    m.drawstates()
    m.drawmapboundary(fill_color='#46bcec')
    m.fillcontinents(color = 'white',lake_color='#46bcec')
    # convert lat and lon to map projection coordinates
    lons, lats = m(lon, lat)
    # plot points as red dots
    m.scatter(lons, lats, marker = 'o', color='r', zorder=5)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多