Matplotlib中的scatter函数

1、scatter函数原型

Matplotlib中的scatter函数

2、其中散点的形状参数marker如下:

Matplotlib中的scatter函数

3、其中颜色参数c如下:

Matplotlib中的scatter函数

4、基本的使用方法如下:

 

  1. #导入必要的模块  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. #产生测试数据  
  5. x = np.arange(1,10)  
  6. y = x  
  7. fig = plt.figure()  
  8. ax1 = fig.add_subplot(111)  
  9. #设置标题  
  10. ax1.set_title('Scatter Plot')  
  11. #设置X轴标签  
  12. plt.xlabel('X')  
  13. #设置Y轴标签  
  14. plt.ylabel('Y')  
  15. #画散点图  
  16. ax1.scatter(x,y,c = 'r',marker = 'o')  
  17. #设置图标  
  18. plt.legend('x1')  
  19. #显示所画的图  
  20. plt.show()  

 

 

结果如下:

Matplotlib中的scatter函数

5、当scatter后面参数中数组的使用方法,如s,当s是同x大小的数组,表示x中的每个点对应s中一个大小,其他如c,等用法一样,如下:

(1)不同大小

 

  1. #画散点图  
  2. sValue = x*10  
  3. ax1.scatter(x,y,s=sValue,c='r',marker='x')  

 

 

Matplotlib中的scatter函数

(2)不同颜色

 

  1. #画散点图  
  2. cValue = ['r','y','g','b','r','y','g','b','r']  
  3. ax1.scatter(x,y,c=cValue,marker='s')  

 

结果:

Matplotlib中的scatter函数

(3)线宽linewidths

 

  1. #画散点图  
  2. lValue = x  
  3. ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o') 

 

Matplotlib中的scatter函数