matplotlib如何实现自定义散点形状marker
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,这篇文章给大家分享的是有关matplotlib如何实现自定义散点形状marker的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。无填充形状和填充形状使用这种形状只需给mark
千家信息网最后更新 2025年11月15日matplotlib如何实现自定义散点形状marker
这篇文章给大家分享的是有关matplotlib如何实现自定义散点形状marker的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
无填充形状和填充形状
使用这种形状只需给marker指定一个字符或者一个数字即可


Tex形状
通过自定义一个符合Latex格式的字符串复制给marker即可,因此如果想让makrer为一个数字或者单词字母或者特殊的符号便可以定义这样一个字符串
import matplotlib.pyplot as plt import numpy as np data = np.random.rand(3,2)fig = plt.figure() ax = fig.add_subplot(111) markers = ["$\u266B$", r"$\frac{1}{2}$", "$\heartsuit$"]for i in range(data.shape[0]): ax.scatter(data[i,0], data[i,1], marker=markers[i], s=400) plt.show()Path对象
matplotlib中Path类的定义参考了图片格式svg格式的定义,具体不做阐述,可自行百度。Path对象给定制marker提供了极大的便利,可以使用Path模块中已经定义好的一些Path类供用户组合,用户也可以自定义Path类
使用Path模块中的Path对象
import matplotlib.pyplot as pltimport matplotlib.path as mpathimport numpy as npstar = mpath.Path.unit_regular_star(6) # 星型Pathcircle = mpath.Path.unit_circle() # 圆形Path # 整合两个路径对象的点verts = np.concatenate([circle.vertices, star.vertices[::-1, ...]])# 整合两个路径对象点的类型codes = np.concatenate([circle.codes, star.codes])# 根据路径点和点的类型重新生成一个新的Path对象 cut_star = mpath.Path(verts, codes)plt.plot(np.arange(10)**2, '--r', marker=cut_star, markersize=15)plt.show()
自定义Path对象
import matplotlib.pyplot as pltimport matplotlib.path as mpathimport numpy as npcircle = mpath.Path.unit_circle() # 获得一个圆 verts_part= circle.wedge(340,220).vertices # 按逆时针从340度到220度部分圆的路径点codes_part = circle.wedge(340,220).codes # 点类型 verts_part = verts_part[1:-2] # 去除第一个点和最后两个点 codes_part = codes_part[1:-2] # 整合新的点 verts = np.concatenate([np.array([[0,-2]]), verts_part, np.array([[0,-2]])]) codes = [mpath.Path.MOVETO] + codes_part.tolist() + [mpath.Path.CLOSEPOLY] icon = mpath.Path(vertices=verts, codes=codes)plt.plot(verts[:,0], verts[:,1]) plt.show()
plt.scatter(data[:,0], data[:,1], marker=icon, s=300, facecolor="none",edgecolors="black")plt.show()
从svg格式转化为Path对象
既然Path类的定义起源于svg,那么引申出一个问题,能否将svg格式的图片转化为Path对象?这样我们就不用费力自己定义Path对象,而是可以用别人已经定义好的svg格式的图片转换为Path对象
答案是可以的,可以用svgpath3mpl库解析svg转化为matplotlib的Path对象
import matplotlib as mpl from svgpath3mpl import parse_pathimport xml.etree.ElementTree as etree from six import StringIO import re
# svg格式图片的svg代码svg = """"""# 解析代码 tree = etree.parse(StringIO(svg)) root = tree.getroot() width = int(re.match(r'\d+', root.attrib['width']).group())height = int(re.match(r'\d+', root.attrib['height']).group())path_elems = root.findall('.//{http://www.w3.org/2000/svg}path') # 解析出每个Path对象paths = [parse_path(elem.attrib['d']) for elem in path_elems] facecolors = [elem.attrib.get('fill', 'none') for elem in path_elems] facecolors = [c if c !="" else "none" for c in facecolors]edgecolors = [elem.attrib.get('stroke', 'none') for elem in path_elems]linewidths = [elem.attrib.get('stroke_width', 1) for elem in path_elems]#定义旋转矩阵def rot(verts, az): #顺时针旋转 rad = az / 180 * np.pi verts = np.array(verts) rotMat = np.array([[np.cos(rad), -np.sin(rad)], [np.sin(rad), np.cos(rad)]]) transVerts = verts.dot(rotMat) return transVerts
# 合并每个Path对象verts = paths[0].vertices codes = paths[0].codes for i in range(1,len(paths)): verts = np.concatenate([verts, paths[i].vertices]) codes = np.concatenate([codes, paths[i].codes])verts = rot(verts, 180) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(verts[:,0], verts[:,1] , color="red") plt.show()
# 定义解析函数 def svg2path(svg): # 解析代码 tree = etree.parse(StringIO(svg)) root = tree.getroot() path_elems = root.findall('.//{http://www.w3.org/2000/svg}path') # 解析出每个Path对象 paths = [parse_path(elem.attrib['d']) for elem in path_elems] # 合并path对象 verts = paths[0].vertices codes = paths[0].codes for i in range(1, len(paths)): verts = np.concatenate([verts, paths[i].vertices]) codes = np.concatenate([codes, paths[i].codes]) # 复原为原来的形状 verts = rot(verts, 270) verts = np.fliplr(verts) # 水平翻转,镜像 #verts = (verts - verts.min(0)) / (verts.max(0) - verts.min(0)) icon = mpath.Path(vertices=verts, codes=codes) return iconsvg = ["""""", """""", """"""]icons = []for s in svg: icons.append(svg2path(s))
fig = plt.figure() ax = fig.add_subplot(111) for i in range(data.shape[0]): ax.scatter(data[i,0], data[i,1], marker=icons[i], s=3000)plt.show()
感谢各位的阅读!关于"matplotlib如何实现自定义散点形状marker"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
对象
形状
格式
图片
路径
两个
代码
字符
类型
整合
内容
字符串
数字
更多
模块
用户
篇文章
参考
不错
实用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
ups联动服务器
我的世界npc复制服务器
校园网络安全管理方法
资料数据库设计
宿迁网络技术价格查询
如何正确的使用网络技术
实时提醒软件开发
电脑ibm服务器
数据的网络安全
民企非营利企业软件开发
计算机网络技术应用题及答案
索尼 双数据库
风雪网络安全测评公司怎么样
软件开发机所应包含的内容
数据库概论是学什么的
简单地学习网络安全
sqlite数据库并发读写
宽城区智能网络技术服务质量保障
数据库数据记录时间取反
网络安全的特点不包括
2021年3月校园网络安全
人力资源包括哪些数据库
计算机网络技术教研计划
镜像管理windows服务器
中国虚拟网络技术
服务器欠费截图
重视网络安全有什么意义
国资云介绍ppt 网络安全
我国未来的网络安全趋势有
软件开发技术部门的作用