matplotlib 如何只画一堆圆的轮廓

mspsb9vt  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(106)

我想画出我创建的几个圆的交点的边界。这是我到目前为止所做的:
编辑

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.patches import PathPatch
from matplotlib.path import Path
import matplotlib.ticker as mticker
import numpy as np

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

ax.set_extent([11, 38, 56, 70], crs=ccrs.PlateCarree())

ax.add_feature(cfeature.BORDERS, linestyle='-', linewidth=1)
ax.coastlines(linewidth=1)

# Adding the circles
radius = 120 # km
n_samples = 80 # Number of points on the circle
lons = (21.64, 22.5, 23.82, 24.5, 25.44, 26.32, 26.9, 27.11, 27.38, 29.45, 29.8)
lats = (60.13, 61.81, 63.1, 60.56, 62.3, 64.77, 67.14, 60.9, 62.86, 63.84, 61.91)

for lon, lat in zip(lons,lats):
    circle = Geodesic().circle(lon, lat, radius*1000., n_samples=n_samples)
    feature = cartopy.feature.ShapelyFeature([Polygon(circle)], ccrs.Geodetic(), fc='orange', alpha=0.4, ec="black", lw=0.5, linestyle="-")
    ax.add_feature(feature)

字符串
我已经在网上搜索过了,但到目前为止还没有找到解决方案。如果有人能帮我解决这个问题,那就太好了:)

smdnsysy

smdnsysy1#

你没有提到问题是什么,所以很难回答。
但是你可能无意中混合了不同的单位来创建你的圆。你定义你的Map是正交的,中心在纬度/经度和半径在米。你期望的结果是什么?
假设你指定了具有PlateCarree()投影的圆,半径也应该这样定义。在你现在的代码中一切都很好,但是你设置了半径为250000!!
例如,如果您将半径设置为2.5度,则看起来像下面所示的图像:
x1c 0d1x的数据
你必须决定你是否想要一个圆是地球上的圆(大地),Map投影或图像上的圆(图形坐标)等,然后以一致的方式选择相应的单位。
编辑:
Geodetic类当然是一个很好的选择,pyproj包也有这种类型的操作功能。
可以绘制多个圆的并集,如下面的示例所示,它使用Shapely执行并集。

import matplotlib.pyplot as plt

from cartopy.geodesic import Geodesic
import cartopy.crs as ccrs
import cartopy.feature as cfeature

from shapely.geometry import Polygon
from shapely.ops import unary_union

fig, ax = plt.subplots(figsize=(10, 6), subplot_kw=dict(projection=ccrs.Mercator()))

ax.set_extent([11, 38, 56, 70], crs=ccrs.PlateCarree())
ax.add_feature(cfeature.BORDERS, linestyle='-', linewidth=.3)
ax.coastlines(linewidth=.3, resolution="50m")

# Adding the circles
radius = 120 # km
n_samples = 80 # Number of points on the circle
lons = (21.64, 22.5, 23.82, 24.5, 25.44, 26.32, 26.9, 27.11, 27.38, 29.45, 29.8)
lats = (60.13, 61.81, 63.1, 60.56, 62.3, 64.77, 67.14, 60.9, 62.86, 63.84, 61.91)

geo = Geodesic()
circles = [Polygon(geo.circle(lon, lat, radius*1000., n_samples=n_samples)) for lon, lat in zip(lons, lats)]

feature = cfeature.ShapelyFeature(unary_union(circles), ccrs.PlateCarree(), fc='orange', alpha=0.4, ec="k", lw=2, linestyle="-")
# feature = cfeature.ShapelyFeature(circles, ccrs.PlateCarree(), fc='orange', alpha=0.4, ec="k", lw=2, linestyle="-")
ax.add_feature(feature)

字符串



如果没有union,只绘制circles,它看起来像:


相关问题