Matplotlib:类似于clabel()的投影图网格线上的标签

时间:2017-09-13 23:49:05

标签: python matplotlib plot astropy

我正在使用Astropy WCS软件包对各种球形投影图进行大量工作,并且遇到了一些关于网格线的挫败感。由于网格线并不总是与图像边界框相交或多个相交在同一位置,因此它们可以不标记或使其标签难以辨认。我希望能够在每一行中插入网格线标签,非常类似于应用于等高线图的matplotlib.pyplot.clabel()函数,如此matplotlib example。因为我是新用户,我无法嵌入图像;我道歉。

我知道我可以使用text(),figtext()或annotate()来放置标签,但是由于clabel()有效,我认为功能已经存在,即使它还没有应用于网格线。除了投影图之外,是否有人知道类似于clabel()的在线网格线标签可以应用于普通矩形图上的网格线?

1 个答案:

答案 0 :(得分:0)

要注释网格线,您可以使用主刻度线的位置(因为它们是创建网格线的位置)。

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10)
y = np.sin(x)*10

fig, ax = plt.subplots()
ax.plot(x,y)
ax.grid()

for xi in ax.xaxis.get_majorticklocs():
    ax.text(xi,0.8, "{:.2f}".format(xi), clip_on=True, ha="center",
            transform=ax.get_xaxis_transform(), rotation=90,
            bbox={'facecolor':'w', 'pad':1, "edgecolor":"None"})
for yi in ax.yaxis.get_majorticklocs():
    ax.text(0.86,yi, "{:.2f}".format(yi), clip_on=True, va="center",
            transform=ax.get_yaxis_transform(), 
            bbox={'facecolor':'w', 'pad':1, "edgecolor":"None"})

plt.show()

enter image description here

相关问题