scatter python_Python的散点图绘制 scatter

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

python能画的图种类非常多,而且看上去都很好看,具体种类部分可参看:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure

1468269-20200328161047979-810761627.png

这里主要是探索下散点图绘制。

1. 首先是导入包,创建数据

importmatplotlib.pyplot as pltimportnumpy as np

n = 10

x= np.random.rand(n) * 2 #随机产生10个0~2之间的x坐标

y = np.random.rand(n) * 2 #随机产生10个0~2之间的y坐标

2. 创建一张figure

fig = plt.figure(1)

3. 设置颜色 color 值【可选参数,即可填可不填】,方式有几种

#colors = np.random.rand(n) # 随机产生10个0~1之间的颜色值,或者

colors = ['r', 'g', 'y', 'b', 'r', 'c', 'g', 'b', 'k', 'm'] #可设置随机数取

4. 设置点的面积大小 area 值 【可选参数】

area = 20*np.arange(1,n+1)

5. 设置点的边界线宽度 【可选参数】

widths = np.arange(n) #0-9的数字

6. 正式绘制散点图:scatter

plt.scatter(x, y, s=area, c=colors, linewidths=widths, alpha=0.5, marker='o')

其参数主要有:

def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,

vmin=None, vmax=None, alpha=None, linewidths=None,

verts=None, edgecolors=None,**kwargs):"""A scatter plot of *y* vs *x* with varying marker size and/or color.

Parameters

----------

x, y : array_like, shape (n, )

The data positions.

s : scalar or array_like, shape (n, ), optional

The marker size in points**2.

Default is ``rcParams['lines.markersize'] ** 2``.

c : color, sequence, or sequence of color, optional, default: 'b'

The marker color. Possible values:

- A single color format string.

- A sequence of color specifications of length n.

- A sequence of n numbers to be mapped to colors using *cmap* and

*norm*.

- A 2-D array in which the rows are RGB or RGBA.

Note that *c* should not be a single numeric RGB or RGBA sequence

because that is indistinguishable from an array of values to be

colormapped. If you want to specify the same RGB or RGBA value for

all points, use a 2-D array with a single row.

marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'

The marker style. *marker* can be either an instance of the class

or the text shorthand for a particular marker.

See `~matplotlib.markers` for more information marker styles.

cmap : `~matplotlib.colors.Colormap`, optional, default: None

A `.Colormap` instance or registered colormap name. *cmap* is only

used if *c* is an array of floats. If ``None``, defaults to rc

``image.cmap``.

alpha : scalar, optional, default: None

The alpha blending value, between 0 (transparent) and 1 (opaque).

linewidths : scalar or array_like, optional, default: None

The linewidth of the marker edges. Note: The default *edgecolors*

is 'face'. You may want to change this as well.

If *None*, defaults to rcParams ``lines.linewidth``.

7. 设置轴标签:xlabel、ylabel

#设置X轴标签

plt.xlabel('X坐标')#设置Y轴标签

plt.ylabel('Y坐标')

8. 设置图标题:title

plt.title('test绘图函数')

9. 设置轴的上下限显示值:xlim、ylim

#设置横轴的上下限值

plt.xlim(-0.5, 2.5)#设置纵轴的上下限值

plt.ylim(-0.5, 2.5)

10. 设置轴的刻度值:xticks、yticks

#设置横轴精准刻度

plt.xticks(np.arange(np.min(x)-0.2, np.max(x)+0.2, step=0.3))#设置纵轴精准刻度

plt.yticks(np.arange(np.min(y)-0.2, np.max(y)+0.2, step=0.3))

也可按照xlim和ylim来设置

#设置横轴精准刻度

plt.xticks(np.arange(-0.5, 2.5, step=0.5))#设置纵轴精准刻度

plt.yticks(np.arange(-0.5, 2.5, step=0.5))

11. 在图中某些点上(位置)显示标签:annotate

#plt.annotate("(" + str(round(x[2],2)) +", "+ str(round(y[2],2)) +")", xy=(x[2], y[2]), fontsize=10, xycoords='data') #或者

plt.annotate("({0},{1})".format(round(x[2],2), round(y[2],2)), xy=(x[2], y[2]), fontsize=10, xycoords='data')#xycoords='data' 以data值为基准#设置字体大小为 10

12. 在图中某些位置显示文本:text

plt.text(round(x[6],2), round(y[6],2), "good point", fontdict={'size': 10, 'color': 'red'}) #fontdict设置文本字体#Add text to the axes.

13. 设置显示中文

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签

plt.rcParams['axes.unicode_minus']=False #用来正常显示负号

14. 设置legend,【注意,'绘图测试’:一定要是可迭代格式,例如元组或者列表,要不然只会显示第一个字符,也就是legend会显示不全】

plt.legend(['绘图测试'], loc=2, fontsize = 10)#plt.legend(['绘图测试'], loc='upper left', markerscale = 0.5, fontsize = 10) #这个也可#markerscale:The relative size of legend markers compared with the originally drawn ones.

其参数loc对应为:

1468269-20200328164112903-669497019.png

15. 保存图片 savefig

plt.savefig('test_xx.png', dpi=200, bbox_inches='tight', transparent=False)#dpi: The resolution in dots per inch,设置分辨率,用于改变清晰度#If *True*, the axes patches will all be transparent

16. 显示图片 show

plt.show()

结果如下:

1468269-20200328163840429-1715227207.png

##

参考:

https://blog.csdn.net/u014636245/article/details/82799573

https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure

https://www.jianshu.com/p/78ba36dddad8

https://blog.csdn.net/u010852680/article/details/77770097

https://blog.csdn.net/u013634684/article/details/49646311


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

相关文章

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的功能是分…

【WINDOWS / DOS 批处理】for命令详解(八)

for命令详解&#xff08;一&#xff09;【共十篇】 for命令详解&#xff08;二&#xff09;【共十篇】 for命令详解&#xff08;三&#xff09;【共十篇】 for命令详解&#xff08;四&#xff09;【共十篇】 for命令详解&#xff08;五&#xff09;【共十篇】 fo…