如何在matplotlib阴谋的特定位置添加网格线?(How to add a grid line

2019-07-19 00:58发布

如何在上一个matplotlib阴谋y轴在特定位置添加格?

Answer 1:

是。 这是非常简单的。 使用set_[x|y]ticks的方法axes对象,并切换栅格为正常:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_yticks([0.2, 0.6, 0.8], minor=False)
ax.set_yticks([0.3, 0.55, 0.7], minor=True)
ax.yaxis.grid(True, which='major')
ax.yaxis.grid(True, which='minor')
plt.show()



Answer 2:

如果你只希望把一两行,你可以使用

ax.axhline(y, linestyle='--', color='k') # horizontal lines
ax.axvline(x, linestyle='--', color='k') # vertical lines

设置为你想要什么都行样式和颜色(或线/艺术家属性的所有其余部分)



文章来源: How to add a grid line at a specific location in matplotlib plot?