scatter python_python中的scatter()方法

article/2025/11/10 18:59:24

1、scatter函数原型

201711081604306.jpg

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

201711081604307.jpg

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

201711081604308.jpg

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

#导入必要的模块

importnumpy as np

importmatplotlib.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()

结果如下:

201711081604309.jpg

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

(1)、不同大小

#导入必要的模块

importnumpy as npimportmatplotlib.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*10ax1.scatter(x,y,s=sValue,c='r',marker='x')#设置图标

plt.legend('x1')#显示所画的图

plt.show()

2017110816043010.jpg

(2)、不同颜色

#导入必要的模块

importnumpy as npimportmatplotlib.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()

结果:

2017110816043011.jpg

(3)、线宽linewidths

#导入必要的模块

importnumpy as npimportmatplotlib.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()

结果如下:

2017110816043012.jpg

注: 这就是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 *

importmatplotlibimportmatplotlib.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):

2017110816252313.png

figure(2):

2017110816252314.png


http://chatgpt.dhexx.cn/article/twGLJZuK.shtml

相关文章

scatter python_Python的散点图绘制 scatter

python能画的图种类非常多,而且看上去都很好看,具体种类部分可参看:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure 这里主要是探索下散点图绘制。 1. 首先是导入包,创建数据 imp…

scatter python_Python scatter详解

函数原型:matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None,vmin=None, vmax=None, alpha=None, linewidths=None,verts=None, edgecolors=None, hold=None, data=None,**kwargs) 参数作用如下: x, y位置。 s大小。 c颜色,可能的情况…

scatter

scatter 散点图 全页折叠 语法 scatter(x,y) scatter(x,y,sz) scatter(x,y,sz,c) scatter(___,filled) scatter(___,mkr) scatter(___,Name,Value) scatter(ax,___) s scatter(___) 说明 示例 scatter(x,y) 在向量 x 和 y 指定的位置创建一个包含圆形的散点图。该类型的图形也…

py使用scatter画散点/气泡图

本博文源于《python数据可视化》(黑马程序员编著)。旨在讲解python如何使用scatter函数进行绘画散点图和气泡图。先讲解scatter函数参数如何使用,然后再演示两个例子进行绘画散点图和气泡图 scatter函数参数讲解 scatter(x,y,sNone,cNone,m…

ComposeOptions.kotlinCompilerVersion is deprecated

我为我的 Compose 工程升级 AGP 后 (7.0.0 > 7.0.2)重新编译发生下面错误 ComposeOptions.kotlinCompilerVersion is deprecated. Compose now uses the kotlin compiler defined in your buildscript. 以前需要通过该 composeOptions 指定 Kotlin 版…

比 Java 更强大的 kotlin.Deprecated

我们都知道 Java 有一个java.lang.Deprecated注解,用来将一个 API 标记为“废弃”,或者说“不建议使用”。比如 String 类就有一个被标记为 Deprecated的构造函数: Deprecated public String(byte ascii[], int hibyte) {this(ascii, hibyte…

Android IntentService deprecated|笔记

先回顾一下, 面试一般都喜欢问IntentService 原理, 个人觉的啥是原理,不就是源码吗? 就下面几行源码,就能出滋生出来,几道面试题: 什么IntentService继承service阿,自带looper阿&…

java 注解 @Deprecated

目录 一 笔记二 Deprecated 源码三 定义一个已过时的类 AnnotationTest03_User.java四 使用自定义的过时注解类 一 笔记 Deprecated 可以标注很多元素:类、接口、方法、属性。。。。。。 这个注解也是给编译器看的,也是做编译检查的;被这个…

JAVA后台开发提升注解篇 @Deprecated

前期说明 先说明下,这个注解不加,对代码没有任何影响。 加了的话,会让调用端的人觉得你比较上道。 这是为什么呢? 我们先来简单聊下 Deprecated这个注解。 Deprecated注解 作用域:类、方法或者属性上 格式如下 …

@Deprecated注解

刚学到一个注解 Deprecated 表示这个方法下个版本可能会被弃用 看个东西 /** deprecated */Deprecatedpublic static boolean isEmpty(Nullable Object str) {return str null || "".equals(str);}这是 springframework 下的一个方法 StringUtils.isEmpty() 然后…

deprecated注释 原因

Deprecated 标记下线接口或者属性的时候,希望能够说明下线原因及新的方法地址 可以使用注释 /*** deprecated 我为什么要下线这个字段或者方法,替代的字段或者方法是 {link com.example.demo.SimpleCache.CacheObj#longData}*/Deprecatedprivate BigDe…

deprecated的用法

deprecated的用法:在java中用deprecated标志该方法过时 实例:有如下方法 public Collection getUserPropList(String userId, String systemId,String valueType) throws Exception ... { .... String filter ""; filter "USER_ID" userId …

【Java】Deprecated 注解

1. Deprecated 注解 Deprecated: 用于表示某个程序元素(类,方法等)已过时如果使用 Deprecated 去修饰一个类,表示这个类已经过时了,但过时不代表不能用了,即不推荐使用,仍然可以使用 public class Deprecated_ {publ…

Linux命令之grep命令

一、命令介绍 grep命令是文本搜索命令,它可以正则表达式搜索文本,也可从一个文件中的内容作为搜索关键字。grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字…

grep与egrep

个人觉得egrep比较好用,感觉改良了grep的一些不可以直接操作的东西,但是总体来说还是没太大区别的,都是一个过滤工具。 grep 和 egrep 都要通过 正则表达式来筛选我们想要的东西,只能筛选文本内容,不能对目录筛选&…

Linux grep/egrep命令详解

grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来 grep搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。 grep的规则表达式&…

如何在 Linux 中使用 ripgrep (rg) 命令?

ripgrep是开源社区正在进行的 RIIR&#xff08;用 Rust 重写&#xff09;努力的一个优秀成果。&#xff0c;它旨在成为经典grep 命令的高级替代品。 使用 ripgrep 的语法如下&#xff1a; rg <pattern> [files/directories]使用 ripgrep&#xff0c;无需提及文件名。如…

Linux常用命令——grep(*)

grep 文本过滤工具 语法格式&#xff1a;grep 【options】【pattern】【file】 grep [参数] [匹配模式] [查找的文件] 注意&#xff1a; 1.grep 是 Linux 系统中最重要的命令之一&#xff0c;其功能是从文本文件或管道数据流中筛选匹配的行及数据。 2.grep 命令里的匹配模式或模…

Linux常用命令——grep

grep 文本过滤工具 语法格式:grep 【options】【pattern】【file】 grep [参数] [匹配模式] [查找的文件]注意:1.grep 是 Linux 系统中最重要的命令之一,其功能是从文本文件或管道数据流中筛选匹配的行及数据。2.grep 命令里的匹配模式或模式匹配,都是你要好找的东西,可以…

【Linux】grep 命令详解

文章目录 一、grep常用命令1、语法2、范例 二、grep的一些高级参数1、语法2、范例 三、基础正则表达式练习1、与中括号 [] 结合2、与反向选择^结合使用3、与行首 ^ 和行尾 $ 字符结合4、任意一个字符 . 与重复字符 * 5、 {} 限定连续字符范围 一、grep常用命令 grep的功能是分…