当我在python中使用底图时,经度有时会颠倒,翻转地图

When I use basemap in python, longitude is sometimes reversed, flipping the map

在 python 中,我使用底图 (https://matplotlib.org/basemap/) for plotting spatial data, and I've used it for several years without any large problems. I recently had to reinstall python3 (through conda, along with a number of modules) and basemap now has a strange issue: under certain conditions, the map will be displayed with flipped longitudes, switching east and west. As an example, I use this code: https://matplotlib.org/basemap/users/robin.html。如果我按原样使用该代码,地图显示正常,但是当我设置 lon_0=180 时,地图被翻转,如下图所示。

Image of map problem

将 lon_0 设置为任何正数会导致翻转地图,而 0 或负数会导致正确的地图。 lon_0 应该简单地设置绘制地图的中心经度,不应该有这种行为,所以我不确定发生了什么。有没有人以前见过这种行为,或者对如何解决它有建议?我可以更改我的代码来解决它,但我宁愿让事情正常工作。

我正在使用 python3.7.3。我已经尝试使用命令 "conda install -c anaconda basemap" 更新底图,但它告诉我底图已经是最新的了。

这是代码。它与上面链接的代码相同,但 lon_0 设置为 180。

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
# lon_0 is central longitude of projection.
# resolution = 'c' means use crude resolution coastlines.
m = Basemap(projection='robin',lon_0=180,resolution='c')
m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,30.))
m.drawmeridians(np.arange(0.,360.,60.))
m.drawmapboundary(fill_color='aqua')
plt.title("Robinson Projection")
plt.show()

当我运行代码时,唯一的输出是这个,这似乎无关:

map_test.py:36: MatplotlibDeprecationWarning: dedent 函数在 Matplotlib 3.1 中已弃用,并将在 3.3 中删除。请改用 inspect.cleandoc。 m = 底图(投影='robin',lon_0=180,分辨率='c')

有什么想法吗?

这应该可以解决问题。只需在创建轴后添加:

# Matplotlib >= 3.1.0 (introduced "set_inverted")
ax.xaxis.set_inverted(False)

# Matplotlib < 3.1.0 (this is just the source code for "set_inverted")
interval = ax.xaxis.get_view_interval()
ax.set_xlim(sorted(interval), auto=None)

经过反复试验,选择 中心经度而不是正经度似乎也有效:

m = Basemap('robin', lon_0=-180)