【数字的可视化:python画图之散点图sactter函数详解】

最近开始学习python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下:

1、scatter函数原型

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

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

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()  
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
ax1.scatter(x,y,c = 'r',marker = 'o')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()

结果如下:

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

(1)、不同大小

  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. sValue = x*10  
  17. ax1.scatter(x,y,s=sValue,c='r',marker='x')  
  18. #设置图标  
  19. plt.legend('x1')  
  20. #显示所画的图  
  21. plt.show()  
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
sValue = x*10
ax1.scatter(x,y,s=sValue,c='r',marker='x')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()

(2)、不同颜色

  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. cValue = ['r','y','g','b','r','y','g','b','r']  
  17. ax1.scatter(x,y,c=cValue,marker='s')  
  18. #设置图标  
  19. plt.legend('x1')  
  20. #显示所画的图  
  21. plt.show()  
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
cValue = ['r','y','g','b','r','y','g','b','r']
ax1.scatter(x,y,c=cValue,marker='s')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()

结果:

(3)、线宽linewidths

  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. lValue = x  
  17. ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o')  
  18. #设置图标  
  19. plt.legend('x1')  
  20. #显示所画的图  
  21. plt.show()  
#导入必要的模块
import numpy as np
import matplotlib.pyplot as plt
#产生测试数据
x = np.arange(1,10)
y = x
fig = plt.figure()
ax1 = fig.add_subplot(111)
#设置标题
ax1.set_title('Scatter Plot')
#设置X轴标签
plt.xlabel('X')
#设置Y轴标签
plt.ylabel('Y')
#画散点图
lValue = x
ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o')
#设置图标
plt.legend('x1')
#显示所画的图
plt.show()


                     注:  这就是scatter基本的用法。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值