Skip to content

Instantly share code, notes, and snippets.

@m-tmatma
Created November 6, 2021 00:10
Show Gist options
  • Save m-tmatma/cb3c8eb2d58f509ee95d67840efc91b0 to your computer and use it in GitHub Desktop.
Save m-tmatma/cb3c8eb2d58f509ee95d67840efc91b0 to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/questions/24943991/change-grid-interval-and-specify-tick-labels-in-matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import (AutoMinorLocator, MultipleLocator)
fig, ax = plt.subplots(figsize=(10, 8))
x = range(100+1)
y = range(100,200+1)
ax.plot(x, y, label="test")
# Set axis ranges; by default this will put major ticks every 25.
#ax.set_xlim(0, 200)
#ax.set_ylim(0, 200)
# Change major ticks to show every 20.
ax.xaxis.set_major_locator(MultipleLocator(20))
ax.yaxis.set_major_locator(MultipleLocator(20))
# Change minor ticks to show every 5. (20/4 = 5)
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
# Turn grid on for both major and minor ticks and style minor slightly
# differently.
ax.grid(which='major', color='#CCCCCC', linestyle='--')
ax.grid(which='minor', color='#CCCCCC', linestyle=':')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment