Matplot 第三部分

设置坐标轴:

修改坐标轴上的小标,以及显示坐标轴上的小标:

 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 
 5 if 1:
 6     plt.figure(figsize=(14,12))
 7     x = np.linspace(0,10)
 8     plt.plot(x,x**3)
 9 
10     new_ticks = np.linspace(0,10,20)
11     plt.xticks(new_ticks)
12     plt.yticks([0,500,1000],[r'$bad\ bad$',r'$normal\ \alpha\ \beta\ \gamma$',r'$good$']) # $数学格式  \ 是转移字符
13     pass
14 plt.show()
里面有关于在坐标轴上,显示特殊符号

 

 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 
 5 if 1:
 6     plt.figure(figsize=(15,12))
 7     x = np.linspace(-np.pi/2,np.pi/2)
 8     plt.plot(x,x)
 9     plt.plot(x,np.cos(x))
10 
11     ax = plt.gca()
12     ax.spines['top'].set_visible(False)
13     ax.spines['right'].set_visible(False)
14 
15     ax.spines['bottom'].set_position(('data',0))
16     ax.spines['left'].set_position(('data',0))
17 
18 
19 plt.show()
隐藏坐标轴和移动坐标轴
 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 
 5 if 1:
 6     plt.figure(figsize=(15,12))
 7     x = np.linspace(-np.pi/2,np.pi/2)
 8     plt.plot(x,x,label='line1')
 9     plt.plot(x,np.cos(x),label='line2')
10     plt.legend() #加上图例
11 
12     ax = plt.gca()
13     ax.spines['top'].set_visible(False)
14     ax.spines['right'].set_visible(False)
15 
16     ax.spines['bottom'].set_position(('data',0))
17     ax.spines['left'].set_position(('data',0))
18 
19 
20 plt.show()
加上图例
 1 import numpy as np
 2 import pandas as pd
 3 import matplotlib.pyplot as plt
 4 
 5 if 1:
 6     plt.figure(figsize=(15,12))
 7     x = np.linspace(-np.pi/2,np.pi/2)
 8     plt.plot(x,x,label='line1')
 9     plt.plot(x,np.cos(x),label='line2')
10     plt.legend() #加上图例
11 
12 
13     x0 = 0.739
14     plt.scatter(x0,x0,s=50,color='b') #注 这里的 s 指的是 size 注意不可写size 只能写s
15     plt.plot([x0,x0],[x0,0],'k--',linewidth=2.5)  #lw 指的是 linewidth,这个可以简写也可不简写
16 
17     #下面添加标注
18     plt.annotate('y=x y=cos(x)的交点为 {}'.format([x0,x0]),fontproperties='SimHei',xy=(x0,x0),xytext=(x0+0.2,x0-0.05))
19 
20 
21 
22 
23 
24 
25     ax = plt.gca()
26     ax.spines['top'].set_visible(False)
27     ax.spines['right'].set_visible(False)
28 
29     ax.spines['bottom'].set_position(('data',0))
30     ax.spines['left'].set_position(('data',0))
31 
32 #交点的位置是 0.739
33 if 0:
34     #二分法
35     if 0:
36         def f(x):
37             return np.cos(x) -x
38         res = [0,np.pi/2]
39         for i in range(10): #[0.7378447589729933, 0.739378739760879]
40             mid = (res[0] + res[1])/2
41             if f(res[0])*f(mid) < 0:
42                 res[1] = mid
43             else:
44                 res[0] = mid
45             print(res)
46             pass
47 
48     #切线法
49     if 1:
50         def f(x):
51             return x-np.cos(x)  #是单调递增的
52         res = np.pi/2
53         for i in range(10): #0.7390851332151607
54             res = res - f(res)/(1 + np.sin(res))
55         print(res)
56         pass
57     pass
58 
59 
60 plt.show()
添加标注

 

你可能感兴趣的:(Matplot 第三部分)