python - 为数据点添加注释

标签 python matplotlib

我想生成一个方位角-仰角图来描述物体在一定日期范围内的运动。在下面的示例图中,我使用标准 matplotlib 调用绘制了数据的极坐标图。但是,我想以编程方式向某些点添加刻度线和文本标签,以注释与数据关联的日期。这些是使用 Gimp 添加到下图中的。

理想情况下,我希望绘制的刻度线垂直于绘制它们的曲线位置,但如果我可以通过现有的 matplotlib 函数调用让它们垂直或水平,我可以接受它们。但是,我一直无法找到一组执行此操作的函数。有这样的东西还是我需要自己写?

AZ-EL plot with annotations

from matplotlib.pyplot import *

azel = [(0.13464431952125472,294.0475121469728,41761.31282856121),
  (1.0847050101323694, 294.07215546949817, 41762.31339111264),
  (2.0568066678342625, 294.08166309282285, 41763.3139526239),
  (3.0241776724839813, 294.07688196040385, 41764.3145128838),
  (3.9693016837899107, 294.05876829872494, 41765.315071676574),
  (4.880729810599228, 294.0283602965401, 41766.315628782446),
  (5.750685455655052, 293.98677810487305, 41767.31618397785),
  (6.573500415719916, 293.93516919550444, 41768.316737037436),
  (7.344961744736395, 293.8748176439982, 41769.31728773474),
  (8.061838763227069, 293.80692556364824, 41770.317835842376),
  (8.7216239379929, 293.732913633802, 41771.31838113272),
  (9.322354443421153, 293.65412057153674, 41772.31892337514),
  (9.862485802985763, 293.57204901846984, 41773.319462333326),
  (10.340798827919878, 293.48820161621876, 41774.31999776034),
  (10.756318508719481, 293.40413564791425, 41775.32052939467),
  (11.108256812309081, 293.3215176797138, 41776.32105695697),
  (11.395961455622944, 293.2420689192882, 41777.321580147836),
  (11.618873216922772, 293.16759253657835, 41778.32209864656),
  (11.776501176361972, 293.09997366379525, 41779.322612110234),
  (11.868395395228971, 293.04117939541976, 41780.32312017416),
  (11.894134963116164, 292.9932041466896, 41781.32362245329),
  (11.853317752636167, 292.95820625738247, 41782.324118542434),
  (11.745559565648168, 292.9383167465194, 41783.32460801967),
  (11.57050010967345, 292.9358305576615, 41784.325090448285),
  (11.327811535631849, 292.95306995512664, 41785.32556538106),
  (11.01721978218292, 292.9923298824759, 41786.32603236289),
  (10.638530188935318, 293.0560692078104, 41787.32649093496),
  (10.191665062487234, 293.1466921577181, 41788.32694063783),
  (9.676711487750586, 293.26663027954356, 41789.32738101277),
  (9.093982799653546, 293.4182877998745, 41790.32781160377),
  (8.444096276542357, 293.6040416245422, 41791.328231959254),
  (7.728075593018872, 293.82621401786434, 41792.328641632426),
  (6.947485289289849, 294.08704528188883, 41793.32904018317),
  (6.104631834860636, 294.3886391148799, 41794.329427178716),
  (5.202865010632301, 294.7329352905619, 41795.3298021964),
  (4.247126458469349, 295.12173697887573, 41796.3301648264),
  (3.2448043086196177, 295.55651950068204, 41797.3305146729),
  (2.2076748744292662, 296.0385122900315, 41798.3308513581),
  (1.1552935211005704, 296.56867157340787, 41799.33117452266),
  (0.12014145335536401, 297.1474344829181, 41800.33148382535)]

fig = figure()
ax = fig.add_subplot(1,1,1,projection='polar')
ax.plot([az for el,az,_ in azel], [el for el,az,_ in azel])
ylim([0,25])
show()

最佳答案

我假设您知道要注释哪些刻度线。棘手的部分是判断文本应该位于要注释的点的左侧还是右侧。如果您对该部分的解决方案感兴趣,请告诉我,我认为这不是这里的重点。在 show() 之前插入以下内容

ax.plot(azel[20][1],azel[20][0], marker=(2,0,0), mew=3, markersize=7)
ax.annotate('Nav 20', xy=(azel[20][1], azel[20][0]), xytext=(azel[20][1], azel[20][0]+2), horizontalalignment='left', verticalalignment='top')
ax.plot(azel[10][1],azel[10][0], marker=(2,0,20), mew=3, markersize=7)
ax.annotate('Nav 10', xy=(azel[10][1], azel[10][0]), xytext=(azel[20][1], azel[20][0]+2), horizontalalignment='left', verticalalignment='top')

标记的参数是(多边形边数,多边形为 0,以度为单位的角度),我选择边数为 2 来制作刻度线。 mew 控制标记壁的厚度,markersize 控制多边形的大小(在本例中为长度)。

关于python - 为数据点添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20571836/

相关文章:

python - 获取 pandas 中分类变量的映射

pip install --user 时 Python console_scripts 不起作用

python - 如何将 subprocess.call 的结果打印到 python 脚本中的文件

python - 在python中将点转换为png

python - 带有居中标签的堆积条形图

python-3.x - 在python中命名轴

Matplotlib:不要在图例中显示错误栏

python - scc 风格的 QPushbutton

python - 批量保存数据到图像: need speedup

python - 来自 x、y 值的 Matplotlib 直方图,日期时间月份作为 bin