Skip to content

Instantly share code, notes, and snippets.

@bstellato
Created May 15, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstellato/beded1f3adbd49db499e7138d33f7a9c to your computer and use it in GitHub Desktop.
Save bstellato/beded1f3adbd49db499e7138d33f7a9c to your computer and use it in GitHub Desktop.
Matplotlib python helper function to generate latex plots
import matplotlib as mpl
mpl.use('pgf') # Export pgf figures
import matplotlib.pylab as plt
# Text width in pt
# -> Get this from LaTeX using \the\textwidth
text_width = 469.75
def figsize(scale):
fig_width_pt = text_width
inches_per_pt = 1.0 / 72.27 # Convert pt to inch
# Aesthetic ratio (you could change this)
golden_mean = (5.0 ** 0.5 - 1.0) / 2.0
fig_width = fig_width_pt * inches_per_pt * scale # width in inches
fig_height = fig_width * golden_mean # height in inches
# fig_height = fig_width # height in inches
fig_size = [fig_width, fig_height]
return fig_size
# Paper stylesheet from: https://gist.github.com/bstellato/e24405efcc532eeda445ea3ab43922f1
plt.style.use(['paper'])
def create_figure(fig_size):
if fig_size is not None:
fig = plt.figure(figsize=figsize(fig_size))
else:
fig = plt.figure()
ax = fig.add_subplot(111)
return ax
# Example plot creation
# import numpy as np
# x = np.linspace(0, 10., 100)
# y = np.sin(x)
# ax = create_figure(0.9)
# plt.plot(x, y, label=r'$\sin(x)$')
# plt.xlabel(r"$x$")
# plt.ylabel(r"$\sin(x)$")
# plt.legend(loc='best')
# plt.tight_layout()
# plt.savefig('test_paper.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment