Skip to content

Instantly share code, notes, and snippets.

@corbett
Created October 31, 2017 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save corbett/be8b9da03a86ba62e01e74de658abf90 to your computer and use it in GitHub Desktop.
Save corbett/be8b9da03a86ba62e01e74de658abf90 to your computer and use it in GitHub Desktop.
Create beautiful square figures with big labels and the correct number of ticks
def create_figure(size=3.6,nxticks=6):
import matplotlib
from matplotlib.ticker import MaxNLocator
figure=matplotlib.pyplot.figure(figsize=(size,size))
ax = figure.add_subplot(1, 1, 1, position = [0.2, 0.15, 0.75, 0.75])
ax.xaxis.set_major_locator(MaxNLocator(nxticks))
return ax
def format_axes(ax,xf='%d',yf='%d',nxticks=6,nyticks=6,labelsize=10):
import pylab
from matplotlib.ticker import FormatStrFormatter, MaxNLocator
xFormatter = FormatStrFormatter(xf)
yFormatter = FormatStrFormatter(yf)
if xf:
ax.xaxis.set_major_formatter(xFormatter)
ax.xaxis.set_major_locator(MaxNLocator(nxticks))
ax.tick_params(axis='x', which='major', labelsize=labelsize)
if yf:
ax.yaxis.set_major_formatter(yFormatter)
ax.yaxis.set_major_locator(MaxNLocator(nyticks))
ax.tick_params(axis='y', which='major', labelsize=labelsize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment