添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

缩放制图时更新纬线/纵线坐标轴

0 人关注

我正在cartopy中绘制一些数据。我希望能够放大地图上的某个区域,并让经纬度轴更新以反映放大的区域。相反,当我放大的时候,它们就完全消失了。我怎样才能解决这个问题?

以下是我生成坐标轴的代码

plt.figure()
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, edgecolor='black')
gl = ax.gridlines(crs=cartopy.crs.PlateCarree(), draw_labels=True,
                  linewidth=2, color='gray', alpha=0.5, linestyle='--')
# plot some stuff here
    
1 个评论
不可能使用交互式缩放。
python
python-3.x
matplotlib
cartopy
mfgeng
mfgeng
发布于 2021-04-04
1 个回答
Dr Fred
Dr Fred
发布于 2021-11-01
已采纳
0 人赞同

在交互式模式下,有可能更新车篷网格线,但你需要对导航工具条进行子类化。

在下面这个例子中,我使用了PySide/QT5的示例代码,它允许我替换一个子类化的工具条,然后合并到gridliner示例代码中。每次使用缩放/平移/原点时,重载的工具条回调都会重新创建网格线。

我使用了python3.8,matplotlib-3.4.2,cartopy-0.20

import sys
from PySide2 import QtWidgets
from PySide2.QtWidgets import QVBoxLayout
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import cartopy.crs as ccrs
class CustomNavigationToolbar(NavigationToolbar):
    toolitems = [t for t in NavigationToolbar.toolitems if t[0] in ('Home', 'Pan', 'Zoom', 'Save')]
    def __init__(self, canvas, parent, coordinates=True, func_recreate_gridlines=None):
        print('CustomNavigationToolbar::__init__')
        super(CustomNavigationToolbar, self).__init__(canvas, parent, coordinates)
        self.func_recreate_gridlines = func_recreate_gridlines
    def home(self, *args):
        print('CustomNavigationToolbar::home')
        super(CustomNavigationToolbar, self).home(*args)
        if self.func_recreate_gridlines is not None:
            self.func_recreate_gridlines()
    def release_pan(self, event):
        print('CustomNavigationToolbar::release_pan')
        super(CustomNavigationToolbar, self).release_pan(event)
        if self.func_recreate_gridlines is not None:
            self.func_recreate_gridlines()
    def release_zoom(self, event):
        print('CustomNavigationToolbar::release_zoom')
        super(CustomNavigationToolbar, self).release_zoom(event)
        if self.func_recreate_gridlines is not None:
            self.func_recreate_gridlines()
class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        print('ApplicationWindow::__init__')
        super().__init__()
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main)
        self.layout = QVBoxLayout(self._main)
        self.fig = Figure()
        self.canvas = FigureCanvas(self.fig)
        self.toolbar = CustomNavigationToolbar(self.canvas, self,
                                               coordinates=True,
                                               func_recreate_gridlines=self.recreate_gridlines)
        self.layout.addWidget(self.canvas)
        self.addToolBar(self.toolbar)
        # figure setup taken from gridlines example at
        # https://scitools.org.uk/cartopy/docs/latest/matplotlib/gridliner.html
        projection = ccrs.RotatedPole(pole_longitude=120.0, pole_latitude=70.0)
        self.ax = self.canvas.figure.add_subplot(1, 1, 1, projection=projection)
        self.ax.set_extent([-6, 3, 48, 58], crs=ccrs.PlateCarree())
        self.ax.coastlines(resolution='10m')
        self._gl = None
        self.recreate_gridlines()
    def recreate_gridlines(self):
        print('ApplicationWindow::recreate_gridlines')
        print(' remove old gridliner artists')
        if self._gl is not None:
            for artist_coll in [self._gl.xline_artists, self._gl.yline_artists, self._gl.xlabel_artists, self._gl.ylabel_artists]:
                for a in artist_coll:
                    a.remove()
            self.ax._gridliners = []
        print(' self.ax.gridlines()')
        self._gl = self.ax.gridlines(crs=ccrs.PlateCarree(),
                                     draw_labels=True, dms=True, x_inline=False, y_inline=False)
if __name__ == "__main__":
    qapp = QtWidgets.QApplication(sys.argv)