惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
L
LINUX DO - 热门话题
S
Schneier on Security
NISL@THU
NISL@THU
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
月光博客
月光博客
Cyberwarzone
Cyberwarzone
B
Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Microsoft Security Blog
Microsoft Security Blog
Vercel News
Vercel News
小众软件
小众软件
M
MIT News - Artificial intelligence
I
InfoQ
aimingoo的专栏
aimingoo的专栏
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
美团技术团队
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
Help Net Security
Help Net Security
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
T
Tenable Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
Blog — PlanetScale
Blog — PlanetScale
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
D
Docker
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
L
LINUX DO - 最新话题
B
Blog RSS Feed
D
DataBreaches.Net
博客园 - 司徒正美
Recorded Future
Recorded Future
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer YOLOv5 目标检测笔记 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6 pydantic+openai+json 控制大模型输出的最佳范式 roofline model zhipuAI接口兼容openai 在docker部署fastapi宝塔里使用nginx反代套上cloudflare获取请求的真实ip clion搭建libbpf-bootstrap开发环境 coze+coze-discord-proxy+ChatNextWebUI实现AI自由 安卓内核时间使用的是UTC时间 colab运行google最新开源模型Gemma Sora技术报告 视频生成模型作为世界模拟器 笔记 archlinux flutter开发踩坑 fastapi集成google auth登录 linux下NTFS磁盘报错输入输出错误 Venn-Abers 预测器 基于Venn-Abers预测器的系统日志异常检测方法_顾兆军 手机平板远程访问kvm虚拟机的windows phi-2弱智吧测评 poe的gemini pro或是百度开发 google gemini api使用 google gemini api申请 构建用于复杂数据处理的高效UDP服务器和客户端 matplotlib中文字体渲染 TruFor笔记和代码复现 深入分析:GitHub Trending 项目 "multipleWindow3dScene" pua大模型 ggml教程|mnist手写体识别量化推理 xgboost2.0最佳实践 xgboost使用GPU最佳实践 马踏棋盘 cloudlflare推理llama2 docker搭建elasticsearch并使用python连接 FreeU-文字生成图片的免费午餐笔记 使用xgboost的c接口推理模型 Archlinux使用CMake调用xgboost的c接口 m2cgen生成机器学习c语言推理代码 xgboost模型序列化存储并推理 speculative-sampling笔记 prompt2model笔记 RoboTAP笔记 自建obsidian同步服务 MediaPipe即将推出图像生成服务 Dual-Stream Diffusion Net for Text-to-Video Generation笔记 ViT在DDPM取代UNet(DiT) arch4edu搞崩了我的flutter LISA(推理分割)笔记 在终端绘制GPU显存使用曲线 GPTBot介绍 arch蓝牙无法连接 GPU部署llama-cpp-python(llama.cpp通用) 花式求GCD 使用llama构建一个蜜罐(前端) 使用llama构建一个蜜罐(后端) llama-cpp-python快速上手 快速上手llama2.c(更新版) Paper Gestalt笔记 DINO-v2笔记 快速上手llama2.c AnyDoor笔记 Archlinux安装scrcpy加载共享库出错 error while loading shared libraries:libusb-1.0.so.0:wrong ELF class:ELFCLASS32 npc_gzip笔记 python调用c++函数 Filesystem type ntfs3,ntfs not configured in kernel open_clip编码图像和文本 PicGo配置CloudflareR2图片储存 ArchlinuxGnome快捷键打开终端 clip-interrogator代码解析 GroundingDINO安装报错解决 2023华为鲲鹏畅想日暨西安高新国际会议中心零食午饭测评 RoboMaster开源仓库汇总(长期更新) 没有手都可以在腾讯云创建镜像 I3D笔记
解决 Matplotlib Scatter 不支持 Marker 列表的问题:mscatter 实现
About the Author StudyingLover · 2024-06-13 · via StudyingLover's Blog

Matplotlib

Python

Scatter Plot

Markers

Visualization

Data Analysis

Issue 11155

Published on

Matplotlib 的 standard plt.scatter 函数虽然允许传入列表来定义每个点的颜色 (c) 和大小 (s),但原生不支持传入列表来定义形状 (marker)。尝试传入 marker=['o', 'x', ...] 会导致错误。

这是 Matplotlib 长期存在的一个 Feature Request (参考 Issue #11155)。

解决方案

使用自定义函数 mscatter。该函数通过直接替换底层 PathCollection 中的 paths 对象,暴力实现了为每个点指定不同几何形状的功能。

代码实现

import matplotlib.pyplot as plt
import matplotlib.markers as mmarkers

def mscatter(x, y, s, ax=None, m=None, **kw):
    """
    绘制散点图,支持为每个点指定不同的 marker 形状。
    
    参数:
    x, y : array-like
        数据点坐标
    s : scalar or array-like
        点的大小 (size)
    ax : matplotlib.axes.Axes, optional
        绘图坐标轴对象,默认为 plt.gca()
    m : list
        marker 形状列表,长度必须与 x 相同 (e.g. ['o', 'v', 'x', ...])
    **kw : 
        传递给 scatter 的其他参数 (如 c, alpha 等)
    """
    if not ax:
        ax = plt.gca()
        
    # 1. 先调用标准 scatter
    sc = ax.scatter(x, y, s, **kw)
    
    # 2. 如果传入了 marker 列表 m,则替换路径
    if (m is not None) and (len(m) == len(x)):
        paths = []
        for marker in m:
            if isinstance(marker, mmarkers.MarkerStyle):
                marker_obj = marker
            else:
                marker_obj = mmarkers.MarkerStyle(marker)
            
            # 获取 marker 的路径并应用变换
            path = marker_obj.get_path().transformed(marker_obj.get_transform())
            paths.append(path)
            
        # 3. 核心 hack:设置 PathCollection 的路径
        sc.set_paths(paths)
        
    return sc

使用示例

import numpy as np

# 生成测试数据
N = 10
x = np.random.rand(N)
y = np.random.rand(N)
s = np.random.randint(50, 200, size=N) # 随机大小
c = np.random.rand(N)                  # 随机颜色

# 定义不同的 marker 列表
# 注意:长度必须与数据长度一致
markers = ['o', 'v', '^', '<', '>', '1', '2', '3', '4', 's']

# 绘图
fig, ax = plt.subplots()
mscatter(x, y, s=s, c=c, m=markers, ax=ax)

plt.show()

注意事项

  1. 性能:由于需要循环创建 Path 对象,如果数据量极大(例如几万个点),初始化速度可能会比原生 scatter 慢。
  2. 图例 (Legend):自动生成的 Legend 可能无法正确显示混合的形状。如果需要 Legend,可能需要手动构建 Line2D 对象作为 proxy artists。