带有 Cartopy 和 gridspec 的 Matplotlib
Matplotlib with Cartopy and gridspec
我想创建一个图,左侧是 Cartopy 图,右侧是两个堆叠的 Matplotlib 图。如果我只使用 Matplotlib 图,代码将如下所示。
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
# LEFT
ax = fig.add_subplot(gs[:, 0])
ax.plot(np.arange(0, 1000, 100))
# RIGHT TOP
ax = fig.add_subplot(gs[0, 1])
ax.plot(np.arange(0, 1000, 100))
# RIGHT BOTTOM
ax = fig.add_subplot(gs[1, 1])
ax.plot(np.arange(0, 1000, 100))
plt.show()
...到目前为止一切顺利。
但是,如果我添加 Cartopy 图,我无法使其紧贴左侧的轴。我想我使用 ax = plt.axes()
.
的方式有问题
import cartopy.crs as ccrs
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
# LEFT
ax = fig.add_subplot(gs[:, 0])
ax = plt.axes(
projection = ccrs.Orthographic(
central_longitude=0,
central_latitude=0
)
)
ax.stock_img()
# RIGHT TOP
ax = fig.add_subplot(gs[0, 1])
ax.plot(np.arange(0, 1000, 100))
# RIGHT BOTTOM
ax = fig.add_subplot(gs[1, 1])
ax.plot(np.arange(0, 1000, 100))
plt.show()
如何使 Cartopy 图贴在左侧子图的轴上?
发生这种情况是因为在创建 left-pane 之后,您为覆盖整个图形的 cartopy 创建了一个新轴。相反,您需要在 fig.add_subplot
内传递 projection
,如下所示:
import cartopy.crs as ccrs
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
# LEFT
ax = fig.add_subplot(gs[:, 0], projection = ccrs.Orthographic(
central_longitude=0,
central_latitude=0
))
ax.stock_img()
# RIGHT TOP
ax = fig.add_subplot(gs[0, 1])
ax.plot(np.arange(0, 1000, 100))
# RIGHT BOTTOM
ax = fig.add_subplot(gs[1, 1])
ax.plot(np.arange(0, 1000, 100))
plt.show()
我想创建一个图,左侧是 Cartopy 图,右侧是两个堆叠的 Matplotlib 图。如果我只使用 Matplotlib 图,代码将如下所示。
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
# LEFT
ax = fig.add_subplot(gs[:, 0])
ax.plot(np.arange(0, 1000, 100))
# RIGHT TOP
ax = fig.add_subplot(gs[0, 1])
ax.plot(np.arange(0, 1000, 100))
# RIGHT BOTTOM
ax = fig.add_subplot(gs[1, 1])
ax.plot(np.arange(0, 1000, 100))
plt.show()
...到目前为止一切顺利。
但是,如果我添加 Cartopy 图,我无法使其紧贴左侧的轴。我想我使用 ax = plt.axes()
.
import cartopy.crs as ccrs
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
# LEFT
ax = fig.add_subplot(gs[:, 0])
ax = plt.axes(
projection = ccrs.Orthographic(
central_longitude=0,
central_latitude=0
)
)
ax.stock_img()
# RIGHT TOP
ax = fig.add_subplot(gs[0, 1])
ax.plot(np.arange(0, 1000, 100))
# RIGHT BOTTOM
ax = fig.add_subplot(gs[1, 1])
ax.plot(np.arange(0, 1000, 100))
plt.show()
如何使 Cartopy 图贴在左侧子图的轴上?
发生这种情况是因为在创建 left-pane 之后,您为覆盖整个图形的 cartopy 创建了一个新轴。相反,您需要在 fig.add_subplot
内传递 projection
,如下所示:
import cartopy.crs as ccrs
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(2, 2)
# LEFT
ax = fig.add_subplot(gs[:, 0], projection = ccrs.Orthographic(
central_longitude=0,
central_latitude=0
))
ax.stock_img()
# RIGHT TOP
ax = fig.add_subplot(gs[0, 1])
ax.plot(np.arange(0, 1000, 100))
# RIGHT BOTTOM
ax = fig.add_subplot(gs[1, 1])
ax.plot(np.arange(0, 1000, 100))
plt.show()