如何在 Matplotlib 中的地图中插入比例尺?


要在 matplotlib 中的地图中插入比例尺,我们可以使用 AnchoredBar() 类实例化比例尺对象。

步骤

  • 设置图像大小并调整子图之间的间距和周围的间距。

  • 使用 numpy 创建随机数据。

  • 使用 imshow() 方法将数据显示为图像,即,在 2D 规则光栅上。

  • 使用 gca() 方法获取当前坐标系。

  • 绘制一条水平比例尺,在下方带有居中对齐的标签。

  • 向当前坐标系添加一条比例尺 artist。

  • 关闭坐标系。

  • 要显示图像,请使用 show() 方法。

示例

from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

data = np.random.rand(5, 5)

img = plt.imshow(data, cmap="YlGnBu")
ax = plt.gca()
scalebar = AnchoredSizeBar(ax.transData, 1, "1 Meter", 9)

ax.add_artist(scalebar)
ax.axis('off')
plt.show()

输出

更新于: 2021-06-03

2K+ 浏览

开启您的 职业生涯

完成课程,获得认证

立即开始
广告
© . All rights reserved.