iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >【Python知识】可视化函数plt.scatter
  • 709
分享到

【Python知识】可视化函数plt.scatter

pythonnumpy机器学习 2023-09-04 16:09:57 709人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录 一、说明 二、函数和参数详解 2.1 scatter函数原型 2.2 参数详解 2.3 其中散点的形状参数marker如下: 2.4 其中颜色参数c如下: 三、画图示例 3.1 关于坐标x,y和s,c 3.2 多元高斯的情况 3.3 

目录

一、说明

二、函数和参数详解

2.1 scatter函数原型

2.2 参数详解

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

2.4 其中颜色参数c如下:

三、画图示例

3.1 关于坐标x,y和s,c

3.2 多元高斯的情况

3.3  绘制例子

3.4 绘图例3

3.5  同心绘制

3.6 有标签绘制

3.7 直线划分

3.8 曲线划分


一、说明

       关于matplotlib的scatter函数有许多活动参数,如果不专门注解,是无法掌握精髓的,本文专门针对scatter的参数和调用说起,并配有若干案例。

二、函数和参数详解

2.1 scatter函数原型

matplotlib.pyplot.scatter(xys=Nonec=Nonemarker=Nonecmap=NonenORM=Nonevmin=Nonevmax=Nonealpha=Nonelinewidths=None*edgecolors=Noneplotnonfinite=Falsedata=None**kwargs)

2.2 参数详解

属性参数意义
坐标x,y输入点列的数组,长度都是size
点大小s点的直径数组,默认直径20,长度最大size
点颜色c点的颜色,默认蓝色 'b',也可以是个 RGB 或 RGBA 二维行数组。
点形状marker点的样式,默认小圆圈 'o'。
调色板cmap

Colormap,默认 None,标量或者是一个 colormap 的名字,只有 c 是一个浮点数数组时才使用。如果没有申明就是 image.cmap。

亮度(1)normNormalize,默认 None,数据亮度在 0-1 之间,只有 c 是一个浮点数的数组的时才使用。
亮度(2)vmin,vmax亮度设置,在 norm 参数存在时会忽略。
透明度alpha透明度设置,0-1 之间,默认 None,即不透明
线linewidths 标记点的长度
颜色

edgecolors

颜色或颜色序列,默认为 'face',可选值有 'face', 'none', None。

plotnonfinite

布尔值,设置是否使用非限定的 c ( inf, -inf 或 nan) 绘制点。

**kwargs 

其他参数。

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

2.4 其中颜色参数c如下:

三、画图示例

3.1 关于坐标x,y和s,c

import numpy as npimport matplotlib.pyplot as plt# Fixing random state for reproducibilitynp.random.seed(19680801)N = 50x = np.random.rand(N)y = np.random.rand(N)colors = np.random.rand(N)          # 颜色可以随机area = (30 * np.random.rand(N))**2  # 点的宽度30,半径15plt.scatter(x, y, s=area, c=colors, alpha=0.5)  plt.show()

        注意:以上核心语句是:

plt.scatter(x, y, s=area, c=colors, alpha=0.5, marker=",")

        其中:x,y,s,c维度一样就能成。

3.2 多元高斯的情况

​import numpy as npimport matplotlib.pyplot as pltfig=plt.figure(figsize=(8,6))#Generating a Gaussion dataset:#creating random vectors from the multivariate normal distribution#given mean and covariancemu_vec1=np.array([0,0])cov_mat1=np.array([[1,0],[0,1]])X=np.random.multivariate_normal(mu_vec1,cov_mat1,500)R=X**2R_sum=R.sum(axis=1)plt.scatter(X[:,0],X[:,1],color='green',marker='o', =32.*R_sum,edgecolor='black',alpha=0.5)plt.show()​

3.3  绘制例子

