【问题标题】:How to insert scale bar in a map in matplotlib如何在 matplotlib 的地图中插入比例尺
【发布时间】:2017-02-08 17:57:29
【问题描述】:

关于如何在 matplotlib 中的地图中插入显示长度比例的比例尺的任何想法?类似于我附上的那个。

或者也许有关于自动测量和显示距离的任何想法(而不是绘制箭头并手动写入距离!)?

谢谢:)

【问题讨论】:

标签: python matplotlib scale matplotlib-basemap


【解决方案1】:

matplotlib 中有一个已经存在的比例尺类,名为 AnchoredSizeBar。在下面的示例中,AnchoredSizeBar 用于将比例尺添加到图像(或映射到 100x100 米的随机区域)。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
import matplotlib.font_manager as fm
fontprops = fm.FontProperties(size=18)
fig, ax = plt.subplots()
ax.imshow(np.random.random((10,10)),extent=[0,100,0,100]) 

Extent 定义图像水平和垂直值的最大值和最小值。

scalebar = AnchoredSizeBar(ax.transData,
                           20, '20 m', 'lower center', 
                           pad=0.1,
                           color='white',
                           frameon=False,
                           size_vertical=1,
                           fontproperties=fontprops)

ax.add_artist(scalebar)

AnchoredSizeBar 的前四个参数是坐标系的变换对象、比例尺长度、标签和位置。进一步的可选参数会改变布局。这些在文档中进行了解释。

ax.set_yticks([])
ax.set_xticks([])

这给了

【讨论】:

    【解决方案2】:

    我会尝试matplotlib-scalebar 包。 (例如您的示例 c。)

    假设您正在绘制带有imshow 或类似名称的地图图像,并且您知道像素宽度/单元格大小(地图图像上一个像素的实际等效大小),您可以自动创建比例尺:

    这个例子直接来自PyPi matplotlib-scalebar package page,但为了完整起见:

    import matplotlib.pyplot as plt
    import matplotlib.cbook as cbook
    from matplotlib_scalebar.scalebar import ScaleBar
    
    plt.figure()
    image = plt.imread(cbook.get_sample_data('grace_hopper.png'))
    plt.imshow(image)
    scalebar = ScaleBar(0.2) # 1 pixel = 0.2 meter
    plt.gca().add_artist(scalebar)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 2013-12-31
      • 1970-01-01
      • 2013-07-16
      • 2014-11-19
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多