Search code examples
pythonmatplotlibcartopy

How to change an existing normal axis to cartopy axis in different size subplots?


I made a figure of three subplots with different size, but I don't know how to change the left one to lat-lon projection provided by cartopy. I want only the left subplot to have the property of projection : ccrs.PlateCarree() or transform = ccrs.PlateCarree() so that I can plot a map on it, while the other two subplots on the right should remain unchanged. Can someone tell me where I should put the projection information? Here is my code so far:

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

proj = ccrs.PlateCarree(central_longitude=0)
fig, axes = plt.subplot_mosaic("AABB;AABB;AACC", figsize = (16, 6))
plt.show()

enter image description here

All comments are welcome. Thanks in advance.


Solution

  • I don't think it's possible with the higher level methods like subplots or subplot_mosaic. But when you use the GridSpec method, you can provide axes-specific keywords.

    For example:

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec
    import cartopy.crs as ccrs
    import numpy as np
    
    fig = plt.figure(figsize=(9, 3), constrained_layout=True, facecolor="w")
    gs = GridSpec(3, 3, figure=fig)
    
    ax1 = fig.add_subplot(gs[:, 0], projection=ccrs.PlateCarree())
    ax2 = fig.add_subplot(gs[0:2, 1])
    ax3 = fig.add_subplot(gs[2:3, 1])
    
    ax1.set_extent((-90,90,90,-90))
    ax1.coastlines()
    ax2.imshow(np.random.rand(10,17))
    ax3.plot(np.random.randn(100).cumsum())
    

    https://matplotlib.org/stable/tutorials/intermediate/arranging_axes.html

    enter image description here