How to add a grid line at a specific location in matplotlib plot?

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()

Custom tick locations

Leave a Comment