from matplotlib import pyplot as pltimport numpy as np# Generating a Gaussion dTset:#Creating random vectors from the multivaritate normal distribution#givem mean and covariancemu_vecl = np.array([0, 0])cov_matl = np.array([[2,0],[0,2]])x1_samples = np.random.multivariate_normal(mu_vecl, cov_matl,100)x2_samples = np.random.multivariate_normal(mu_vecl+0.2, cov_matl +0.2, 100)x3_samples = np.random.multivariate_normal(mu_vecl+0.4, cov_matl +0.4, 100)plt.figure(figsize = (8, 6))plt.scatter(x1_samples[:,0], x1_samples[:, 1], marker='x',           color = 'blue', alpha=0.7, label = 'x1 samples')plt.scatter(x2_samples[:,0], x1_samples[:,1], marker='o',           color ='green', alpha=0.7, label = 'x2 samples')plt.scatter(x3_samples[:,0], x1_samples[:,1], marker='^',           color ='red', alpha=0.7, label = 'x3 samples')plt.title('Basic scatter plot')plt.ylabel('variable X')plt.xlabel('Variable Y')plt.legend(loc = 'upper right')plt.show()    import matplotlib.pyplot as plt        fig,ax = plt.subplots()        ax.plot([0],[0], marker="o",  markersize=10)    ax.plot([0.07,0.93],[0,0],    linewidth=10)    ax.scatter([1],[0],           s=100)        ax.plot([0],[1], marker="o",  markersize=22)    ax.plot([0.14,0.86],[1,1],    linewidth=22)    ax.scatter([1],[1],           s=22**2)        plt.show()![image.png](Http://upload-images.jianshu.io/upload_images/8730384-8d27a5015b37ee97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)    import matplotlib.pyplot as plt        for dpi in [72,100,144]:            fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)        ax.set_title("fig.dpi={}".format(dpi))            ax.set_ylim(-3,3)        ax.set_xlim(-2,2)            ax.scatter([0],[1], s=10**2,                    marker="s", linewidth=0, label="100 points^2")        ax.scatter([1],[1], s=(10*72./fig.dpi)**2,                    marker="s", linewidth=0, label="100 pixels^2")            ax.legend(loc=8,framealpha=1, fontsize=8)            fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")        plt.show() 

3.4 绘图例3

import matplotlib.pyplot as pltfor dpi in [72,100,144]:    fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)    ax.set_title("fig.dpi={}".format(dpi))    ax.set_ylim(-3,3)    ax.set_xlim(-2,2)    ax.scatter([0],[1], s=10**2,                marker="s", linewidth=0, label="100 points^2")    ax.scatter([1],[1], s=(10*72./fig.dpi)**2,                marker="s", linewidth=0, label="100 pixels^2")    ax.legend(loc=8,framealpha=1, fontsize=8)    fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")plt.show() 

3.5  同心绘制

plt.scatter(2, 1, s=4000, c='r')plt.scatter(2, 1, s=1000 ,c='b')plt.scatter(2, 1, s=10, c='g')

3.6 有标签绘制

import matplotlib.pyplot as pltx_coords = [0.13, 0.22, 0.39, 0.59, 0.68, 0.74,0.93]y_coords = [0.75, 0.34, 0.44, 0.52, 0.80, 0.25,0.55]fig = plt.figure(figsize = (8,5))plt.scatter(x_coords, y_coords, marker = 's', s = 50)for x, y in zip(x_coords, y_coords):    plt.annotate('(%s,%s)'%(x,y), xy=(x,y),xytext = (0, -10), textcoords = 'offset points',ha = 'center', va = 'top')plt.xlim([0,1])plt.ylim([0,1])plt.show()

3.7 直线划分

# 2-cateGory classfication with random 2D-sample data# from a multivariate normal distributionimport numpy as npfrom matplotlib import pyplot as pltdef decision_boundary(x_1):    """Calculates the x_2 value for plotting the decision boundary."""#    return 4 - np.sqrt(-x_1**2 + 4*x_1 + 6 + np.log(16))    return -x_1 + 1# Generating a gaussion dataset:# creating random vectors from the multivariate normal distribution# given mean and covariancemu_vec1 = np.array([0,0])cov_mat1 = np.array([[2,0],[0,2]])x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1,100)mu_vec1 = mu_vec1.reshape(1,2).T # TO 1-COL VECTORmu_vec2 = np.array([1,2])cov_mat2 = np.array([[1,0],[0,1]])x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)mu_vec2 = mu_vec2.reshape(1,2).T # to 2-col vector# Main scatter plot and plot annotationf, ax = plt.subplots(figsize = (7, 7))ax.scatter(x1_samples[:, 0], x1_samples[:,1], marker = 'o',color = 'green', s=40)ax.scatter(x2_samples[:, 0], x2_samples[:,1], marker = '^',color = 'blue', s =40)plt.legend(['Class1 (w1)', 'Class2 (w2)'], loc = 'upper right')plt.title('Densities of 2 classes with 25 bivariate random patterns each')plt.ylabel('x2')plt.xlabel('x1')ftext = 'p(x|w1) -N(mu1=(0,0)^t, cov1 = I)\np.(x|w2) -N(mu2 = (1, 1)^t), cov2 =I'plt.figtext(.15,.8, ftext, fontsize = 11, ha ='left')#Adding decision boundary to plotx_1 = np.arange(-5, 5, 0.1)bound = decision_boundary(x_1)plt.plot(x_1, bound, 'r--', lw = 3)x_vec = np.linspace(*ax.get_xlim())x_1 = np.arange(0, 100, 0.05)plt.show()

