python - 你能改变cartopy中的虹膜立方体投影吗

标签 python matplotlib cartopy python-iris

我真的很喜欢 cartopy 可以自动在不同的 map 投影中绘制的想法。然而,我不知道如何处理 Iris 立方体。作为一个姐妹项目,我希望我能够做到。可以做这样的事情吗?

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

someCube = I.load('someCube.pp')
ax = plt.axes(projection=ccrs.Robinson())
I.plot.contourf(someCube, transform=ccrs.Robinson())
plt.show()

谢谢

最佳答案

我采用了您的伪代码并使其可以与 Iris 的示例数据一起运行:

import iris
import iris.plot as iplt
import cartopy.crs as ccrs
import matplotlib.pyplot as plt


fname = iris.sample_data_path('air_temp.pp')
air_temp = iris.load_cube(fname)

ax = plt.axes(projection=ccrs.Robinson())
iplt.contourf(air_temp, transform=ccrs.Robinson(central_longitude=180))
ax.coastlines()

plt.show()

如果运行此代码,您将收到如下异常:

Traceback (most recent call last):
  File "using_custom_projections.py", line 11, in <module>
    iris.plot.contourf(air_temp, transform=ccrs.Robinson())
  File "lib/iris/plot.py", line 452, in contourf
    result = _draw_2d_from_points('contourf', None, cube, *args, **kwargs)
  File "lib/iris/plot.py", line 263, in _draw_2d_from_points
    result = _map_common(draw_method_name, arg_func, iris.coords.POINT_MODE, cube, data, *args, **kwargs)
  File "lib/iris/plot.py", line 406, in _map_common
    assert 'transform' not in kwargs, 'Transform keyword is not allowed.'
AssertionError: Transform keyword is not allowed.

它试图告诉您,您不需要告诉它立方体位于哪个“变换”(或坐标系)中。原因是 Iris 立方体应该包含有关基础数据的完整元数据:坐标系是该元数据的一部分。

因此,要使示例正常工作,您只需删除 contourf 调用中的 Transform 关键字参数即可:

import iris
import iris.plot as iplt
import cartopy.crs as ccrs
import matplotlib.pyplot as plt


fname = iris.sample_data_path('air_temp.pp')
air_temp = iris.load_cube(fname)

ax = plt.axes(projection=ccrs.Robinson(central_longitude=180))
iplt.contourf(air_temp)
ax.coastlines()

plt.show()

contoured result

iris gallery中有一个类似的例子,具体来说http://scitools.org.uk/iris/docs/latest/examples/graphics/rotated_pole_mapping.html#rotated-pole-mapping-03 (示例中的最后一个图)。

HTH,

关于python - 你能改变cartopy中的虹膜立方体投影吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13766275/

相关文章:

python - 在 python 中,如何让我自己的异常类在 shell 中漂亮地打印出来?

python - 如何在 Python Tkinter GUI 中嵌入 Cartopy?

python - 使用 SciPy 数值求解 ODE

python - Pyplot 堆积直方图 - 列中出现的次数

python - 从 Matplotlib 中的散点图提取数据

matplotlib - 尝试绘制简单的纬度/经度点时遇到奇怪的问题

matplotlib - 使用 Cartopy 合并国家

python - 在循环内递增 for 循环

python - 从 Excel 电子表格中提取值

python - TypeError : argument 1 must be pygame. Surface,而不是 str 如何修复?