python+matplotlib实现鼠标移动三角形高亮及索引显示

python+matplotlib实现鼠标移动三角形高亮及索引显示,第1张

概述Trifinder事件实例实例展示Trifinder对象对的使用。当鼠标移动到一个被分割的三角形上,这个三角形高亮显示,并且它的标签在图标题显示。

Trifinder事件实例

实例展示Trifinder对象对的使用。当鼠标移动到一个被分割的三角形上,这个三角形高亮显示,并且它的标签在图标题显示。

展示下演示结果:

完整代码:

import matplotlib.pyplot as pltfrom matplotlib.tri import Triangulationfrom matplotlib.patches import polygonimport numpy as npdef update_polygon(tri):  if tri == -1:    points = [0,0]  else:    points = triang.triangles[tri]  xs = triang.x[points]  ys = triang.y[points]  polygon.set_xy(List(zip(xs,ys)))def motion_notify(event):  if event.inaxes is None:    tri = -1  else:    tri = trifinder(event.xdata,event.ydata)  update_polygon(tri)  plt.Title('In triangle %i' % tri)  event.canvas.draw()# Create a Triangulation.n_angles = 16n_radii = 5min_radius = 0.25radii = np.linspace(min_radius,0.95,n_radii)angles = np.linspace(0,2 * np.pi,n_angles,endpoint=False)angles = np.repeat(angles[...,np.newaxis],n_radii,axis=1)angles[:,1::2] += np.pi / n_anglesx = (radii*np.cos(angles)).flatten()y = (radii*np.sin(angles)).flatten()triang = Triangulation(x,y)triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1),y[triang.triangles].mean(axis=1))        < min_radius)# Use the triangulation's default TriFinder object.trifinder = triang.get_trifinder()# Setup plot and callbacks.plt.subplot(111,aspect='equal')plt.triplot(triang,'bo-')polygon = polygon([[0,0],[0,0]],facecolor='y') # dummy data for xs,ysupdate_polygon(-1)plt.gca().add_patch(polygon)plt.gcf().canvas.mpl_connect('motion_notify_event',motion_notify)plt.show()

总结

本文所示是一个Python+matplotlib实现的简单实例,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

您可能感兴趣的文章:python+matplotlib实现礼盒柱状图实例代码Python+matplotlib实现填充螺旋实例python+matplotlib演示电偶极子实例代码Python+matplotlib+numpy实现在不同平面的二维条形图Python+matplotlib+numpy绘制精美的条形统计图python+matplotlib绘制简单的海豚(顶点和节点的 *** 作)Python使用Matplotlib实现Logos设计代码Python使用matplotlib填充图形指定区域代码示例 总结

以上是内存溢出为你收集整理的python+matplotlib实现鼠标移动三角形高亮及索引显示全部内容,希望文章能够帮你解决python+matplotlib实现鼠标移动三角形高亮及索引显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1200746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存