python+matplotlib實(shí)現(xiàn)鼠標(biāo)移動三角形高亮及索引顯示
Trifinder事件實(shí)例
實(shí)例展示Trifinder對象對的使用。當(dāng)鼠標(biāo)移動到一個(gè)被分割的三角形上,這個(gè)三角形高亮顯示,并且它的標(biāo)簽在圖標(biāo)題顯示。
展示下演示結(jié)果:

完整代碼:
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
from matplotlib.patches import Polygon
import numpy as np
def update_polygon(tri):
if tri == -1:
points = [0, 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 = 16
n_radii = 5
min_radius = 0.25
radii = 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_angles
x = (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,ys
update_polygon(-1)
plt.gca().add_patch(polygon)
plt.gcf().canvas.mpl_connect('motion_notify_event', motion_notify)
plt.show()
總結(jié)
本文所示是一個(gè)Python+matplotlib實(shí)現(xiàn)的簡單實(shí)例,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
python中p-value的實(shí)現(xiàn)方式
今天小編就為大家分享一篇python中p-value的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
寫一個(gè)Python腳本自動爬取Bilibili小視頻
這篇文章主要介紹了寫一個(gè) Python 腳本自動爬取 Bilibili 小視頻的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
Pytorch損失函數(shù)torch.nn.NLLLoss()的使用
這篇文章主要介紹了Pytorch損失函數(shù)torch.nn.NLLLoss()的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python讀取HDFS目錄下的所有文件的實(shí)現(xiàn)示例
HDFS是Apache Hadoop的分布式文件系統(tǒng),本文主要介紹了Python讀取HDFS目錄下的所有文件的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
基于Python實(shí)現(xiàn)倒計(jì)時(shí)工具
這篇文章主要為大家詳細(xì)介紹了基于Python實(shí)現(xiàn)倒計(jì)時(shí)工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Python實(shí)現(xiàn)求解最大公約數(shù)的五種方法總結(jié)
求最大公約數(shù)是習(xí)題中比較常見的類型,本文小編將給大家提供五種比較常見的算法,都是用Python語言實(shí)現(xiàn)的,感興趣的小伙伴可以了解一下2022-07-07
python3 pillow生成簡單驗(yàn)證碼圖片的示例
本篇文章主要介紹了python3 pillow生成簡單驗(yàn)證碼圖片的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09

