Skip to content

Instantly share code, notes, and snippets.

@joastbg
Last active February 23, 2016 13:35
Show Gist options
  • Select an option

  • Save joastbg/d23e35b832b5595958f7 to your computer and use it in GitHub Desktop.

Select an option

Save joastbg/d23e35b832b5595958f7 to your computer and use it in GitHub Desktop.
Settings for creating nice plots
import numpy as np
from scipy import fftpack
import matplotlib.pyplot as plt
from matplotlib import rcParams
from matplotlib import use
#import matplotlib as mpl
use('pgf')
def figsize(scale):
fig_width_pt = 469.755 # Get this from LaTeX using \the\textwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5.0)-1.0)/2.0 # Aesthetic ratio (you could change this)
fig_width = fig_width_pt*inches_per_pt*scale # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
return fig_size
#rcParams['text.usetex'] = True
#rcParams['font.size'] = 11.0
#rcParams['font.family'] = 'lmodern'
#rcParams['text.latex.unicode'] = True
#rcParams['text.latex.preamble'] = [r'\boldmath']
pgf_with_latex = { # setup matplotlib to use latex for output
#"backend": 'ps',
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 11.0, # LaTeX default is 10pt font.
"text.fontsize": 11.0,
"legend.fontsize": 11.0, # Make the legend/label fonts a little smaller
"xtick.labelsize": 9.0,
"ytick.labelsize": 9.0,
"figure.figsize": figsize(0.9), # default fig size of 0.9 textwidth
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
r"\usepackage[T1]{fontenc}", # plots will be generated using this preamble
]
}
rcParams.update(pgf_with_latex)
width = 2.0
freq = 0.5
freq2 = 0.025
t = np.linspace(-10, 10, 251) # linearly space time array
#g = np.exp(-np.abs(t)/width) * np.sin(2.0*np.pi*freq*t) * np.exp(2.5*np.pi*freq2*t)
g = np.sin(2.0*np.pi*freq*t)
dt = t[1]-t[0] # increment between times in time array
G = fftpack.fft(g) # FFT of g
f = fftpack.fftfreq(g.size, d=dt) # frequenies f[i] of g[i]
f = fftpack.fftshift(f) # shift frequencies from min to max
G = fftpack.fftshift(G) # shift G order to coorespond to f
#fig, ax = plt.subplots(nrows=1, ncols=1)
fig = plt.figure(1, figsize=figsize(width), frameon=True)
ax1 = fig.add_subplot(211)
ax1.plot(t, g, linewidth=2)
ax1.set_xlabel(r'$t$')
ax1.set_ylabel(r'$g(t)$')
ax1.grid(True)
ax2 = fig.add_subplot(212)
ax2.plot(f, np.real(G), color='dodgerblue', label='real part', linewidth=2)
ax2.plot(f, np.imag(G), color='coral', label='imaginary part', linewidth=2)
ax2.legend()
ax2.set_xlabel(r'$f$')
ax2.set_ylabel(r'$G(f)$')
ax2.grid(True)
def savefig(filename):
plt.savefig('{}.pgf'.format(filename), dpi=300, bbox_inches='tight')
plt.savefig('{}.pdf'.format(filename), dpi=300, bbox_inches='tight')
plt.savefig('{}.png'.format(filename), dpi=150, bbox_inches='tight')
#fig.savefig('fft.png', dpi=100, bbox_inches='tight')
savefig('fft')
plt.show()
"""
Settings for creating nice plots
"""
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True
rcParams['font.size'] = 11.0
rcParams['font.family'] = 'lmodern'
rcParams['text.latex.unicode'] = True
fig, ax = plt.subplots(nrows=1, ncols=1)
fig.set_size_inches(4.9,3.5)
ax.plot([0,1,2], [10,20,3], 'k', linewidth=3)
ax.margins(y=.05)
ax.margins(x=.05)
[i.set_linewidth(1.0) for i in ax.spines.itervalues()]
plt.xlabel(r'$\alpha$', fontsize=16)
plt.ylabel(r'$\beta$', fontsize=16)
plt.grid(True)
fig.savefig('to.png', dpi=100, bbox_inches='tight')
plt.close(fig)
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
def figsize(scale):
fig_width_pt = 469.755 # Get this from LaTeX using \the\textwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5.0)-1.0)/2.0 # Aesthetic ratio (you could change this)
fig_width = fig_width_pt*inches_per_pt*scale # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
return fig_size
pgf_with_latex = { # setup matplotlib to use latex for output
#"backend": 'ps',
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 11.0, # LaTeX default is 10pt font.
"text.fontsize": 11.0,
"legend.fontsize": 11.0, # Make the legend/label fonts a little smaller
"xtick.labelsize": 9.0,
"ytick.labelsize": 9.0,
"figure.figsize": figsize(0.9), # default fig size of 0.9 textwidth
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
r"\usepackage[T1]{fontenc}", # plots will be generated using this preamble
]
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as plt
# I make my own newfig and savefig functions
def newfig(width):
plt.clf()
fig = plt.figure(figsize=figsize(width))
ax = fig.add_subplot(111)
return fig, ax
def savefig(filename):
plt.savefig('{}.pgf'.format(filename), dpi=300, bbox_inches='tight')
plt.savefig('{}.pdf'.format(filename), dpi=300, bbox_inches='tight')
plt.savefig('{}.png'.format(filename), dpi=150, bbox_inches='tight')
# Simple plot
fig, ax = newfig(0.6)
def ema(y, a):
s = []
s.append(y[0])
for t in range(1, len(y)):
s.append(a * y[t] + (1-a) * s[t-1])
return np.array(s)
y = [0]*200
y.extend([20]*(1000-len(y)))
s = ema(y, 0.01)
ax.margins(y=.05)
ax.margins(x=.05)
ax.legend()
ax.plot(s, linewidth=2)
plt.xlabel(r'One $\alpha$')
plt.ylabel(r'Two $\beta$')
plt.grid(True)
savefig('ema')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment