You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Basemap上绘制带真实经纬度的研究区域框体遇问题求助

Fixing Study Area Box Drawing on Basemap (Polygon & DrawGreatCircle Approaches)

Hey there! Let's work through getting that study area box properly drawn on your Basemap plot. You mentioned trying two methods—let's break down how to get both working, starting with the Polygon approach since you shared some code for that.

First: Fixing the Polygon Method

Looking at your code snippet, the main missing pieces are defining your study area's geographic bounds and converting those lat/lon coordinates to Basemap's projected coordinates (since Basemap uses its own coordinate system, not raw lat/lon). Here's a complete, working version of your code with those gaps filled:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

# Initialize your Basemap (keeping your original projection parameters)
m = Basemap(width=6000000, height=5000000, rsphere=(6378137.00,6356752.3142),
            resolution='l', projection='lcc',
            lat_1=0., lat_2=30, lat_0=15, lon_0=-75.)

# Draw basic map elements to give context
m.drawcoastlines()
m.drawcountries()
m.drawparallels(range(-20, 40, 10), labels=[1,0,0,0])
m.drawmeridians(range(-90, -60, 10), labels=[0,0,0,1])

# --------------------------
# Define your study area bounds (replace with your actual lat/lon values!)
# Format: [(lon1, lat1), (lon2, lat2), (lon3, lat3), (lon4, lat4)]
# This example is a box around the Caribbean region matching your projection
study_area_lonlat = [
    (-85, 5),
    (-65, 5),
    (-65, 25),
    (-85, 25)
]
# Convert lat/lon to Basemap's projected x,y coordinates
study_area_xy = [m(lon, lat) for lon, lat in study_area_lonlat]

# Create and add the Polygon to the plot
study_box = Polygon(
    study_area_xy,
    linewidth=2,          # Adjust line thickness
    linestyle='--',       # You can use your dashes here too!
    edgecolor='red',      # Set border color
    facecolor='none'      # Make the box transparent (no fill)
)
plt.gca().add_patch(study_box)

# Optional: Add your custom dashes if needed
# lw = 1
# dashes = [5,7]
# study_box.set_linestyle((0, dashes))
# study_box.set_linewidth(lw)

plt.title('Study Area Box on Basemap')
plt.show()

Key fixes/notes for the Polygon method:

  • Coordinate Conversion: Always convert raw lat/lon to Basemap's projected coordinates using m(lon, lat)—this is the most common mistake when adding patches to Basemap.
  • Define Bounds: You need to explicitly list the four corners of your study area (in order, either clockwise or counter-clockwise).
  • Styling: I added example styling, but you can swap in your lw and dashes variables by uncommenting the last part of the code.

Second: Using drawgreatcircle for Large-Scale Boxes

If your study area spans a large portion of the globe (where straight lines in lat/lon don't follow the Earth's curvature), drawgreatcircle can be useful. However, it draws a single great circle between two points—so to make a box, you'll need to draw four separate great circles for each edge.

A quick example for a box using drawgreatcircle:

# Using the same Basemap instance 'm' from above

# Define four corners (same as before)
corner1 = (-85, 5)
corner2 = (-65, 5)
corner3 = (-65, 25)
corner4 = (-85, 25)

# Draw each edge as a great circle
m.drawgreatcircle(corner1[0], corner1[1], corner2[0], corner2[1], color='blue', linewidth=2, linestyle=':')
m.drawgreatcircle(corner2[0], corner2[1], corner3[0], corner3[1], color='blue', linewidth=2, linestyle=':')
m.drawgreatcircle(corner3[0], corner3[1], corner4[0], corner4[1], color='blue', linewidth=2, linestyle=':')
m.drawgreatcircle(corner4[0], corner4[1], corner1[0], corner1[1], color='blue', linewidth=2, linestyle=':')

plt.show()

Note for drawgreatcircle:

  • This method is best for large regions where you want lines to follow the Earth's curvature. For small, localized study areas, the Polygon method is simpler and more accurate.
  • If your box crosses the 180° longitude line, you'll need to split the great circle into two segments to avoid Basemap drawing a line across the entire globe.

内容的提问来源于stack exchange,提问作者Angela Maria Gomez Garcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:47:09