Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2c2b93

Browse files
Update D2_of_3Day_DoneWithPython.md
1 parent 4361a7d commit d2c2b93

File tree

1 file changed

+272
-1
lines changed

1 file changed

+272
-1
lines changed

‎D2_of_3Day_DoneWithPython.md‎

Lines changed: 272 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
```
1515
## 作者:Yupeng Jiang
1616
* 伦敦大学学院 数学系 (英国顶尖大学,2018 QS世界大学排名中位列世界第7名,英国第3名)
17-
* email:yupeng.jiang.13@ucl.ac.uk
17+
* email:yupeng.jiang.13atcl.ac.uk
1818
* 2016年6月5日
1919
* [课件来自] https://zhuanlan.zhihu.com/p/21332075
2020
## 翻译:Murphy Wan
@@ -949,9 +949,280 @@ plt.plot(x, x-1, ’k-’) # continue plot
949949
plt.plot(x, np.zeros_like(x), ’k-’)
950950

951951
```
952+
* 注意:您的x轴在plt.plot函数中应与y轴的尺寸相同。 (Note: Your x-axis should be the same dimension to y-axis in plt.plot function.)
952953

954+
![the simplest plot](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img012_the_simplest_plot.jpg)
955+
956+
957+
958+
959+
960+
```python
961+
962+
```
963+
964+
965+
966+
## 多个制图图例标签和标题 (Multiple plotting, legends, labels and title)
953967

954968
```python
969+
970+
import numpy as np
971+
import matplotlib.pyplot as plt
972+
973+
x = np.linspace(0, 10, 201)
974+
plt.figure(figsize = (4, 4))
975+
for n in range(2, 5):
976+
y = x ** (1 / n)
977+
plt.plot(x, y, label=’x^(1/’ \
978+
+ str(n) + ’)’)
979+
plt.legend(loc = ’best’)
980+
plt.xlabel(’X axis’)
981+
plt.ylabel(’Y axis’)
982+
plt.xlim(-2, 10)
983+
plt.title(’Multi-plot e.g. ’, fontsize = 18)
984+
955985
```
956986

