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實現的簡單實例,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
