# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: conda_python3
#     language: python
#     name: conda_python3
# ---

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

# <br>
# <br>

# # Generating some sample data

# [[back to top](#Sections)]

# + jupyter={"outputs_hidden": false}
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()
# -

# <br>
# <br>

# # Default grid

# [[back to top](#Sections)]

# + jupyter={"outputs_hidden": false}
plt.hist(data, bins=bins, alpha=0.5)
plt.grid()

plt.show()
# -

# Or alternatively:

# + jupyter={"outputs_hidden": false}
plt.hist(data, bins=bins, alpha=0.5)
ax = plt.gca()
ax.grid(True)

plt.show()
# -

# <br>
# <br>

# # Vertical and horizontal grids

# [[back to top](#Sections)]

# ## Vertical grid

# [[back to top](#Sections)]

# + jupyter={"outputs_hidden": false}
plt.hist(data, bins=bins, alpha=0.5)
ax = plt.gca()
ax.xaxis.grid(True)

plt.show()
# -

# ## Horizontal grid

# [[back to top](#Sections)]

# + jupyter={"outputs_hidden": false}
plt.hist(data, bins=bins, alpha=0.5)
ax = plt.gca()
ax.yaxis.grid(True)

plt.show()
# -

# <br>
# <br>

# # Controlling the gridline style

# [[back to top](#Sections)]

# ## Changing the tick frequency

# [[back to top](#Sections)]

# + jupyter={"outputs_hidden": false}
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)]

# + jupyter={"outputs_hidden": false}
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()

# + jupyter={"outputs_hidden": true}

# -



StarcoderdataJupyterScriptsDedupFiltered