python+matplotlib實(shí)現(xiàn)鼠標(biāo)移動(dòng)三角形高亮及索引顯示
Trifinder事件實(shí)例
實(shí)例展示Trifinder對(duì)象對(duì)的使用。當(dāng)鼠標(biāo)移動(dòng)到一個(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)的簡(jiǎn)單實(shí)例,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
pyecharts如何實(shí)現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖
這篇文章主要介紹了pyecharts如何實(shí)現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11基于Python3制作一個(gè)帶GUI界面的小說(shuō)爬蟲(chóng)工具
這篇文章主要為大家介紹了一個(gè)通過(guò)Python3制作的帶GUI界面的小說(shuō)爬蟲(chóng)工具,用來(lái)從筆趣閣爬取小說(shuō)。感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-02-02用于ETL的Python數(shù)據(jù)轉(zhuǎn)換工具詳解
這篇文章主要介紹了用于ETL的Python數(shù)據(jù)轉(zhuǎn)換工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07利用Python創(chuàng)建API服務(wù)器并處理RESTful請(qǐng)求
在軟件開(kāi)發(fā)實(shí)踐中,構(gòu)建API服務(wù)器是一項(xiàng)基礎(chǔ)且重要的任務(wù),本文將介紹如何使用Python中的Flask框架創(chuàng)建一個(gè)API服務(wù)器,并展示如何處理不同的RESTful請(qǐng)求方法,感興趣的小伙伴可以了解下2024-02-02django中賬號(hào)密碼驗(yàn)證登陸功能的實(shí)現(xiàn)方法
這篇文章主要介紹了django中賬號(hào)密碼驗(yàn)證登陸功能的實(shí)現(xiàn)方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Python獲取網(wǎng)頁(yè)上圖片下載地址的方法
這篇文章主要介紹了Python獲取網(wǎng)頁(yè)上圖片下載地址的方法,涉及Python操作正則表達(dá)式匹配字符串的技巧,需要的朋友可以參考下2015-03-03