Python画图之散点图(plt.scatter)

article/2025/11/5 10:34:45

        散点图的应用很广泛,以前介绍过很多画图方法:Python画图(直方图、多张子图、二维图形、三维图形以及图中图),漏掉了这个,现在补上,用法很简单,我们可以help(plt.scatter)看下它的用法:

Help on function scatter in module 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)Make a scatter plot of `x` vs `y`Marker size is scaled by `s` and marker color is mapped to `c`Parameters----------x, y : array_like, shape (n, )Input datas : scalar or array_like, shape (n, ), optionalsize in points^2.  Default is `rcParams['lines.markersize'] ** 2`. c : color, sequence, or sequence of color, optional, default: 'b'      `c` can be a single color format string, or a sequence of color    specifications of length `N`, or a sequence of `N` numbers to be   mapped to colors using the `cmap` and `norm` specified via kwargs  (see below). 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.  `c` can be a 2-D array in which the     rows are RGB or RGBA, however, including the case of a single      row to specify the same color for all points.marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'     See `~matplotlib.markers` for more information on the different    styles of markers scatter supports. `marker` can be eitheran instance of the class or the text shorthand for a particular    marker.cmap : `~matplotlib.colors.Colormap`, optional, default: NoneA `~matplotlib.colors.Colormap` instance or registered name.       `cmap` is only used if `c` is an array of floats. If None,defaults to rc `image.cmap`.norm : `~matplotlib.colors.Normalize`, optional, default: NoneA `~matplotlib.colors.Normalize` instance is used to scaleluminance data to 0, 1. `norm` is only used if `c` is an array of  floats. If `None`, use the default :func:`normalize`.vmin, vmax : scalar, optional, default: None`vmin` and `vmax` are used in conjunction with `norm` to normalize luminance data.  If either are `None`, the min and max of the      color array is used.  Note if you pass a `norm` instance, your     settings for `vmin` and `vmax` will be ignored.alpha : scalar, optional, default: NoneThe alpha blending value, between 0 (transparent) and 1 (opaque)   linewidths : scalar or array_like, optional, default: NoneIf None, defaults to (lines.linewidth,).verts : sequence of (x, y), optionalIf `marker` is None, these vertices will be used toconstruct the marker.  The center of the marker is locatedat (0,0) in normalized units.  The overall marker is rescaled      by ``s``.edgecolors : color or sequence of color, optional, default: None       If None, defaults to 'face'If 'face', the edge color will always be the same asthe face color.If it is 'none', the patch boundary will notbe drawn.For non-filled markers, the `edgecolors` kwargis ignored and forced to 'face' internally.Returns-------paths : `~matplotlib.collections.PathCollection`Other parameters----------------kwargs : `~matplotlib.collections.Collection` propertiesSee Also--------plot : to plot scatter plots when markers are identical in size and    colorNotes-----* The `plot` function will be faster for scatterplots where markers    don't vary in size or color.* Any or all of `x`, `y`, `s`, and `c` may be masked arrays, in which  case all masks will be combined and only unmasked points will be     plotted.Fundamentally, scatter works with 1-D arrays; `x`, `y`, `s`, and `c` may be input as 2-D arrays, but within scatter they will beflattened. The exception is `c`, which will be flattened only if its size matches the size of `x` and `y`.

我们可以看到参数比较多,平时主要用到的就是大小、颜色、样式这三个参数

s:形状的大小,默认 20,也可以是个数组,数组每个参数为对应点的大小,数值越大对应的图中的点越大。
c:形状的颜色,"b":blue   "g":green    "r":red   "c":cyan(蓝绿色,青色)  "m":magenta(洋红色,品红色) "y":yellow "k":black  "w":white
marker:常见的形状有如下
".":点                   ",":像素点           "o":圆形
"v":朝下三角形   "^":朝上三角形   "<":朝左三角形   ">":朝右三角形
"s":正方形           "p":五边星          "*":星型
"h":1号六角形     "H":2号六角形 

"+":+号标记      "x":x号标记
"D":菱形              "d":小型菱形 
"|":垂直线形         "_":水平线形

我们来看几个示例(在一张图显示了)

