Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to independently set horizontal and vertical, major and minor grid lines of a plot?

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!

like image 270
ragesz Avatar asked Jan 04 '17 11:01

ragesz


1 Answers

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

enter image description here

like image 161
ImportanceOfBeingErnest Avatar answered Nov 08 '22 11:11

ImportanceOfBeingErnest