3.8 曲线划分

# 2-category classfication with random 2D-sample data# from a multivariate normal distributionimport numpy as npfrom matplotlib import pyplot as pltdef decision_boundary(x_1):    """Calculates the x_2 value for plotting the decision boundary."""    return 4 - np.sqrt(-x_1**2 + 4*x_1 + 6 + np.log(16))# Generating a gaussion dataset:# creating random vectors from the multivariate normal distribution# given mean and covariancemu_vec1 = np.array([0,0])cov_mat1 = np.array([[2,0],[0,2]])x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1,100)mu_vec1 = mu_vec1.reshape(1,2).T # TO 1-COL VECTORmu_vec2 = np.array([1,2])cov_mat2 = np.array([[1,0],[0,1]])x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)mu_vec2 = mu_vec2.reshape(1,2).T # to 2-col vector# Main scatter plot and plot annotationf, ax = plt.subplots(figsize = (7, 7))ax.scatter(x1_samples[:, 0], x1_samples[:,1], marker = 'o',color = 'green', s=40)ax.scatter(x2_samples[:, 0], x2_samples[:,1], marker = '^',color = 'blue', s =40)plt.legend(['Class1 (w1)', 'Class2 (w2)'], loc = 'upper right')plt.title('Densities of 2 classes with 25 bivariate random patterns each')plt.ylabel('x2')plt.xlabel('x1')ftext = 'p(x|w1) -N(mu1=(0,0)^t, cov1 = I)\np.(x|w2) -N(mu2 = (1, 1)^t), cov2 =I'plt.figtext(.15,.8, ftext, fontsize = 11, ha ='left')#Adding decision boundary to plotx_1 = np.arange(-5, 5, 0.1)bound = decision_boundary(x_1)plt.plot(x_1, bound, 'r--', lw = 3)x_vec = np.linspace(*ax.get_xlim())x_1 = np.arange(0, 100, 0.05)plt.show()

来源地址:https://blog.csdn.net/gongdiwudu/article/details/129947219

--结束END--

本文标题: 【Python知识】可视化函数plt.scatter

