How to change interactively center of a cartopy projection?
Asked Answered
K

0

6

Is there a way to improve the rendering of this interactive matplotlib/cartopy script ?

Please install first ipympl https://github.com/matplotlib/ipympl

I haven't found how to change the central longitude and latitude without instantiate a new figure.

This script is missing fluidity when clicking with left mouse button and choose the new center of the projection. I am looking for a trackball behaviour as in https://threejs.org/examples/misc_controls_trackball.

Any help/suggestion is welcomed.

%matplotlib widget

import numpy as np
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs

fig = plt.figure(figsize=(10,6), layout='constrained')

proj0 = ccrs.PlateCarree()
proj1 = ccrs.Orthographic(0, 80)

ax1 = fig.add_subplot(1, 1, 1, projection=proj1)
ax1.coastlines()
ax1.gridlines(xlocs=np.arange(-180,180,10), ylocs=np.arange(-80,90,10))
ax1.set_global()

def onpress(event):
    global proj1
    if event.button == 1:
        lon, lat = proj0.transform_point(event.xdata, event.ydata, src_crs=proj1)
        proj1 = ccrs.Orthographic(lon, lat)
        ax1 = fig.add_subplot(1, 1, 1, projection=proj1)
        ax1.coastlines()
        ax1.gridlines(xlocs=np.arange(-180,180,10), ylocs=np.arange(-80,90,10))
        ax1.set_global()
        plt.draw()

fig.canvas.mpl_connect('button_press_event', onpress)

plt.show()

enter image description here

Kearse answered 2/10, 2022 at 13:48 Comment(3)
Do you want to change the way the user interacts with the projection e.g. click and drag to rotate the globe? Or do you just want to make the current code more efficient?Annulus
The current code more efficient. It is not fluid as expected. If the user interacts have to be changed for that no problem.Kearse
Unfortunately, I think the answer is that matplotlib, backed by PROJ, is just no way to build a live re-projectable interactive such as one you're used to seeing with WebGL or other technologies built for 3D rendering for interactive user interfaces. The code you've written is a one-way pipeline. You define the object, then call a non-python extension library to project/transform it, then hand off the transformed result to a matplotlib axis object, then matplotlib decides how the transformed object should be rendered. Going back and changing the projection requires redoing all this work.Astonish

© 2022 - 2025 — McMap. All rights reserved.