GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Matplotlib 散点图 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Matplotlib 教程
Matplotlib 教程 Matplotlib 安装 Matplotlib Pyplot Matplotlib 绘图标记 Matplotlib 绘图线 Matplotlib 轴标签和标题 Matplotlib 网格线 Matplotlib 绘制多图 Matplotlib 散点图 Matplotlib 柱形图 Matplotlib 饼图 Matplotlib 直方图 Matplotlib imshow() Matplotlib imsave() Matplotlib imread() Matplotlib 中文显示 Seaborn 教程 Matplotlib 常用函数 Matplotlib 参考文档
Matplotlib 绘制多图
Matplotlib 柱形图

Matplotlib 散点图

我们可以使用 pyplot 中的 scatter() 方法来绘制散点图。

scatter() 方法语法格式如下:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)

参数说明:

x,y:长度相同的数组,也就是我们即将绘制散点图的数据点,输入数据。

s:点的大小,默认 20,也可以是个数组,数组每个参数为对应点的大小。

c:点的颜色,默认蓝色 'b',也可以是个 RGB 或 RGBA 二维行数组。

marker:点的样式,默认小圆圈 'o'。

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

norm:Normalize,默认 None,数据亮度在 0-1 之间,只有 c 是一个浮点数的数组的时才使用。

vmin,vmax::亮度设置,在 norm 参数存在时会忽略。

alpha::透明度设置,0-1 之间,默认 None,即不透明。

linewidths::标记点的长度。

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

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

**kwargs::其他参数。

以下实例 scatter() 函数接收长度相同的数组参数,一个用于 x 轴的值,另一个用于 y 轴上的值:

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])

plt.scatter(x, y)
plt.show()

显示结果如下:

设置图标大小:

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
sizes = np.array([20,50,100,200,500,1000,60,90])
plt.scatter(x, y, s=sizes)
plt.show()

显示结果如下:

自定义点的颜色:

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
colors = np.array(["red","green","black","orange","purple","beige","cyan","magenta"])

plt.scatter(x, y, c=colors)
plt.show()

显示结果如下:

设置两组散点图:

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y, color = 'hotpink')

x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y, color = '#88c999')

plt.show()

显示结果如下:

使用随机数来设置散点图:

实例

import numpy as np
import matplotlib.pyplot as plt

# 随机数生成器的种子
np.random.seed(19680801)


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2  # 0 to 15 point radii

plt.scatter(x, y, s=area, c=colors, alpha=0.5) # 设置颜色及透明度

plt.title("RUNOOB Scatter Test") # 设置标题

plt.show()

显示结果如下:

颜色条 Colormap

Matplotlib 模块提供了很多可用的颜色条。

颜色条就像一个颜色列表,其中每种颜色都有一个范围从 0 到 100 的值。

下面是一个颜色条的例子:

设置颜色条需要使用 cmap 参数,默认值为 'viridis',之后颜色值设置为 0 到 100 的数组。

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.show()

显示结果如下:

如果要显示颜色条,需要使用 plt.colorbar() 方法:

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.colorbar()

plt.show()

显示结果如下:

换个颜色条参数, cmap 设置为 afmhot_r:

实例

import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='afmhot_r')
plt.colorbar()
plt.show()

显示结果如下:

颜色条参数值可以是以下值:

颜色名称   保留关键字
Accent   Accent_r
Blues   Blues_r
BrBG   BrBG_r
BuGn   BuGn_r
BuPu   BuPu_r
CMRmap   CMRmap_r
Dark2   Dark2_r
GnBu   GnBu_r
Greens   Greens_r
Greys   Greys_r
OrRd   OrRd_r
Oranges   Oranges_r
PRGn   PRGn_r
Paired   Paired_r
Pastel1   Pastel1_r
Pastel2   Pastel2_r
PiYG   PiYG_r
PuBu   PuBu_r
PuBuGn   PuBuGn_r
PuOr   PuOr_r
PuRd   PuRd_r
Purples   Purples_r
RdBu   RdBu_r
RdGy   RdGy_r
RdPu   RdPu_r
RdYlBu   RdYlBu_r
RdYlGn   RdYlGn_r
Reds   Reds_r
Set1   Set1_r
Set2   Set2_r
Set3   Set3_r
Spectral   Spectral_r
Wistia   Wistia_r
YlGn   YlGn_r
YlGnBu   YlGnBu_r
YlOrBr   YlOrBr_r
YlOrRd   YlOrRd_r
afmhot   afmhot_r
autumn   autumn_r
binary   binary_r
bone   bone_r
brg   brg_r
bwr   bwr_r
cividis   cividis_r
cool   cool_r
coolwarm   coolwarm_r
copper   copper_r
cubehelix   cubehelix_r
flag   flag_r
gist_earth   gist_earth_r
gist_gray   gist_gray_r
gist_heat   gist_heat_r
gist_ncar   gist_ncar_r
gist_rainbow   gist_rainbow_r
gist_stern   gist_stern_r
gist_yarg   gist_yarg_r
gnuplot   gnuplot_r
gnuplot2   gnuplot2_r
gray   gray_r
hot   hot_r
hsv   hsv_r
inferno   inferno_r
jet   jet_r
magma   magma_r
nipy_spectral   nipy_spectral_r
ocean   ocean_r
pink   pink_r
plasma   plasma_r
prism   prism_r
rainbow   rainbow_r
seismic   seismic_r
spring   spring_r
summer   summer_r
tab10   tab10_r
tab20   tab20_r
tab20b   tab20b_r
tab20c   tab20c_r
terrain   terrain_r
twilight   twilight_r
twilight_shifted   twilight_shifted_r
viridis   viridis_r
winter   winter_r

AI 思考中...
Matplotlib 绘制多图
Matplotlib 柱形图

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.