【问题标题】:Matplotlib: Create lat/lon white/black round bounding box around basemapMatplotlib:在底图周围创建纬度/经度白色/黑色圆形边界框
【发布时间】:2017-11-22 21:25:41
【问题描述】:

我正在尝试将我的 matplotlib 地图周围的默认框架的设计更改为 lon/lat 白色和黑色圆形“边界框”类型的框架,例如,黑色从 W 90 度延伸到 45 度 W ,和白色从45W到0度等。

为矩形地图的 R 用户开发了一个非常相似的代码(参见:http://menugget.blogspot.co.uk/2012/04/add-frame-to-map.html),但我没有为 Python 用户找到任何东西。

有谁知道 Python 中是否有允许这种框架设计的包?

以下是我在 python 中绘制南极洲的代码。代码下面的图显示了我想要实现的 R 等价物。

代码:

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

fig = plt.figure(figsize=(11.7,8.3))

plt.subplots_adjust(left=0.05,right=0.95,top=0.90,bottom=0.05,wspace=0.15,hspace=0.05)
ax = plt.subplot(111)

x1 = -180
x2 = 180
y1 = -90
y2 = 90

m = Basemap(projection='spstere', llcrnrlat=y1,urcrnrlat=y2,llcrnrlon=x1,urcrnrlon=x2,boundinglat=-63,lon_0=180, resolution='l',round=True)
m.drawmeridians(np.arange(0,360,60),labels=[1,1,1,1],linewidth=0.5, fontsize=10, dashes=[1,5])
m.drawparallels(np.arange(-90,90,20),linewidth=0.5, fontsize=10, dashes=[1,5])
m.drawcoastlines(linewidth=0.5)

fig.show()

【问题讨论】:

    标签: python matplotlib matplotlib-basemap


    【解决方案1】:

    我不确定这是否特别强大,但它适用于您的示例,并且是我第一次真正体验底图(之前做过一些映射)。

    from matplotlib.patches import Wedge
    def draw_round_frame(m, width_percent=0.05, degree=45):
        centre_x = (ax.get_xlim()[0] + ax.get_xlim()[1]) / 2
        centre_y = (ax.get_ylim()[0] + ax.get_ylim()[1]) / 2
        width = abs(centre_x) * width_percent
    
        inner_radius = abs(centre_x) - width/2
        outer_radius = inner_radius + width
    
        angle_breaks = list(range(0, 361, degree))
    
        for i, (from_angle, to_angle) in enumerate(list(zip(angle_breaks[:-1], angle_breaks[1:]))):
            color='white' if i%2 == 0 else 'black'
            wedge = Wedge((centre_x, centre_y), outer_radius, from_angle, to_angle, width=outer_radius - inner_radius, 
                          facecolor=color,
                          edgecolor='black',
                          clip_on=False,
                          ls='solid',
                          lw=1)
            ax.add_patch(wedge)
    

    要使用只需在fig.show()之前调用它

    draw_round_frame(m)
    plt.show()
    

    它可能应该比它更多地使用地图坐标,并且它与轴重叠,因为我目前无法找到在地图本身周围添加更多空间的方法,但可能会给你一个开始的想法。

    【讨论】:

    • 太好了,这正是我想要的,非常感谢肯!我调整了 boundinglat 值(从 -63 到 -62)以在轮廓和框架之间获得更多空间,并将框架的 width_percent 值从 0.05 缩短到 0.02。这样就行了。
    猜你喜欢
    • 2023-03-22
    • 2011-12-28
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    相关资源
    最近更新 更多