You are reading an old version of the documentation (v3.0.0). For the latest version see
Version 3.0.0
Fork me on GitHub

Related Topics

Trifinder Event DemoΒΆ

Example showing the use of a TriFinder object. As the mouse is moved over the triangulation, the triangle under the cursor is highlighted and the index of the triangle is displayed in the plot title.

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]
        points = triang.triangles[tri]
    xs = triang.x[points]
    ys = triang.y[points]
    polygon.set_xy(np.column_stack([xs, ys]))

def motion_notify(event):
    if event.inaxes is None:
        tri = -1
        tri = trifinder(event.xdata, event.ydata)
    plt.title('In triangle %i' % tri)

# 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)
                < 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
plt.gcf().canvas.mpl_connect('motion_notify_event', motion_notify)

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery