为什么不能在Matplotlib/Cartopy地图上添加图像?

2024-10-01 07:17:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,其中包括用卡通画在地图上绘制数据。在

到目前为止,一切都在运行,但我一直在重构代码,使程序的其他部分可以调用不同的函数。为此,我有一个函数将我的背景添加到地图中,另一个函数在指定的纬度/经度处添加一个placemarker。很明显,我想要背景上方的位置标记,但我似乎不能让它工作。在

作为背景,我想能够使用卡通股票图像或网络地图瓷砖。问题是相同的任何一种方式,所以我使用卡通背景为当前的测试目的。这个函数是:

def custom_background(self, source_point):
    cartmap = self.plot
    source_point = source_point.split(" ")
    source_point = (float(source_point[0]), float(source_point[1]))
    dx = 2.5
    dy = 5
    pad = 0.5
    lon_min, lon_max = source_point[0]-dx, source_point[0]+dx
    lat_min, lat_max = source_point[1]-dy, source_point[1]+dy
    area = 4*dx*dy
    zoom = self.get_zoom(area) ##only relevant when using a map tile
    cartmap.set_extent([lat_min-pad, lat_max+pad, lon_min-pad, lon_max+pad])
    #~ cartmap.add_image(self.tile, zoom)
    cartmap.add_feature(cartopy.feature.LAND, zorder=1)
    return cartmap

以下是placemark函数:

^{pr2}$

这两个都被称为一个接一个,就像这样:

    cartmap = self.custom_background(mysrc)
    #~ cartmap=self.plot
    self.add_point_icon(x1, y1, cartmap)

结果:

如果我按原样运行代码,则映射图如下所示:

map with background

如果我将其更改为(即绕过绘制背景的函数):

    #~ cartmap = self.custom_background(mysrc)
    cartmap=self.plot
    self.add_point_icon(x1, y1, cartmap)

然后我得到:

enter image description here

为什么我不能让红色的“加号”标志出现在地图的顶部?我尝试过设置不同对象的“zorder”参数,但它似乎没有任何作用。我现在完全不知所措。任何帮助都将不胜感激,谢谢。在

编辑:也许我还应该包括创建子批次的行:

def __init__(self, mylevs):
    self.fig, self.header, self.footer, self.plot, self.legend = 
        self.create_spec()

def create_spec(self):
    """Define layout of figure"""

    #left column: header, footer, plt
    fig = plt.figure(figsize=(12,10))
    layout = 1
    if layout == 1: #Default
        widths = [8,1]
        heights = [2, 10, 3]
        column_border = 0.75
        pad = 0.1
        colorbar_width = 0.05
        spec = gridspec.GridSpec(ncols=1, nrows=3, width_ratios = [1], height_ratios=heights, left=0.1, right = column_border)
        #right column: colorbar
        spec2 = gridspec.GridSpec(ncols=1, nrows=1, width_ratios = [1], height_ratios=[1], left=column_border+pad, right=column_border+pad+colorbar_width)

        header = plt.subplot(spec[0,0])
        footer = plt.subplot(spec[2,0])
        plot = plt.subplot(spec[1,0], projection=cimgt.OSM().crs)
        legend = plt.subplot(spec2[0,0])

    return fig, header, footer, plot, legend

Tags: 函数selfsourceplot地图columnpltpoint
1条回答
网友
1楼 · 发布于 2024-10-01 07:17:30

问题是在从地图块切换回卡通背景时,我忘记了从OSM()返回中央空调系统(). 在地图平铺投影中,placemark正在视图窗口外绘制。在

相关问题 更多 >