python+matplotlib實現(xiàn)鼠標移動三角形高亮及索引顯示
更新時間:2018年01月15日 15:25:36 投稿:mengwei
這篇文章主要介紹了Python+matplotlib實現(xiàn)鼠標移動三角形高亮及索引顯示,具有一定借鑒價值,需要的朋友可以參考下
Trifinder事件實例
實例展示Trifinder對象對的使用。當鼠標移動到一個被分割的三角形上,這個三角形高亮顯示,并且它的標簽在圖標題顯示。
展示下演示結果:
完整代碼:
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()
總結
本文所示是一個Python+matplotlib實現(xiàn)的簡單實例,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
pyecharts如何實現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖
這篇文章主要介紹了pyecharts如何實現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11利用Python創(chuàng)建API服務器并處理RESTful請求
在軟件開發(fā)實踐中,構建API服務器是一項基礎且重要的任務,本文將介紹如何使用Python中的Flask框架創(chuàng)建一個API服務器,并展示如何處理不同的RESTful請求方法,感興趣的小伙伴可以了解下2024-02-02