Matplotlib: Labels on projection plot grid lines 类似于 clabel()
Matplotlib: Labels on projection plot grid lines similar to clabel()
我正在使用 Astropy WCS 包处理各种球面投影图,并且 运行 对网格线感到有些沮丧。由于网格线并不总是与图像边界框相交或在同一位置多次相交,因此它们可能没有标签或标签变得难以辨认。我希望能够在每一行中插入网格线标签,非常类似于 matplotlib.pyplot.clabel() function applied to contour plots, as in this matplotlib example。我是新用户,无法嵌入图片;抱歉。
我知道我可以使用 text()、figtext() 或 annotate() 放置标签,但由于 clabel() 有效,我认为该功能已经存在,即使它尚未应用于网格线。除了投影绘图,有谁知道可以将类似于 clabel() 的内嵌网格线标签应用于普通矩形图上的网格线的方法?
要注释网格线,您可以使用主要刻度的位置(因为这些是创建网格线的位置)。
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()
我正在使用 Astropy WCS 包处理各种球面投影图,并且 运行 对网格线感到有些沮丧。由于网格线并不总是与图像边界框相交或在同一位置多次相交,因此它们可能没有标签或标签变得难以辨认。我希望能够在每一行中插入网格线标签,非常类似于 matplotlib.pyplot.clabel() function applied to contour plots, as in this matplotlib example。我是新用户,无法嵌入图片;抱歉。
我知道我可以使用 text()、figtext() 或 annotate() 放置标签,但由于 clabel() 有效,我认为该功能已经存在,即使它尚未应用于网格线。除了投影绘图,有谁知道可以将类似于 clabel() 的内嵌网格线标签应用于普通矩形图上的网格线的方法?
要注释网格线,您可以使用主要刻度的位置(因为这些是创建网格线的位置)。
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()