[英]Matplotlib with Cartopy and gridspec
我想创建一个 plot,左侧有一个 Cartopy plot,右侧有两个堆叠的 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 plot,我无法让它粘在左侧的轴上。 我想我使用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 plot 贴在左侧子图的轴上?
发生这种情况是因为在创建左窗格后,您为覆盖整个图的 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.