Python中scatter函数参数及用法详解
作者:AnneQiQi 时间:2022-05-03 09:55:58
最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下:
1、scatter函数原型
2、其中散点的形状参数marker如下:
3、其中颜色参数c如下:
4、基本的使用方法如下:
#导入必要的模块
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)、不同大小
#导入必要的模块
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)、不同颜色
#导入必要的模块
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
#导入必要的模块
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基本的用法。
PS:下面举个示例
本文记录了python中的数据可视化——散点图scatter,令x作为数据(50个点,每个30维),我们仅可视化前两维。labels为其类别(假设有三类)。
这里的x就用random来了,具体数据具体分析。
label设定为[1:20]->1, [21:35]->2, [36:50]->3,(python中数组连接方法:先强制转为list,用+,再转回array)
用matplotlib的scatter绘制散点图,legend和matlab中稍有不同,详见代码。
x = rand(50,30)
from numpy import *
import matplotlib
import matplotlib.pyplot as plt
#basic
f1 = plt.figure(1)
plt.subplot(211)
plt.scatter(x[:,1],x[:,0])
# with label
plt.subplot(212)
label = list(ones(20))+list(2*ones(15))+list(3*ones(15))
label = array(label)
plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)
# with legend
f2 = plt.figure(2)
idx_1 = find(label==1)
p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)
idx_2 = find(label==2)
p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)
idx_3 = find(label==3)
p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15)
plt.legend(loc = 'upper right')
result:
figure(1):
figure(2):
来源:http://blog.csdn.net/anneqiqi/article/details/64125186
标签:Python,scatter
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何在Access报表中每隔N行显示一条粗线
2008-11-16 18:11:00
段正淳的css笔记(6)-浅谈css框架开发
2008-01-13 22:42:00
![](https://img.aspxhome.com/file/UploadPic/20081/13/2008113224547397s.gif)
Dreamweaver MX网页图片热区使用方法
2008-05-20 12:50:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113613945s.jpg)
Django初步使用Celery处理耗时任务和定时任务问题
2023-10-15 07:41:59
![](https://img.aspxhome.com/file/2023/0/88010_0s.png)
python中list循环语句用法实例
2022-09-12 15:08:05
String.indexOf 方法介绍
2013-06-01 20:22:27
Pycharm及python安装详细教程(图解)
2023-01-06 00:26:00
![](https://img.aspxhome.com/file/2023/6/75266_0s.jpg)
python编程-将Python程序转化为可执行程序[整理]
2022-10-28 03:48:43
jQuery使用手册--核心篇(Core)
2007-11-22 22:05:00
HTML5 Web Storage全解析
2010-06-26 13:06:00
![](https://img.aspxhome.com/file/UploadPic/20106/26/01-15s.jpg)
dir()、readdir()、scandir()和glob()四种遍历目录方法及性能分析
2023-05-25 11:18:27
IE7下动态创建Iframe时,去除边框的办法
2009-01-19 13:56:00
系统默认的MySQL用户名消失的解决方法
2008-12-02 14:26:00
Django压缩静态文件的实现方法详析
2023-06-15 05:31:33
浅谈Python编程中3个常用的数据结构和算法
2022-02-11 20:15:04
python引入导入自定义模块和外部文件的实例
2023-01-25 10:02:18
Python 中 -m 的典型用法、原理解析与发展演变
2023-07-09 17:11:40
![](https://img.aspxhome.com/file/2023/0/87550_0s.jpg)
python argparse模块传参用法实例
2022-01-19 09:53:42
Selenium chrome配置代理Python版的方法
2022-02-24 14:26:08
JavaScrip如果基于url实现图片下载
2023-08-24 22:31:41