import matplotlib.pyplot as plt
import numpy as np
import pandas as pdx=np.array([3,5])
y=np.array([7,8])x1=np.random.randint(10,size=(25,))
y1=np.random.randint(10,size=(25,))plt.scatter(x,y,c='r')
plt.scatter(x1,y1,s=100,c='b',marker='*')#使用pandas来读取
x2=[]
y2=[]
rdata=pd.read_table('1.txt',header=None)
for i in range(len(rdata[0])):x2.append(rdata[0][i].split(',')[0])y2.append(rdata[0][i].split(',')[1])plt.scatter(x2,y2,s=200,c='g',marker='o')
plt.show()

 其中文档1.txt内容如下(上面图中的4个绿色大点)

5,6
7,9
3,4
2,7


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

相关文章

Python 绘制数据散点图

&#x1f3f3;️‍&#x1f308;Python 有着强大的绘图库 matplotlib, 该库集成了大量的绘图函数&#xff0c;可以满足我们平时绝大多数的绘图需求。其中&#xff0c;matplotlib 库是 Python 进行可视化功能的主要软件包&#xff0c;matplot 本是 matlab 中的绘图库&#xff0c…

python画散点图

文章目录 前言一、散点图函数二、函数参数介绍三、代码实例总结 前言 最近在搞聚类算法&#xff0c;所以难免会用到一些散点图的用法&#xff0c;总结一下&#xff0c;方便以后参考。 一、散点图函数 #首先调用一下画图的库 import matplotlib.pyplot as plt plt.scatter(x, …

python绘制散点图

前言 散点图是指在回归分析中&#xff0c;数据点在直角坐标系平面上的分布图&#xff0c;散点图表示因变量随自变量而变化的大致趋势&#xff0c;据此可以选择合适的函数对数据点进行拟合。 用两组数据构成多个坐标点&#xff0c;考察坐标点的分布&#xff0c;判断两变量之间…

python绘制散点图,非常全,非常详细(已验证)

少废话&#xff0c;直接上代码 import matplotlib.pyplot as plt import numpy as np # 1. 首先是导入包&#xff0c;创建数据 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 …

2MSL的特点及意义

含义&#xff1a;&#xff12;&#xff2d;&#xff33;&#xff2c;是主动方在第四次挥手后进入等待时间 特点&#xff1a; &#xff11;、在&#xff12;&#xff2d;&#xff33;&#xff2c;时段中&#xff0c;两端的端口都不能用&#xff0c;除非加入SO_REUSEADDR参数&am…

TCP之2MSL

1.TCP之2MSL 1.1 MSL MSL:Maximum Segment Lifetime报文段最大生存时间&#xff0c;它是任何报文段被丢弃前在网络内的最长时间 为什么存在MSL TCP报文段以IP数据报在网络内传输&#xff0c;而IP数据报则有限制其生存时间的TTL字段&#xff0c;并且TTL的限制是基于跳数 MSL大小…

TCP的2MSL问题

2MSL (Maximum SegmentLifetime) TIME_WAIT状态的存在有两个理由&#xff1a; 让4次挥手关闭流程更加可靠&#xff1b;4次挥手的最后一个ACK是是由主动关闭方发送出去的&#xff0c;若这个ACK丢失&#xff0c;被动关闭方会再次发一个FIN过来。若主动关闭方能够保持一个2MSL的TI…

linux内核网络TIME_WAIT

目录 四次挥手过程 出现的现象 如何查看信息 TIME_WAIT状态等待的时间 TIME_WAIT 的作用 TIME_WAIT副作用 解决方法 1、将系统值&#xff08;net.ipv4.tcp_max_tw_buckets&#xff09;调小 2、调低TCP_TIMEWAIT_LEN 3、SO_LINGER 4、net.ipv4.tcp_tw_reuse 5、SO_…

TCP四次挥手 2MSL TIME_WAIT详解

TCP四次挥手 & 2MSL & TIME_WAIT详解 TCP四次挥手流程各状态解析 2MSL(2倍最大报文段生成时间)2MSL (Maximum Segment Lifetime) TIME_WAIT状态的存在有两个理由该状态为什么设计在主动关闭这一方?如何正确对待2MSL TIME_WAIT? TCP四次挥手流程 【注意】只要是申请关…

网络编程知识预备(2) —— 三次握手与四次挥手、半连接状态、2MSL

参考&#xff1a;网络编程知识预备(2) ——三次握手与四次挥手、流量控制(滑动窗口)、拥塞控制、半连接状态、2MSL_行稳方能走远的博客-CSDN博客 目录 一、三次握手 什么是三次握手&#xff1f; 三次握手图解 三次握手过程解析 &#xff08;1&#xff09;第一次握手 &am…

