How do I add grid at a specific location on the y axis in a matplotlib plot?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes. It's very simple. Use the set_[x|y]ticks
methods of axes
object and toggle the grid as normal:
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()
回答2:
If you only want to put in a line or two you can use
ax.axhline(y, linestyle='--', color='k') # horizontal lines
ax.axvline(x, linestyle='--', color='k') # vertical lines
with line style and color (or all the rest of line/artist properties) set to what ever you want