如何在Cartopy投影中绘制圆的半径?

问题描述 投票:0回答:1

我试图在 Cartopy 投影上通过一点绘制圆的半径。我找不到任何与此相关的内容。

这是我到目前为止所拥有的:

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.Mercator())

ax.set_extent([18, 28, 59.5, 64.1], crs=ccrs.PlateCarree())
ax.coastlines(linewidth=.5)

# Add the radar distance circle
lon_ika = 23.076
lat_ika = 61.76
radius = 250
n_samples = 80

circles = Polygon(Geodesic().circle(lon_ika, lat_ika, radius*1000., n_samples=n_samples))
feature = cfeature.ShapelyFeature(circles, ccrs.PlateCarree(), fc='None', ec="black", lw=1, linestyle="-")
linestyle="--")
circle = ax.add_feature(feature)

# Adding red dot and name of the radar station to the plot
ax.plot(lon_ika, lat_ika, "o", c='r', transform=ccrs.PlateCarree(), markersize=6, label="Ikaalinen")

# Adding red cross and name of IOP location to the plot
lon_hyy = 24.3
lat_hyy = 61.83
ax.plot(lon_hyy, lat_hyy, "x", c='r', transform=ccrs.PlateCarree(), markersize=6, label="Hyytiälä")

# Add labels
plt.legend(loc='upper left', fontsize=12, framealpha=1, edgecolor='black')

plt.show()

到目前为止我还没有在网上找到任何相关内容:/

python numpy matplotlib cartopy
1个回答
0
投票

我将使用以下函数来完成此操作,该函数实现一些基本的三角函数来获取圆:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np

def draw_circle(ax, lon, lat, radius, resolution=100):
    """
    Draws a circle on a Cartopy map

    Parameters:
    - ax: Cartopy axes
    - lon, lat: Center coordinates of the circle
    - radius: Radius of the circle in degrees
    - resolution: Number of points to use for drawing the circle (default is 100)
    """
    theta = np.linspace(0, 2*np.pi, resolution)
    circle_lon = lon + np.cos(theta) * radius
    circle_lat = lat + np.sin(theta) * radius

    ax.plot(circle_lon, circle_lat, transform=ccrs.PlateCarree(), color='red', label='Circle')

这是一个使用示例:

center_lon, center_lat = 1, 0  # Center of the circle
radius_degrees = 10  # Radius of the circle in degrees

# Create a Cartopy map
fig, ax = plt.subplots(
    subplot_kw={'projection': ccrs.PlateCarree()}
) # See https://scitools.org.uk/cartopy/docs/v0.15/crs/projections.html for projections
ax.stock_img()

# Draw the circle
draw_circle(ax, center_lon, center_lat, radius_degrees)

# Some additional matplotlib commands
ax.set_title('Circle on Cartopy Projection')
ax.legend()

plt.show()

您可能需要访问文档以获取所需的可视化格式: https://scitools.org.uk/cartopy/docs/v0.15/matplotlib/intro.html

© www.soinside.com 2019 - 2024. All rights reserved.