987+
![multiple-plotting](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img013_multiple_plotting.jpg)
988+
989+
* Forinformations:See
990+
991+
```python
992+
993+
help(plt.plot)
994+
995+
```
996+
997+
998+
```python
999+
```
1000+
1001+
1002+
1003+
## 绘制子图 (Subplots,即在 )
1004+
1005+
```python
1006+
1007+
import numpy as np’
1008+
import matplotlib.pyplot as plt
1009+
1010+
def pffcall(S, K):
1011+
return np.maximum(S - K, 0.0)
1012+
def pffput(S, K):
1013+
return np.maximum(K - S, 0.0)
1014+
1015+
S = np.linspace(50, 151, 100)
1016+
fig = plt.figure(figsize=(12, 6))
1017+
1018+
sub1 = fig.add_subplot(121) # col, row, num
1019+
sub1.set_title('Call', fontsize = 18)
1020+
plt.plot(S, pffcall(S, 100), 'r-', lw = 4)
1021+
plt.plot(S, np.zeros_like(S), 'black',lw = 1)
1022+
sub1.grid(True)
1023+
sub1.set_xlim([60, 120])
1024+
sub1.set_ylim([-10, 40])
1025+
1026+
sub2 = fig.add_subplot(122)
1027+
sub2.set_title('Put', fontsize = 18)
1028+
plt.plot(S, pffput(S, 100), 'r-', lw = 4)
1029+
plt.plot(S, np.zeros_like(S), 'black',lw = 1)
1030+
sub2.grid(True)
1031+
sub2.set_xlim([60, 120])
1032+
sub2.set_ylim([-10, 40])
1033+
1034+
```
1035+
1036+
![multiple_plottion](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img014_multiple_plotting.jpg)
1037+
1038+
* Figure: 一个子图的例子
1039+
(注释:这里,可以把Figure,即fig理解为一张大画布,你把它分成了两个子区域(sub1和sub2),然后在每个子区域各画了一幅图。)
1040+
1041+
1042+
1043+
```python
1044+
```
1045+
1046+
1047+
1048+
## 在绘制的图上添加文本和注释 (Adding texts to plots)
1049+
1050+
```python
1051+
1052+
import numpy as np
1053+
from scipy.stats import norm
1054+
import matplotlib.pyplot as plt
1055+
1056+
def call(S, K=100, T=0.5, vol=0.6, r=0.05):
1057+
d1 = (np.log(S/K) + (r + 0.5 * vol**2) \
1058+
*T) / np.sqrt(T) / vol
1059+
d2 = (np.log(S/K) + (r - 0.5 * vol**2) \
1060+
*T) / np.sqrt(T) / vol
1061+
return S * norm.cdf(d1) - K * \
1062+
np.exp(-r * T) * norm.cdf(d2)
1063+
1064+
def delta(S, K=100, T=0.5, vol=0.6, r=0.05):
1065+
d1 = (np.log(S/K) + (r + 0.5 * vol**2)\
1066+
*T) / np.sqrt(T) / vol
1067+
return norm.cdf(d1)
1068+
1069+
```
1070+
1071+
1072+
## (Code continues:)
1073+
1074+
```python
1075+
1076+
S = np.linspace(40, 161, 100)
1077+
fig = plt.figure(figsize=(7, 6))
1078+
ax = fig.add_subplot(111)
1079+
plt.plot(S,(call(S)-call(100)),’r’,lw=1)
1080+
plt.plot(100, 0, ’ro’, lw=1)
1081+
plt.plot(S,np.zeros_like(S), ’black’, lw = 1)
1082+
plt.plot(S,call(S)-delta(100)*S- \
1083+
(call(100)-delta(100)*100), ’y’, lw = 1)
1084+
1085+
```
1086+
1087+
1088+
## (Code continues:)
1089+
1090+
1091+
```python
1092+
1093+
ax.annotate(’$\Delta$ hedge’, xy=(100, 0), \
1094+
xytext=(110, -10),arrowprops= \
1095+
dict(headwidth =3,width = 0.5, \
1096+
facecolor=’black’, shrink=0.05))
1097+
ax.annotate(’Original call’, xy= \
1098+
(120,call(120)-call(100)),xytext\
1099+
=(130,call(120)-call(100)),\
1100+
arrowprops=dict(headwidth =10,\
1101+
width = 3, facecolor=’cyan’, \
1102+
shrink=0.05))
1103+
plt.grid(True)
1104+
plt.xlim(40, 160)
1105+
plt.xlabel(’Stock price’, fontsize = 18)
1106+
plt.ylabel(’Profits’, fontsize = 18)
1107+
1108+
```
1109+
1110+
![annotation](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img015_annotation.jpg)
1111+
1112+
1113+
1114+
1115+
```python
1116+
```
1117+
1118+
## 3D plot of a function with 2 variables
1119+
1120+
```python
9571121

1122+
import numpy as np
1123+
import matplotlib.pyplot as plt
1124+
from matplotlib import cm
1125+
from mpl_toolkits.mplot3d import Axes3D
1126+
1127+
x, y = np.mgrid[-5:5:100j, -5:5:100j]
1128+
z = x**2 + y**2
1129+
fig = plt.figure(figsize=(8, 6))
1130+
ax = plt.axes(projection='3d')
1131+
surf = ax.plot_surface(x, y, z, rstride=1,\
1132+
cmap=cm.coolwarm, cstride=1, \
1133+
linewidth=0)
1134+
fig.colorbar(surf, shrink=0.5, aspect=5)
1135+
plt.title('3D plot of $z = x^2 + y^2$')
1136+
1137+
```
1138+
1139+
![3D-plot](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img016_3D_plot.jpg)
1140+
1141+
1142+
1143+
```python
1144+
```
1145+
1146+
1147+
## 实验3:atplotlib (Lab 3: Matplotlib)
1148+
1149+
* 用蓝线绘制以下函数 (Plot the following function with blue line)
1150+
1151+
![1](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img017_1.jpg)
1152+
1153+
- 然后用红点标记坐标(1,2) (Then mark the coordinate (1, 2) with a red point.)
1154+
1155+
* 使用np.linspace()使t ∈ [0,2π]。 然后给 (Use np.linspace0 to make t ∈ [0,2π]. Then give)
1156+
1157+
![2](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img017_2.jpg)
1158+
1159+
- 针对X绘Y。 在这个情节中添加一个称为"Heart"的标题。 (Plot y against x. Add a title to this plot which is called "Heart" .)
1160+
1161+
* 针对x∈[-10,10], y∈[-10,10], 绘制3D函数 (Plot the 3D function for x∈[-10,10], y∈[-10,10])
1162+
1163+
![3](https://github.com/MurphyWan/Python-first-Practice/blob/master/images/3days_img017_3.jpg)
1164+
1165+
1166+
1167+
1168+
```python
1169+
```
1170+
1171+
1172+
1173+
1174+
1175+
1176+
1177+
1178+
```python
1179+
```
1180+
1181+
1182+
1183+
1184+
1185+
1186+
1187+
```python
1188+
```
1189+
1190+
1191+
1192+
1193+
1194+
1195+
1196+
1197+
1198+
```python
1199+
```
1200+
1201+
1202+
1203+
1204+
1205+
1206+
1207+
1208+
1209+
```python
1210+
```
1211+
1212+
1213+
1214+
1215+
1216+
1217+
1218+
1219+
```python
1220+
```
1221+
1222+
1223+
1224+
1225+
1226+
1227+
```python
1228+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /