-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplotutil.py
More file actions
executable file
·92 lines (80 loc) · 2.65 KB
/
plotutil.py
File metadata and controls
executable file
·92 lines (80 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Andrew Till
Fall 2012
Handy plotting functions for Python
"""
#stdlib
import math
#TPL: Numpy and Matplotlib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
#Return color option
def get_simple_colors(it_cur, it_tot=0):
if it_tot == 0:
it_tot = it_cur
col = ['b', 'r', 'g', 'c', 'm', 'k', 'y']
if it_tot < len(col):
return col[it_cur]
else:
return str(float(it_cur)/it_tot)
#Return color from a colormap (cm)
def get_colors(size, colorMap='Paired', offsetLower=0, offsetUpper=0):
colorLocs = np.linspace(0+offsetLower, 1-offsetUpper, size)
cm = getattr(mpl.cm, colorMap)
return cm(colorLocs)
#Return linestyle option
def get_linestyle(it_cur):
lin = ['-','--','-.',':']
return lin[it_cur % len(lin)]
#Return marker option
def get_marker(it_cur):
mark = ['D', 's', '^', 'v', 'd', 'h', '*', 'o', 'p', 'H', '<', '>', r'$\clubsuit$', r'$\spadesuit$', '8', '1', '3', '2', '4', '+', 'x', ',']
return mark[it_cur % len(mark)]
#Get best-fit line of form y = a*x^order
def get_order_line(xs, ys, order):
xs = np.array(xs, dtype=np.float)
ys = np.array(ys, dtype=np.float)
xsOrder = np.power(xs, order)
coeff = np.dot(ys, xsOrder) / np.dot(xsOrder, xsOrder)
return coeff * xsOrder
# Get least-squares fit of form y = a*x^b
def get_fit_log(xs, ys):
xs = np.array(xs, dtype=np.float)
ys = np.array(ys, dtype=np.float)
mask = np.logical_and(xs > 0, ys > 0)
xsLog = np.log(xs[mask])
ysLog = np.log(ys[mask])
b, aLog = np.polyfit(xsLog, ysLog, 1)
a = np.exp(aLog)
return a * np.power(xs, b)
#Get best-fit line of form y = a*x^order, but use log L2 fit
def get_order_line_log(xs, ys, order):
xs = np.array(xs, dtype=np.float)
ys = np.array(ys, dtype=np.float)
mask = np.logical_and(xs > 0, ys > 0)
xsLog = np.log(xs[mask])
ysLog = np.log(ys[mask])
coeffLog = np.mean(ysLog - order * xsLog)
coeff = np.exp(coeffLog)
return coeff * np.power(xs, order)
#Plot stairs
def get_stairs(x, y):
"""
Given x and y, return points necessary for a step plot
x is of size n+1 and y is of size n.
:Parameters:
- `x`: [x_0,...,x_n] list of x coordinates as numpy array
- `y`: [y_0,...,y_{n-1}] list of y coordinates as numpy array
:rtype: tuple of lists
:return: (xnew,ynew) where
xnew = (x_0,x_1,x_2,...,x_n,x_n)
ynew = (y_0,y_0,y_1,...,y_{n-1})
:requries: numpy
:author: based on pytrix.py of Google Code
"""
nobs = len(x)
assert len(x) == (len(y)+1), "x must be one larger than y in length."
xnew = np.repeat(x, 2)[1: -1]
ynew = np.repeat(y, 2)
return xnew, ynew