2.5Modelsim

视频链接&#xff1a;https://v.youku.com/v_show/id_XNTkxNDg2MTEwNA.html?x&sharefromandroid&sharekey59e4c264c93de043603d938d05eb7fd10 题目&#xff1a;例2.5.1中2选1数据选择器的仿真步骤 原理与目的&#xff1a; 原理&#xff1a;数据选择是指经过选择&am…

TCP的四次挥手及为什么要等待2MSL

一、四次挥手的详述 1、假设Client端发起中断连接请求&#xff0c;也就是发送FIN报文。 2、Server端接到FIN报文后&#xff0c;意思是说"我Client端没有数据要发给你了"&#xff0c;但是如果你还有数据没有发送完成&#xff0c;则不必急着关闭Socket&#xff0c;可以…

为什么等待2MSL

下面是TCP四次挥手的图 介绍一下上图中的主要关键字 FIN_WAIT1是主动断开连接方发出关闭请求后的状态&#xff0c;表示主动方(主动断开TCP连接的一方)已经没有信息要发送给被动方 CLOSED_WAIT是被动方接收到主动方的关闭请求后返回ACK响应后的状态&#xff0c;此时被动方应该…

释放连接:四次挥手过程?为什么要等待2MSL

储备知识&#xff1a;TCP报文段的首部格式&#xff08;讲下面提到的&#xff09;&#xff1a; 1.FIN&#xff1a;用来释放一个连接。当FIN1时&#xff0c;表示此报文段的发送方的数据已经发送完毕&#xff0c;并要求释放运输连接。 2. 确认ACK&#xff08;acknowledgment&#…

SAML2.0使用

最近在工作中和海外一家公司对接单点登录&#xff0c;用到了SAML2.0协议&#xff0c;目前公司的单点登录 还是比较老的CASE3.5版本&#xff0c;不支持SAML2&#xff0c;要支持也要定制优&#xff0c;由于后面肯定是要升级&#xff0c;所 以不在源码上做调整支持&#xff0c;单独…

TCP第四次挥手后为什么要等待2MSL后才断开链接?等待时间为什么是2MSL?

为何要等待2MSL&#xff1f; 1.假如第四次挥手失败了&#xff0c;因为丢失而未到达服务器会怎样呢&#xff1f;这样&#xff0c;服务器会一直收不到客户端的回应&#xff0c;也就无法得知客户端是否收到了即将要断开连接的请求。客户端此刻还蒙在鼓里&#xff0c;还在等待服务器…

为什么TIME_WAIT状态是2MSL?(2个原因)

为什么TIME_WAIT的时间是2MSL&#xff1f; 先来看看上文中TIME_WAIT状态存在的两个理由。 首先看理由1&#xff0c;为了可靠地实现全双工连接的终止&#xff0c;假设图2-5中客户端发送的最后一个ACK丢失&#xff0c;服务端将重传FIN&#xff0c;为了能够收到这个超时重传的FIN…

Time-wait状态(2MSL)

本文转自&#xff1a;https://blog.csdn.net/overstack/article/details/8833894&#xff0c;尊重原创 三次握手四次挥手图&#xff1a; time_wait之后会等2msl。 什么是2MSL&#xff1a; MSL是Maximum Segment Lifetime,译为“报文最大生存时间”&#xff0c;他是任何报文在…

【Linux网络编程】TCP状态转换、半关闭、2MSL时长

------------->【Linux系统编程/网络编程】(学习目录汇总) <-------------- 目录 1. 三次握手、四次挥手过程中的状态变化2. TCP状态转换图3. 半关闭4. 2MSL时长4.1 为什么要2MSL时长&#xff1f;4.2 端口复用 1. 三次握手、四次挥手过程中的状态变化 先结合下图回顾一下…

TIME_WAIT状态(2MSL)的作用

主动关闭的Socket端会进入TIME_WAIT状态&#xff0c;并且持续2MSL时间长度&#xff0c;MSL就是maximum segment lifetime(最大分节生命期&#xff09;&#xff0c;这是一个IP数据包能在互联网上生存的最长时间&#xff0c;超过这个时间将在网络中消失。MSL在RFC 1122上建议是2分…