In [1]:
%load_ext watermark

In [2]:
%watermark -u -v -d -p matplotlib,numpy


Last updated: 31/03/2015 

CPython 3.4.3
IPython 3.0.0

matplotlib 1.4.3
numpy 1.9.1

[More info](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `%watermark` extension


In [3]:
%matplotlib inline



Matplotlib Formatting II: gridlines

Sections



Generating some sample data


In [4]:
import numpy as np
import random
from matplotlib import pyplot as plt

data = np.random.normal(0, 20, 1000) 

# fixed bin size
bins = np.arange(-100, 100, 5) # fixed bin size

plt.xlim([min(data)-5, max(data)+5])

plt.hist(data, bins=bins, alpha=0.5)
plt.show()




Default grid


In [5]:
plt.hist(data, bins=bins, alpha=0.5)
plt.grid()

plt.show()


Or alternatively:


In [6]:
plt.hist(data, bins=bins, alpha=0.5)
ax = plt.gca()
ax.grid(True)

plt.show()




Vertical and horizontal grids

Vertical grid


In [7]:
plt.hist(data, bins=bins, alpha=0.5)
ax = plt.gca()
ax.xaxis.grid(True)

plt.show()


Horizontal grid


In [8]:
plt.hist(data, bins=bins, alpha=0.5)
ax = plt.gca()
ax.yaxis.grid(True)

plt.show()




Controlling the gridline style

Changing the tick frequency


In [9]:
import numpy as np

# major ticks every 10                                     
major_ticks = np.arange(-100, 101, 10)                                              

ax = plt.gca()
ax.yaxis.grid()

ax.set_yticks(major_ticks)                                                       
                                    
plt.hist(data, bins=bins, alpha=0.5)

plt.show()


Changing the tick color and linestyle


In [10]:
from matplotlib import rcParams

rcParams['grid.linestyle'] = '-'
rcParams['grid.color'] = 'blue'
rcParams['grid.linewidth'] = 0.2

plt.grid()
plt.hist(data, bins=bins, alpha=0.5)

plt.show()



In [ ]: