放大cartopy地图时如何剪辑子图边界外的文本?

问题描述

我正在使用带有 cartopy 包的 matplotlib 在 matplotlib 图的子图中显示巴基斯坦地图及其城市名称,如图 1 所示。 但我的问题是,当我放大子图以查看更多细节时,一切正常,但绘制的文本(城市名称显示在子图边界之外,如何裁剪文本外。

这是我在 cartopy 地图上绘制城市名称代码

    def draw_cities(self,extent,axis):

        cities_df = pd.read_csv('assets/maps/cities.csv')

        # axis.plot(lons,lats,markersize=5,marker='o',linestyle='',color='#b30909',transform=ccrs.PlateCarree(),#           label='ASOS Station')

        self.cities_plot = axis.scatter(cities_df.lng.values,cities_df.lat.values,color='red',marker='.',transform=ccrs.PlateCarree())
        transform = ccrs.PlateCarree()._as_mpl_transform(axis)  # set transform for annotations
        self.cities_text = []
        # text annotations
        for lat_s,lon_s,name_s in zip(cities_df.lat.values,cities_df.lng.values,cities_df.city.values):  # loop through lats/lons/names
            if lon_s > extent[0] and lon_s < extent[1] and lat_s > extent[2] and lat_s < extent[3]:
                text = axis.text(lon_s,lat_s,name_s,{'color': 'r','fontsize': 6},horizontalalignment='center',verticalalignment='bottom',clip_on=True,transform=transform)

                self.cities_text.append(text)

cartopy 地图上的放大代码

    def zoom_factory(self,ax,base_scale=2.):
        def zoom_fun(event):
            # get the current x and y limits
            cur_xlim = ax.get_xlim()
            cur_ylim = ax.get_ylim()
            cur_xrange = (cur_xlim[1] - cur_xlim[0]) * .5
            cur_yrange = (cur_ylim[1] - cur_ylim[0]) * .5
            xdata = event.xdata  # get event x location
            ydata = event.ydata  # get event y location
            if event.button == 'up':
                # deal with zoom in
                scale_factor = 1 / base_scale
            elif event.button == 'down':
                # deal with zoom out
                scale_factor = base_scale
            else:
                # deal with something that should never happen
                scale_factor = 1
                print(event.button)
            # set new limits

            ax.figure.canvas.toolbar.push_current() # resolve home button issue

            # zoom for better user erxperince
            ax.set_xlim([xdata - (xdata - cur_xlim[0]) / scale_factor,xdata + (cur_xlim[1] - xdata) / scale_factor])
            ax.set_ylim(
                [ydata - (ydata - cur_ylim[0]) / scale_factor,ydata + (cur_ylim[1] - ydata) / scale_factor])

            self.Canvas1.draw_idle()

        fig = ax.get_figure()  # get the figure of interest
        # attach the call back
        fig.canvas.mpl_connect('scroll_event',zoom_fun)

        # return the function
        return zoom_fun

这是放大前的输出(图1),很好:

figure-1

这是放大后的输出(图2),我想剪辑在子图边界外以蓝色圆圈显示的文本:

figure-2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...