使用matplotlib绘制常用图表(2)常用图标设置

一、使用subplots绘制子图

  

 1 import numpy as np
 2 from matplotlib import pyplot as plt
 3 %matplotlib inline 
 4 x = np.arange(1,100)
 5 #print(x)
 6 
 7 #划分子图将画布分为2x2的画布
 8 fig,axes = plt.subplots(2,2)
 9 
10 axe1 = axes[0,0]
11 axe2 = axes[0,1]
12 axe3 = axes[1,0]
13 axe4 = axes[1,1]
14 
15 #画布大小和分辨率
16 fig = plt.figure(figsize=(20,10),dpi = 80)
17 
18 axe1.plot(x,x)
19 
20 axe2.plot(x,-x)
21 
22 axe3.plot(x,x**2)
23 
24 axe4.plot(x,np.log(x))
25 
26 plt.show()
View Code

二、使用add_subplot绘制子图

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 %matplotlib inline
 4 x = np.arange(1,100)
 5 
 6 fig = plt.figure(figsize = (20,10),dpi=80)
 7 
 8 #画第一个图,2x2矩阵第一个位置
 9 ax1 = fig.add_subplot(2,2,1)
10 ax1.plot(x,x)
11 
12 #画第二个图
13 ax2 = fig.add_subplot(2,2,3)
14 ax2.plot(x,x**2)
15 
16 #画第三个图
17 ax3 =fig.add_subplot(2,2,4)
18 ax3.plot(x,np.log(x))
19 plt.show()
View Code

三、设置图像包围线和底边移动范围

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 y = range(0,14,2)
 5 x = [-3,-2,-1,0,1,2,3]
 6 
 7 plt.figure(figsize=(20,8),dpi=80)
 8 
 9 #获取当前图标图像
10 ax = plt.gca()
11 
12 #设置图像包围线
13 ax.spines['right'].set_color('none')
14 ax.spines['top'].set_color('none')
15 #ax.spines['bottom'].set_color('none')
16 #ax.spines['left'].set_color('none')
17 
18 
19 #设置底边移动范围
20 ax.spines['bottom'].set_position(('data',0))
21 ax.spines['left'].set_position(('data',0))
22 
23 plt.plot(x,y)
24 plt.show()
View Code

四、设置坐标轴范围

 1 # 设置坐标轴范围
 2 import matplotlib.pyplot as plt
 3 import numpy as np
 4 
 5 x = np.arange(-10,11,1)
 6 y = x**2
 7 plt.plot(x,y)
 8 
 9 plt.xlim(xmin = -5)
10 plt.ylim(ymax = 80)
11 plt.show()
View Code
原文地址:https://www.cnblogs.com/luweilehei/p/11416141.html