Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How do I add grid at a specific location on the y axis in a matplotlib plot?

like image 874
UNagaswamy Avatar asked Jan 30 '13 16:01

UNagaswamy


People also ask

What does PLT grid () do in Python?

grid() Function. The grid() function in pyplot module of matplotlib library is used to configure the grid lines.


2 Answers

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

like image 138
Paul H Avatar answered Sep 18 '22 19:09

Paul H


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

like image 27
tacaswell Avatar answered Sep 21 '22 19:09

tacaswell