I want to plot major grid lines of y-axis (horizontal grid lines) but I don't want to plot the vertical major grid lines (of x-axis). Instead I want to plot vertical minor grid lines.
How can I do this?
The ax.grid(which='major', linewidth=0) code hides both vertical and horizontal major grid lines...
Thank you!
The gridline properties can be set independently by ax.xaxis.grid() and ax.yaxis.grid().
In order to activate minor grid lines, you need to first specify a locator for them.
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
fig, ax = plt.subplots(figsize=(5,3))
ax.yaxis.grid(which="major", color='r', linestyle='-', linewidth=2)
ml = MultipleLocator(0.02)
ax.xaxis.set_minor_locator(ml)
ax.xaxis.grid(which="minor", color='k', linestyle='-.', linewidth=0.7)
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With