-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_graph.py
74 lines (53 loc) · 1.74 KB
/
plot_graph.py
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
import pylab as pl
import numpy as np
def hinge(X):
return np.maximum(1-X, 0)
def ramp(X):
return np.minimum(hinge(X), 2)
def sigmoid(X):
return 2.0 / (1 + np.exp(5*X))
def logistic(X):
return np.log2(1 + np.exp(-X))
def zeroone(X):
return map(lambda x : 0 if x>0 else 1, X)
def exp(X):
return np.exp(-X)
def plot(mode):
""" plot range """
xmin, xmax, ymin, ymax = (-2, 2, -1, 3)
fig = pl.figure()
fig.subplots_adjust(left=0.15)
ax = pl.subplot(1,1,1)
interval = 0.5
offset = 0.05
""" move ticks"""
pl.xticks(offset + np.arange(xmin, xmax+1, interval), np.arange(xmin, xmax+interval+1, interval))
pl.yticks(offset + np.arange(ymin, ymax+1, interval), np.arange(ymin, ymax+interval+1, interval))
pl.ylim([ymin,ymax])
""" axis """
pl.hlines([0], xmin, xmax, linestyles="dashed")
pl.vlines([0], ymin, ymax, linestyles="dashed")
""" label """
pl.xlabel("x", style='italic', fontsize=25)
pl.ylabel("l'(x)", style='italic', fontsize=25)
""" title """
pl.title(mode, fontdict={'size':28})
X = np.linspace(xmin, xmax, 256, endpoint=True)
""" plot funcitons """
if mode == "(b)":
pl.plot(X, exp(X), "--r", linewidth=5)
pl.plot(X, hinge(X), "-b", linewidth=5)
#pl.plot(X, logistic(X), linewidth=3, color="y")
elif mode == "(a)":
pl.plot(X, sigmoid(X), "-b", linewidth=5)
pl.plot(X, ramp(X), "--r", linewidth=5)
for i,item in enumerate(ax.get_xticklabels()):
fontsize = 20
item.set_fontsize(fontsize)
for i,item in enumerate(ax.get_yticklabels()):
fontsize = 20
item.set_fontsize(fontsize)
pl.show()
if __name__ == "__main__":
for mode in ["(a)", "(b)"]:
plot(mode)