【问题标题】:get matplotlib / cartopy contour auto label coordinates获取 matplotlib / cartopy 轮廓自动标签坐标
【发布时间】:2021-03-25 21:42:28
【问题描述】:

如何检索下例中自动确定的等高线标签坐标?

来自the documentation的matplotlib示例

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt


delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

fig, ax = plt.subplots()
CS = ax.contour(X, Y, Z)
CS_labels = ax.clabel(CS, inline=True, fontsize=10)
ax.set_title('Simplest default with labels')

我想做类似的事情

label_locations = CS_labels.get_label_coords()

这样我就可以从自动选择的集合开始,然后根据需要手动修改。这在处理labels in geospatial coordinates 时特别有用。

更新:
swatchai 提供的解决方案适用于 matplotlib 和 cartopy。

for txobj in CS.labelTexts:
    pos = txobj.get_position()
    txt = txobj.get_text()
    print(pos, txt)

标签位置最好从CS 对象而不是CS_labels 对象中检索。

注意:
tdy 的解决方案仅适用于 matplotlib,但不适用于使用 cartopy GeoAxes 时,因为 ax.clabel()CS_labels 返回 'NoneType',因此无法以这种方式访问​​ CS_labels[0].get_position()

【问题讨论】:

标签: python matplotlib coordinates contour cartopy


【解决方案1】:

如果我理解正确,你可以使用这个:

label_locations = [label.get_position() for label in CS_labels]

# [( 0.9499999999999864,   0.5360418495133943),
#  ( 1.8999999999999821,   1.755885999331959),
#  ( 0.15000000000000968,  0.8499999999999899),
#  (-0.9000000000000075,  -0.75588599933193),
#  ( 0.4135112591682213,   0.124999999999992),
#  (-0.42169775490495853, -0.2750000000000066)]

【讨论】:

    【解决方案2】:

    对于cartopy,一旦你创建了轮廓标签,你就可以通过

    CS.labelTexts  #CS is contour_collection set
    

    这是演示所有步骤的可运行代码。

    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import cartopy
    import numpy as np
    
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 3.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = np.exp(-X**2 - Y**2)*20
    Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)*20
    Z = (Z1 - Z2) * 2
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
    
    CS = ax.contour(X, Y, Z)
    
    ax.clabel(
        CS,
        colors=['black'],
        manual=False,
        inline=True,
        fmt=' {:.0f} '.format
    )
    
    ax.set_extent([-3,3,-2,3])
    ax.gridlines(draw_labels=True)
    plt.show()
    

    列出轮廓标签文本:-

    CS.labelTexts
    

    输出:

    [Text(1.003030188944607, 0.7749999999999897, ' -30 '),
     Text(1.4249999999999843, 1.7059169688922102, ' -20 '),
     Text(0.30880609807150927, 0.9499999999999895, ' -10 '),
     Text(0.6000000000000081, 0.3999999999999915, ' 0 '),
     Text(-0.7000000000000091, -0.9440944811557408, ' 10 '),
     Text(-0.12500000000001066, -0.8102372655970758, ' 20 '),
     Text(-0.050000000000010925, 0.24709487906649752, ' 30 ')]
    

    打印每个标签的位置和文本:-

    for txobj in CS.labelTexts:
        pos = txobj.get_position()
        txt = txobj.get_text()
        print(pos, txt)
    

    输出:

    (1.003030188944607, 0.7749999999999897)  -30 
    (1.4249999999999843, 1.7059169688922102)  -20 
    (0.30880609807150927, 0.9499999999999895)  -10 
    (0.6000000000000081, 0.3999999999999915)  0 
    (-0.7000000000000091, -0.9440944811557408)  10 
    (-0.12500000000001066, -0.8102372655970758)  20 
    (-0.050000000000010925, 0.24709487906649752)  30 
    

    如果要对每个标签进行操作,则经常使用.set_text().set_position() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2017-03-11
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多