【发布时间】: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()。
【问题讨论】:
-
相关的 matplotlib 问题:stackoverflow.com/questions/19418901/… 有用但不相关的cartopy 轮廓问题:stackoverflow.com/questions/61032307/…
标签: python matplotlib coordinates contour cartopy