62

我正在尝试构建一个简单的函数,该函数采用子图实例 ( matplotlib.axes._subplots.AxesSubplot) 并将其投影转换为另一个投影,例如,转换为其中一个cartopy.crs.CRS投影。

这个想法看起来像这样

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

def make_ax_map(ax, projection=ccrs.PlateCarree()):
    # set ax projection to the specified projection
    ...
    # other fancy formatting
    ax2.coastlines()
    ...

# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2)
# the first subplot remains unchanged
ax1.plot(np.random.rand(10))
# the second one gets another projection
make_ax_map(ax2)

当然,我可以只使用fig.add_subplot()函数:

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax1.plot(np.random.rand(10))

ax2 = fig.add_subplot(122,projection=ccrs.PlateCarree())
ax2.coastlines()

matplotlib但我想知道在定义子图轴投影是否有适当的方法来更改它。不幸的是,阅读 matplotlib API 并没有帮助。

4

3 回答 3

65

您不能更改现有轴的投影,原因如下。但是,您的根本问题的解决方案只是使用此处的 matplotlib 文档subplot_kw中描述的参数。例如,如果您希望所有子图都有投影,您可以这样做plt.subplots()cartopy.crs.PlateCarree

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

# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2, subplot_kw={'projection': ccrs.PlateCarree()})

关于实际问题,在创建轴集时指定投影决定了您获得的轴类,每种投影类型都不同。例如

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

ax1 = plt.subplot(311)
ax2 = plt.subplot(312, projection='polar')
ax3 = plt.subplot(313, projection=ccrs.PlateCarree())

print(type(ax1))
print(type(ax2))
print(type(ax3))

此代码将打印以下内容

<class 'matplotlib.axes._subplots.AxesSubplot'>
<class 'matplotlib.axes._subplots.PolarAxesSubplot'>
<class 'cartopy.mpl.geoaxes.GeoAxesSubplot'>

注意每个轴实际上是不同类的实例。

于 2016-01-04T15:37:53.437 回答
6

假设有多个轴用于 2D 绘图,例如...

fig = matplotlib.pyplot.Figure()
axs = fig.subplots(3, 4) # prepare for multiple subplots
# (some plotting here)
axs[0,0].plot([1,2,3])

...人们可以简单地销毁其中一个并用具有 3D 投影的新替换它:

axs[2,3].remove()
ax = fig.add_subplot(3, 4, 12, projection='3d')
ax.plot_surface(...)

请注意,与 Python 的其他部分不同,它使用从 1(而不是从 0)add_subplot开始的行列索引。

编辑:更改了我关于索引的错字。

于 2020-09-13T20:38:18.380 回答
0

按照这个问题的答案:

在 python 中,如何继承和覆盖类实例上的方法,将这个新版本分配给与旧版本相同的名称?

我发现了一个在创建斧头后更改其投影的技巧,这似乎至少在下面的简单示例中有效,但我不知道这个解决方案是否是最好的方法

from matplotlib.axes import Axes
from matplotlib.projections import register_projection

class CustomAxe(Axes):
    name = 'customaxe'

    def plotko(self, x):
        self.plot(x, 'ko')
        self.set_title('CustomAxe')

register_projection(CustomAxe)


if __name__ == '__main__':
    import matplotlib.pyplot as plt

    fig = plt.figure()

    ## use this syntax to create a customaxe directly
    # ax = fig.add_subplot(111, projection="customaxe")

    ## change the projection after creation
    ax = plt.gca()
    ax.__class__ = CustomAxe

    ax.plotko(range(10))    
    plt.show()
于 2019-05-22T07:22:31.790 回答