当前位置: 首页 > news >正文

Python绘制带自动缩放的线条、多边形和规则多边形集合图

对于前两个子图,我们将使用螺旋。它们的大小将在图形单位中设置,而不是数据单位。它们的位置将通过使用LineCollection和PolyCollection的offsets和offset_transform关键字参数在数据单位中设置。 第三个子图将生成规则多边形,其缩放和定位方式与前两个相同。 最后一个子情节说明了使用 offsets=(xo, yo),即单个元组而不是元组列表,来生成连续偏移的曲线,偏移量以数据单位给出。此行为仅适用于 LineCollection。 import matplotlib.pyplot as plt import numpy as np from matplotlib import collections, transforms nverts = 50 npts = 100 # 做一些螺旋 r = np.arange(nverts) theta = np.linspace(0, 2*np.pi, nverts) xx = r * np.sin(theta) yy = r * np.cos(theta) spiral = np.column_stack([xx, yy]) # 固定随机状态以实现可重复性 rs = np.random.RandomState(19680801) # 做一些偏移 xyo = rs.randn(npts, 2) # 列出颜色列表,依次循环使用默认系列 colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) fig.subplots_adjust(top=0.92, left=0.07, right=0.97, hspace=0.3, wspace=0.3) col = collections.LineCollection( [spiral], offsets=xyo, offset_transform=ax1.transData) trans = fig.dpi_scale_trans + transforms.Affine2D().scale(1.0/72.0) col.set_transform(trans) # the points to pixels transform # 注意:集合初始化器的第一个参数必须是(x, y)元组序列的列表; 我们只有一个序列,但仍然必须将其放入列表中。 ax1.add_collection(col, autolim=True) # autolim=True 启用自动缩放。对于像这样有偏移的集合,它既不高效也不准确,但足以生成一个可用作起点的图。如果你事先知道想显示的 x 和 y 范围,最好明确设置它们,省略 *autolim* 关键字参数(或将其设置为 False),并且省略下面的 'ax1.autoscale_view()' 调用。 # 为线段创建一个变换,使它们的大小以点为单位给出: col.set_color(colors) ax1.autoscale_view() # See comment above, after ax1.add_collection. ax1.set_title('LineCollection using offsets') # ​​​​​​​与上面相同的数据,但填充曲线 col = collections.PolyCollection( [spiral], offsets=xyo, offset_transform=ax2.transData) trans = transforms.Affine2D().scale(fig.dpi/72.0) col.set_transform(trans) # the points to pixels transform ax2.add_collection(col, autolim=True) col.set_color(colors) ax2.autoscale_view() ax2.set_title('PolyCollection using offsets') # ​​​​​​​七边形正多边形 col = collections.RegularPolyCollection( 7, sizes=np.abs(xx) * 10.0, offsets=xyo, offset_transform=ax3.transData) trans = transforms.Affine2D().scale(fig.dpi / 72.0) col.set_transform(trans) # the points to pixels transform ax3.add_collection(col, autolim=True) col.set_color(colors) ax3.autoscale_view() ax3.set_title('RegularPolyCollection using offsets') # ​​​​​​​模拟一系列海洋洋流剖面,每次偏移0.1米/秒,这样它们就形成了有时称为“瀑布”图或“错落”图的效果。 nverts = 60 ncurves = 20 offs = (0.1, 0.0) yy = np.linspace(0, 2*np.pi, nverts) ym = np.max(yy) xx = (0.2 + (ym - yy) / ym) ** 2 * np.cos(yy - 0.4) * 0.5 segs = [] for i in range(ncurves): xxx = xx + 0.02*rs.randn(nverts) curve = np.column_stack([xxx, yy * 100]) segs.append(curve) col = collections.LineCollection(segs, offsets=offs) ax4.add_collection(col, autolim=True) col.set_color(colors) ax4.autoscale_view() ax4.set_title('Successive data offsets') ax4.set_xlabel('Zonal velocity component (m/s)') ax4.set_ylabel('Depth (m)') # 反转y轴,使深度向下增加 ax4.set_ylim(ax4.get_ylim()[::-1]) plt.show()

参考文献:https://matplotlib.org/stable/gallery/shapes_and_collections/collections.html

http://www.cnnetsun.cn/news/1343255.html

相关文章:

  • java面试题总结2
  • 老周虾扯:解构 AI 记忆构建长效 Agent 的底层逻辑与认知体系
  • 大数据建模中的混沌工程:测试系统弹性的方法
  • 深入解析UniAD架构:面向决策规划的端到端自动驾驶Transformer模型全景报告
  • C/C++: 栈包含哪些数据信息
  • 加解密篇 - 非对称加密算法 (RSA、DSA、ECC、DH)
  • 如何使用SQuAD Explorer:探索自然语言理解的终极指南
  • 探索稳定扩散之卓越:Awesome Stable Diffusion
  • 【亲测免费】 WunderGraph 开源项目教程
  • 开源项目 `path` 使用教程
  • 开源项目推荐:Polyfill Library
  • TrackEval:终极多目标跟踪评估工具,HOTA指标轻松掌握!
  • 如何用uni-api快速搭建个人AI服务:5分钟配置多模型负载均衡指南
  • Hyperledger Fabric交易流程详解:从提案到共识的完整生命周期
  • URLImage vs AsyncImage:为什么选择轻量级SwiftUI图片加载库?
  • 从0到1:用FontBlaster构建支持多字体的iOS应用案例
  • cross-spawn vs原生spawn:为什么跨平台开发必须选择前者?
  • 10分钟上手CodeBrowser:从编译数据库到生成首个静态代码网站的完整教程
  • 如何快速上手React Intersection Observer?5分钟入门教程与实例
  • FlexyPool集成HikariCP实战:打造高性能弹性数据库连接池
  • 如何快速集成SideMenuController:iOS侧边菜单开发入门指南
  • 终极指南:使用cookiecutter-django实现高效图像处理——缩略图生成与水印添加全攻略
  • 数据结构面试通关指南:掌握gh_mirrors/al/algorithms中的核心问题与解题技巧
  • 如何高效学习选择排序:从基础实现到优化技巧的完整指南
  • 如何使用Android Sunflower掌握Jetpack Compose:从View到现代UI的完整指南
  • 如何快速掌握mojs文本动画系统:从零开始的架构设计指南
  • 如何快速掌握Pinia性能分析工具:识别状态管理瓶颈的终极指南
  • 如何用Nightwatch.js实现微服务测试:5个跨服务流程验证策略
  • 终极指南:使用Multer实现基于用户角色的文件上传权限控制
  • 如何使用DZNEmptyDataSet打造专业的iOS空数据界面:提升用户体验的完整指南