python - 如何在Python中设置 basemap 的偏移量?

标签 python matplotlib-basemap

这是 basemap 的一个示例:

fig = plt.figure(figsize=(10,6))

ax = fig.add_subplot(121)
ax.set_title('Default')

# miller projection
map = Basemap(projection='mill',lon_0=180)
# plot coastlines, draw label meridians and parallels.
map.drawcoastlines()
map.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0])
map.drawmeridians(np.arange(map.lonmin,map.lonmax+30,60),labels=[0,0,0,1])

ax = fig.add_subplot(122)
ax.set_title('Add offset')

# miller projection
map = Basemap(projection='mill',lon_0=180)
# plot coastlines, draw label meridians and parallels.
map.drawcoastlines()
map.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0],xoffset=100,yoffset=100)
map.drawmeridians(np.arange(map.lonmin,map.lonmax+30,60),labels=[0,0,0,1],xoffset=100,yoffset=100)

我想在 xlabel/ylabel 和轴之间添加更多空间。 但是,当添加xoffsetyoffset时,空间会更小。

example

最佳答案

basemap 不再积极开发,但维护仍会持续一段时间。这意味着由于其他包的更改而导致的问题仍将得到修复,但不会添加新功能。不管怎样,修​​复部分可能需要一些时间,我猜测平行线和经线的 xoffset 功能正在受到影响。但是,查看basemap文档,xoffsetyoffset的功能描述为

xoffset: label offset from edge of map in x-direction (default is 0.01 times width of map in map projection coordinates).

yoffset: label offset from edge of map in y-direction (default is 0.01 times height of map in map projection coordinates).

通过操作 drawparallels()drawmeridians() 生成的 Text 对象,可以很容易地模拟这一点。这些函数的结果存储在一个 dict 中,其中包含每个绘制的平行/子午线的列表元组,其中第二个包含文本标签。 Text 对象具有 get_position()set_position() 方法,结合轴限制和上面的定义,可以用于计算偏移量:

from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

fig = plt.figure(figsize=(10,6))

ax = fig.add_subplot(121)
ax.set_title('Default')

# miller projection
map = Basemap(projection='mill',lon_0=180)
# plot coastlines, draw label meridians and parallels.
map.drawcoastlines()
map.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0])
map.drawmeridians(np.arange(map.lonmin,map.lonmax+30,60),labels=[0,0,0,1])

ax = fig.add_subplot(122)
ax.set_title('Add offset')

# miller projection
map = Basemap(projection='mill',lon_0=180)
# plot coastlines, draw label meridians and parallels.
map.drawcoastlines()

##getting axes dimensions
x0,x1 = ax.get_xlim()
w = x1-x0
y0,y1 = ax.get_ylim()
h = y1-y0

xoffset = 0.05
yoffset = 0.05

result = map.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0])
##
for key, (lines,texts) in result.items():
    for text in texts:
        x,y = text.get_position()
        text.set_position((x0-xoffset*w,y))


result = map.drawmeridians(np.arange(map.lonmin,map.lonmax+30,60),labels=[0,0,0,1])
for key, (lines,texts) in result.items():
    for text in texts:
        x,y = text.get_position()
        text.set_position((x,y0-yoffset*h))



plt.show()

结果图如下所示:

Result of the above code

关于python - 如何在Python中设置 basemap 的偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53457514/

相关文章:

Python LXML- 检查变量值是否具有非 ASCII 值的方法,如果是则返回 unicode 值

python - 在 PyQt 中绘制多边形

python - Matplotlib:用鼠标画出矩形形状的选区

python - 如何通过 python 中的 drawparallels 将标签字体设置为 "Time New Roman"

python - 在 Golang 中解密在 Python AES CFB 中加密的内容

python - 如何在 django 中使用 handler404 View ?

python - 尝试下载html页面来创建一个非常简单的网络爬虫

ipython - 在 ipython 上安装 basemap 时遇到问题

python - 使用 matplotlib 移动网格

python - 尝试在 mac osx 上安装 geos 3.3.8 时出错