#!/usr/bin/env python # coding: utf-8 # [Sebastian Raschka](http://www.sebastianraschka.com) # # [back](https://github.com/rasbt/matplotlib-gallery) to the `matplotlib-gallery` at [https://github.com/rasbt/matplotlib-gallery](https://github.com/rasbt/matplotlib-gallery) # In[1]: get_ipython().run_line_magic('load_ext', 'watermark') # In[2]: get_ipython().run_line_magic('watermark', '-u -v -d -p matplotlib,numpy') # [More info](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `%watermark` extension # In[3]: get_ipython().run_line_magic('matplotlib', 'inline') #
#
# # Matplotlib Formatting II: gridlines # # Sections # - [Generating-some-sample-data](#Generating-some-sample-data) # - [Default grid](#Default-grid) # - [Vertical and horizontal grids](#Vertical-and-horizontal-grids) # - [Vertical grid](#Vertical-grid) # - [Horizontal grid](#Horizontal-grid) # - [Controlling the gridline style](#Controlling-the-gridline-style) # - [Changing the tick frequency](#Changing-the-tick-frequency) # - [Changing the tick color and linestyle](#Changing-the-tick-color-and-linestyle) #
#
# # Generating some sample data # [[back to top](#Sections)] # 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 # [[back to top](#Sections)] # 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 # [[back to top](#Sections)] # ## Vertical grid # [[back to top](#Sections)] # In[7]: plt.hist(data, bins=bins, alpha=0.5) ax = plt.gca() ax.xaxis.grid(True) plt.show() # ## Horizontal grid # [[back to top](#Sections)] # In[8]: plt.hist(data, bins=bins, alpha=0.5) ax = plt.gca() ax.yaxis.grid(True) plt.show() #
#
# # Controlling the gridline style # [[back to top](#Sections)] # ## Changing the tick frequency # [[back to top](#Sections)] # 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 # [[back to top](#Sections)] # 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[ ]: