无法使用Python在底图中的地图投影上绘制圆圈

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

我正在尝试使用中心纬度,经度和半径在米勒投影地图上绘制圆圈。我无法让圆圈显示在地图投影上。我已经尝试使用链接中显示的不同技术绘制它们。

How to plot a circle in basemap or add artiste

How to make smooth circles on basemap projections

这是我的代码:

def plot_notams(dict_of_filtered_notams):

''' Create a map of the US and plot all NOTAMS from a given time period.'''

'''Create the map'''
fig = plt.figure(figsize=(8,6), dpi=200)
ax = fig.add_subplot(111)
m = Basemap(projection='mill',llcrnrlat=20, urcrnrlat=55, llcrnrlon=-135, urcrnrlon=-60, resolution='h')
m.drawcoastlines()
m.drawcountries(linewidth=2)
m.drawstates()
m.fillcontinents(color='coral', lake_color='aqua')
m.drawmapboundary(fill_color='aqua')
m.drawmeridians(np.arange(-130, -65, 10), labels=[1,0,0,1], textcolor='black')
m.drawparallels(np.arange(20, 60, 5), labels=[1,0,0,1], textcolor='black')

''' Now add the NOTAMS to the map '''

notam_data = dict_of_filtered_notams['final_notam_list']

for line in notam_data:
    notam_lat = float(line.split()[0])
    notam_lon = float(line.split()[1])
    coords = convert_coords(notam_lon, notam_lat)
    notam_lon, notam_lat = coords[0], coords[1]
    FL400_radius = np.radians(float(line.split()[2]))
    x,y = m(notam_lon, notam_lat)
    print("notam_lon = ",notam_lon, "notam_lat = ", notam_lat,"\n")
    print("x,y values = ",'%.3f'%x,",",'%.3f'%y,"\n")
    print("FL400_radius = ",('% 3.2f' % FL400_radius))
    print("")
    cir = plt.Circle((x,y), FL400_radius, color="white", fill=False)
    ax.add_patch(cir)

(convert_coords函数只是将notam_lon / notam_lat值格式化为可用格式,如下面的数据所示。)

这是我的数据的样子(您可以在上面的代码中看到它的打印位置):

notam_lon = -117.7839 notam_lat = 39.6431

x,y值= 1914342.075,2398770.441

FL400_radius = 6.98

这是我上面的代码产生的图像:My map projection

我也尝试使用map.plot()函数(具体来说,m.plot(x,y,“o”))代替“ax.add_patch(cir)”。当然,这有效但绘制了点或“o”。这是通过将“ax.add_patch(cir)”替换为“m.plot(x,y,”o“)而生成的图像。” Plot using map.plot() function in place of ax.add_patch()

最后,我正在使用底图1.2.0-1和matplotlib 3.0.3。我没有发现任何迹象表明这些版本不兼容。此外,2个月之前,当我最后这样做时,无法绘制圆圈也不是问题。我在这里不知所措。我感谢任何反馈。谢谢。

python-3.x matplotlib plot matplotlib-basemap
1个回答
1
投票

要在地图上绘制圆圈,您需要适当的位置(x,y)和半径。这是一个工作代码和结果图。

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

# make up 10 data points for location of circles
notam_lon = np.linspace(-117.7839, -100, 10)
notam_lat = np.linspace(39.6431, 52, 10)

# original radius of circle is too small
FL400_radius = 6.98   # what unit?

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)
m = Basemap(projection='mill', llcrnrlat=20, urcrnrlat=55, llcrnrlon=-135, urcrnrlon=-60, resolution='l')

# radiusm = (m.ymax-m.ymin)/10. is good for check plot
radiusm = FL400_radius*10000  # meters, you adjust as needed here

for xi,yi in zip(notam_lon, notam_lat):
    # xy=m(xi,yi): conversion (long,lat) to (x,y) on map
    circle1 = plt.Circle(xy=m(xi,yi), radius=radiusm, \
                         edgecolor="blue", facecolor="yellow", zorder=10)
    #ax.add_patch(circle1)  # deprecated
    ax.add_artist(circle1)  # use this instead

m.drawcoastlines()
m.drawcountries(linewidth=2)
m.drawstates()
m.fillcontinents(color='coral', lake_color='aqua')

# m.drawmapboundary(fill_color='aqua') <-- causes deprecation warnings
# use this instead:
rect = plt.Rectangle((m.xmin,m.ymin), m.xmax-m.xmin, m.ymax-m.ymin, facecolor="aqua", zorder=-10)
ax.add_artist(rect)

m.drawmeridians(np.arange(-130, -65, 10), labels=[1,0,0,1], textcolor='black')
m.drawparallels(np.arange(20, 60, 5), labels=[1,0,0,1], textcolor='black')

plt.show()

输出图:

enter image description here

希望这很有用。

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