本文链接: https://www.lsjlt.com/news/393844.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【Python知识】可视化函数plt.scatter
    目录 一、说明 二、函数和参数详解 2.1 scatter函数原型 2.2 参数详解 2.3 其中散点的形状参数marker如下: 2.4 其中颜色参数c如下: 三、画图示例 3.1 关于坐标x,y和s,c 3.2 多元高斯的情况 3.3 ...
    99+
    2023-09-04
    python numpy 机器学习
  • Python可视化函数plt.scatter详解
    目录一、说明二、函数和参数详解2.1 scatter函数原型2.2 参数详解2.3 其中散点的形状参数marker如下:2.4 其中颜色参数c如下:三、画图示例3.1 关于...
    99+
    2023-05-15
    Python plt.scatter Python plt.scatter函数
  • Python可视化matplotlib模块基础知识
    目录1. matplotlib 模块概述 2. matplotlib.pyplot 相关方法 3. matplotlib.pyplot 图表展示 前言: 互联网时代下,在网络中每天都...
    99+
    2024-04-02
  • Python 数据可视化
    Python 数据可视化 Python提供了多个用于数据可视化的工具和库。其中最常用的包括: 1. Matplotlib:Matplotlib 是一个用于绘制二维图形的 Python 库。它提供了广泛的绘图选项,可以帮助您创建线图、散点图...
    99+
    2023-09-17
    python 数据分析 matplotlib
  • python数据可视化
    1、安装matplotlib 在 cmd 中键入 python -m pip install matplotlib,系统将自动安装,需要等一段时间,待完成后 python -m pip list ,显示 敲黑板划重点:一定通过 cdm ...
    99+
    2023-01-30
    数据 python
  • 浅谈python的函数知识
    目录函数参数的两大分类位置参数可变长参数名称空间总结函数参数的两大分类 形式参数 函数定义阶段括号所写的参数 实际参数 函数调用阶段括号内传入的参数 形参与实参的关系...
    99+
    2024-04-02
  • VUE 数据可视化社区:与同行建立联系,分享知识
    与同行建立联系 社区的一个主要优势在于它为同行提供了一个连接平台。通过在线论坛、社交媒体小组和本地聚会,成员可以: 交换经验和最佳实践:社区成员可以分享他们的项目、挑战和解决方案,从彼此的经验中学习。 建立合作关系:连接可以在合作项目、...
    99+
    2024-04-02
  • Python数据可视化库-Matplot
    我们接着上次的继续讲解,先讲一个概念,叫子图的概念。 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(3,2,1) ...
    99+
    2023-01-31
    数据 Python Matplot
  • Python数据可视化详解
    目录一、Matplotlib模块1、绘制基本图表1. 绘制柱形图2. 绘制条形图3. 绘制折线图4. 绘制面积图5. 绘制散点图6. 绘制饼图和圆环图2、图表的绘制和美化技...
    99+
    2023-05-16
    Python数据可视化 Python可视化 数据可视化
  • 【知识点】Python 的np.prod函数详解
    np.prod是Numpy库中的一个函数,全称为numpy.prod,它的作用是计算数组中所有元素的乘积。该函数是一个快速的计算积的方法,可以接收任意数组或矩阵作为输入,并返回这些数字的乘积。 举个例子: import numpy as...
    99+
    2023-10-02
    python numpy Powered by 金山文档
  • python数据可视化怎么做
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-22
  • python中函数知识点有哪些
    这篇文章主要介绍python中函数知识点有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!函数参数的两大分类形式参数函数定义阶段括号所写的参数实际参数函数调用阶段括号内传入的参数形参与实参的关系可以将形参看成是变量...
    99+
    2023-06-25
  • Python数据可视化的方法
    这篇“Python数据可视化的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Python数据可视化的方法”文章吧。一、数...
    99+
    2023-06-30
  • Python中plt.scatter()函数的常见用法小结
    目录plt.scatter()函数用法一.scatter()函数的定义二.scatter()函数的用法plt.scatter()函数用法 一.scatter()函数的定义 matpl...
    99+
    2023-05-15
    plt.scatter()函数用法 plt.scatter()函数 python plt.scatter()
  • python地图可视化
    安装 自从 v0.3.2 开始,为了缩减项目本身的体积以及维持 pyecharts 项目的轻量化运行,pyecharts 将不再自带地图 js 文件。如用户需要用到地图图表,可自行安装对应的地图文件包。下面介绍如何安装。 全球国家...
    99+
    2023-01-31
    地图 python
  • Python数值方法及数据可视化
    随机数和蒙特卡洛模拟求解单一变量非线性方程求解线性系统方程函数的数学积分常微分方程的数值解 等势线绘图和曲线: 等势线  import numpy as np impor...
    99+
    2024-04-02
  • Python基础知识之函数,类,模块
    目录1、Function 函数1)定义函数2)参数设置3)全局和局部变量2、Class类1)定义class2)class的功能3)继承4)私有属性和功能5)特殊方法3、Module模...
    99+
    2024-04-02
  • 马尔可夫链你知道多少?Python可视化解析MCMC
    马尔可夫链(Markov Chain),又称为离散时间马尔可夫链,可以定义为一个随机过程Y,在某时间t上的任何一个点的值仅仅依赖于在时间t-1上的值。这就表示了我们的随机过程在时间t上具有状态x的概率,如果给出它之前所有的状态,那么就相当于...
    99+
    2023-06-02
  • Python 数据可视化之Bokeh详解
    目录安装散点图折线图条形图交互式数据可视化Interactive Legends添加小部件按钮复选框单选按钮总结安装 要安装此类型,请在终端中输入以下命令。 pip install...
    99+
    2024-04-02
  • Python数据可视化库有哪些
    这篇文章主要介绍“Python数据可视化库有哪些”,在日常操作中,相信很多人在Python数据可视化库有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python数据可视化库有哪些”的疑惑有所帮助!接下来...
    99+
    2023-06-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作