def _set_TUB(self, mesh_size): geo = dmsh.Polygon([[1 / 3, 1 / 3], [1, 0], [0.5, 0.5]]) points, triangles = dmsh.generate(geo, mesh_size) self._centroids = np.empty((len(triangles), 2)) for i, triangle in enumerate(triangles): self._centroids[i] = np.mean(points[triangle], axis=0) tri = Triangulation(points[:, 0], points[:, 1], triangles) self._trifinder = tri.get_trifinder()
def computeDeformation(X, Y, Zx, Zy, shape): ''' Computes the error, or the deformation between the matched keypoints of both images, at each pixel position in the second image. Input(s): X: vertical coordinate of the keypoints, including fringe keypoints Y: horizontal coordinate of the keypoints, including fringe keypoints Zx: vertical deformation of the keypoints Zy: horizontal deformation of the keypoints shape: shape of the image to deform Output(s): dx: map of the vertical deformation, at each pixel position in the 2nd image dy: map of the horizontal deformation, at each pixel position in the 2nd image ''' triangulation = Triangulation(X, Y) finder = triangulation.get_trifinder() triangle = np.zeros(shape) j_coords = np.arange(shape[1]) for i in range(shape[0]): triangle[i] = finder(i * np.ones(shape[1]).astype('int64'), j_coords) array_x = triangulation.calculate_plane_coefficients(Zx) array_y = triangulation.calculate_plane_coefficients(Zy) n_triangle = array_x.shape[0] dx, dy = np.zeros(shape), np.zeros(shape) indices = np.indices(shape) dx = indices[0]*array_x[:,0][triangle.astype('int16')] + indices[1]*array_x[:, 1][triangle.astype('int16')] + \ array_x[:,2][triangle.astype('int16')] dy = indices[0]*array_y[:,0][triangle.astype('int16')] + indices[1]*array_y[:, 1][triangle.astype('int16')] + \ array_y[:,2][triangle.astype('int16')] return dx, dy
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*math.pi, n_angles, endpoint=False) angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) angles[:, 1::2] += math.pi / n_angles x = (radii*np.cos(angles)).flatten() y = (radii*np.sin(angles)).flatten() triangulation = Triangulation(x, y) xmid = x[triangulation.triangles].mean(axis=1) ymid = y[triangulation.triangles].mean(axis=1) mask = np.where(xmid*xmid + ymid*ymid < min_radius*min_radius, 1, 0) triangulation.set_mask(mask) # Use the triangulation's default TriFinder object. trifinder = triangulation.get_trifinder() # Setup plot and callbacks. plt.subplot(111, aspect='equal') plt.triplot(triangulation, '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()
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()
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 * math.pi, n_angles, endpoint=False) angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1) angles[:, 1::2] += math.pi / n_angles x = (radii * np.cos(angles)).flatten() y = (radii * np.sin(angles)).flatten() triangulation = Triangulation(x, y) xmid = x[triangulation.triangles].mean(axis=1) ymid = y[triangulation.triangles].mean(axis=1) mask = np.where(xmid * xmid + ymid * ymid < min_radius * min_radius, 1, 0) triangulation.set_mask(mask) # Use the triangulation's default TriFinder object. trifinder = triangulation.get_trifinder() # Setup plot and callbacks. plt.subplot(111, aspect='equal') plt.triplot(triangulation, '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()
class MyFigure(FigureCanvas): """__图像显示类__ 三个子图的初始化 & 三种重建图像的显示方式 """ def __init__(self, width, height, dpi): self.fig = plt.figure(figsize=(width, height), dpi=dpi) super(MyFigure, self).__init__(self.fig) self.gs = GridSpec(3, 2) #画布布局 # 用于三角剖分的顶点坐标 self.nodes = np.array([[0.0000e+00, 0.0000e+00], [0.0000e+00, 8.3333e-02], [8.3333e-02, 5.1027e-18], [1.0205e-17, -8.3333e-02], [-8.3333e-02, -1.5308e-17], [0.0000e+00, 1.6667e-01], [1.1785e-01, 1.1785e-01], [1.6667e-01, 1.0205e-17], [1.1785e-01, -1.1785e-01], [2.0411e-17, -1.6667e-01], [-1.1785e-01, -1.1785e-01], [-1.6667e-01, -3.0616e-17], [-1.1785e-01, 1.1785e-01], [0.0000e+00, 2.5000e-01], [1.2500e-01, 2.1651e-01], [2.1651e-01, 1.2500e-01], [2.5000e-01, 1.5308e-17], [2.1651e-01, -1.2500e-01], [1.2500e-01, -2.1651e-01], [3.0616e-17, -2.5000e-01], [-1.2500e-01, -2.1651e-01], [-2.1651e-01, -1.2500e-01], [-2.5000e-01, -4.5924e-17], [-2.1651e-01, 1.2500e-01], [-1.2500e-01, 2.1651e-01], [0.0000e+00, 3.3333e-01], [1.2756e-01, 3.0796e-01], [2.3570e-01, 2.3570e-01], [3.0796e-01, 1.2756e-01], [3.3333e-01, 2.0411e-17], [3.0796e-01, -1.2756e-01], [2.3570e-01, -2.3570e-01], [1.2756e-01, -3.0796e-01], [4.0822e-17, -3.3333e-01], [-1.2756e-01, -3.0796e-01], [-2.3570e-01, -2.3570e-01], [-3.0796e-01, -1.2756e-01], [-3.3333e-01, -6.1232e-17], [-3.0796e-01, 1.2756e-01], [-2.3570e-01, 2.3570e-01], [-1.2756e-01, 3.0796e-01], [0.0000e+00, 4.1667e-01], [1.2876e-01, 3.9627e-01], [2.4491e-01, 3.3709e-01], [3.3709e-01, 2.4491e-01], [3.9627e-01, 1.2876e-01], [4.1667e-01, 2.5513e-17], [3.9627e-01, -1.2876e-01], [3.3709e-01, -2.4491e-01], [2.4491e-01, -3.3709e-01], [1.2876e-01, -3.9627e-01], [5.1027e-17, -4.1667e-01], [-1.2876e-01, -3.9627e-01], [-2.4491e-01, -3.3709e-01], [-3.3709e-01, -2.4491e-01], [-3.9627e-01, -1.2876e-01], [-4.1667e-01, -7.6540e-17], [-3.9627e-01, 1.2876e-01], [-3.3709e-01, 2.4491e-01], [-2.4491e-01, 3.3709e-01], [-1.2876e-01, 3.9627e-01], [0.0000e+00, 5.0000e-01], [1.2941e-01, 4.8296e-01], [2.5000e-01, 4.3301e-01], [3.5355e-01, 3.5355e-01], [4.3301e-01, 2.5000e-01], [4.8296e-01, 1.2941e-01], [5.0000e-01, 3.0616e-17], [4.8296e-01, -1.2941e-01], [4.3301e-01, -2.5000e-01], [3.5355e-01, -3.5355e-01], [2.5000e-01, -4.3301e-01], [1.2941e-01, -4.8296e-01], [6.1232e-17, -5.0000e-01], [-1.2941e-01, -4.8296e-01], [-2.5000e-01, -4.3301e-01], [-3.5355e-01, -3.5355e-01], [-4.3301e-01, -2.5000e-01], [-4.8296e-01, -1.2941e-01], [-5.0000e-01, -9.1849e-17], [-4.8296e-01, 1.2941e-01], [-4.3301e-01, 2.5000e-01], [-3.5355e-01, 3.5355e-01], [-2.5000e-01, 4.3301e-01], [-1.2941e-01, 4.8296e-01], [0.0000e+00, 5.8333e-01], [1.2980e-01, 5.6871e-01], [2.5310e-01, 5.2557e-01], [3.6370e-01, 4.5607e-01], [4.5607e-01, 3.6370e-01], [5.2557e-01, 2.5310e-01], [5.6871e-01, 1.2980e-01], [5.8333e-01, 3.5719e-17], [5.6871e-01, -1.2980e-01], [5.2557e-01, -2.5310e-01], [4.5607e-01, -3.6370e-01], [3.6370e-01, -4.5607e-01], [2.5310e-01, -5.2557e-01], [1.2980e-01, -5.6871e-01], [7.1438e-17, -5.8333e-01], [-1.2980e-01, -5.6871e-01], [-2.5310e-01, -5.2557e-01], [-3.6370e-01, -4.5607e-01], [-4.5607e-01, -3.6370e-01], [-5.2557e-01, -2.5310e-01], [-5.6871e-01, -1.2980e-01], [-5.8333e-01, -1.0716e-16], [-5.6871e-01, 1.2980e-01], [-5.2557e-01, 2.5310e-01], [-4.5607e-01, 3.6370e-01], [-3.6370e-01, 4.5607e-01], [-2.5310e-01, 5.2557e-01], [-1.2980e-01, 5.6871e-01], [0.0000e+00, 6.6667e-01], [1.3006e-01, 6.5386e-01], [2.5512e-01, 6.1592e-01], [3.7038e-01, 5.5431e-01], [4.7140e-01, 4.7140e-01], [5.5431e-01, 3.7038e-01], [6.1592e-01, 2.5512e-01], [6.5386e-01, 1.3006e-01], [6.6667e-01, 4.0822e-17], [6.5386e-01, -1.3006e-01], [6.1592e-01, -2.5512e-01], [5.5431e-01, -3.7038e-01], [4.7140e-01, -4.7140e-01], [3.7038e-01, -5.5431e-01], [2.5512e-01, -6.1592e-01], [1.3006e-01, -6.5386e-01], [8.1643e-17, -6.6667e-01], [-1.3006e-01, -6.5386e-01], [-2.5512e-01, -6.1592e-01], [-3.7038e-01, -5.5431e-01], [-4.7140e-01, -4.7140e-01], [-5.5431e-01, -3.7038e-01], [-6.1592e-01, -2.5512e-01], [-6.5386e-01, -1.3006e-01], [-6.6667e-01, -1.2246e-16], [-6.5386e-01, 1.3006e-01], [-6.1592e-01, 2.5512e-01], [-5.5431e-01, 3.7038e-01], [-4.7140e-01, 4.7140e-01], [-3.7038e-01, 5.5431e-01], [-2.5512e-01, 6.1592e-01], [-1.3006e-01, 6.5386e-01], [0.0000e+00, 7.5000e-01], [1.3024e-01, 7.3861e-01], [2.5652e-01, 7.0477e-01], [3.7500e-01, 6.4952e-01], [4.8209e-01, 5.7453e-01], [5.7453e-01, 4.8209e-01], [6.4952e-01, 3.7500e-01], [7.0477e-01, 2.5652e-01], [7.3861e-01, 1.3024e-01], [7.5000e-01, 4.5924e-17], [7.3861e-01, -1.3024e-01], [7.0477e-01, -2.5652e-01], [6.4952e-01, -3.7500e-01], [5.7453e-01, -4.8209e-01], [4.8209e-01, -5.7453e-01], [3.7500e-01, -6.4952e-01], [2.5652e-01, -7.0477e-01], [1.3024e-01, -7.3861e-01], [9.1849e-17, -7.5000e-01], [-1.3024e-01, -7.3861e-01], [-2.5652e-01, -7.0477e-01], [-3.7500e-01, -6.4952e-01], [-4.8209e-01, -5.7453e-01], [-5.7453e-01, -4.8209e-01], [-6.4952e-01, -3.7500e-01], [-7.0477e-01, -2.5652e-01], [-7.3861e-01, -1.3024e-01], [-7.5000e-01, -1.3777e-16], [-7.3861e-01, 1.3024e-01], [-7.0477e-01, 2.5652e-01], [-6.4952e-01, 3.7500e-01], [-5.7453e-01, 4.8209e-01], [-4.8209e-01, 5.7453e-01], [-3.7500e-01, 6.4952e-01], [-2.5652e-01, 7.0477e-01], [-1.3024e-01, 7.3861e-01], [0.0000e+00, 8.3333e-01], [1.3036e-01, 8.2307e-01], [2.5751e-01, 7.9255e-01], [3.7833e-01, 7.4251e-01], [4.8982e-01, 6.7418e-01], [5.8926e-01, 5.8926e-01], [6.7418e-01, 4.8982e-01], [7.4251e-01, 3.7833e-01], [7.9255e-01, 2.5751e-01], [8.2307e-01, 1.3036e-01], [8.3333e-01, 5.1027e-17], [8.2307e-01, -1.3036e-01], [7.9255e-01, -2.5751e-01], [7.4251e-01, -3.7833e-01], [6.7418e-01, -4.8982e-01], [5.8926e-01, -5.8926e-01], [4.8982e-01, -6.7418e-01], [3.7833e-01, -7.4251e-01], [2.5751e-01, -7.9255e-01], [1.3036e-01, -8.2307e-01], [1.0205e-16, -8.3333e-01], [-1.3036e-01, -8.2307e-01], [-2.5751e-01, -7.9255e-01], [-3.7833e-01, -7.4251e-01], [-4.8982e-01, -6.7418e-01], [-5.8926e-01, -5.8926e-01], [-6.7418e-01, -4.8982e-01], [-7.4251e-01, -3.7833e-01], [-7.9255e-01, -2.5751e-01], [-8.2307e-01, -1.3036e-01], [-8.3333e-01, -1.5308e-16], [-8.2307e-01, 1.3036e-01], [-7.9255e-01, 2.5751e-01], [-7.4251e-01, 3.7833e-01], [-6.7418e-01, 4.8982e-01], [-5.8926e-01, 5.8926e-01], [-4.8982e-01, 6.7418e-01], [-3.7833e-01, 7.4251e-01], [-2.5751e-01, 7.9255e-01], [-1.3036e-01, 8.2307e-01], [0.0000e+00, 9.1667e-01], [1.3046e-01, 9.0734e-01], [2.5825e-01, 8.7954e-01], [3.8080e-01, 8.3383e-01], [4.9559e-01, 7.7115e-01], [6.0029e-01, 6.9277e-01], [6.9277e-01, 6.0029e-01], [7.7115e-01, 4.9559e-01], [8.3383e-01, 3.8080e-01], [8.7954e-01, 2.5825e-01], [9.0734e-01, 1.3046e-01], [9.1667e-01, 2.5967e-16], [9.0734e-01, -1.3046e-01], [8.7954e-01, -2.5825e-01], [8.3383e-01, -3.8080e-01], [7.7115e-01, -4.9559e-01], [6.9277e-01, -6.0029e-01], [6.0029e-01, -6.9277e-01], [4.9559e-01, -7.7115e-01], [3.8080e-01, -8.3383e-01], [2.5825e-01, -8.7954e-01], [1.3046e-01, -9.0734e-01], [5.1934e-16, -9.1667e-01], [-1.3046e-01, -9.0734e-01], [-2.5825e-01, -8.7954e-01], [-3.8080e-01, -8.3383e-01], [-4.9559e-01, -7.7115e-01], [-6.0029e-01, -6.9277e-01], [-6.9277e-01, -6.0029e-01], [-7.7115e-01, -4.9559e-01], [-8.3383e-01, -3.8080e-01], [-8.7954e-01, -2.5825e-01], [-9.0734e-01, -1.3046e-01], [-9.1667e-01, -1.6839e-16], [-9.0734e-01, 1.3046e-01], [-8.7954e-01, 2.5825e-01], [-8.3383e-01, 3.8080e-01], [-7.7115e-01, 4.9559e-01], [-6.9277e-01, 6.0029e-01], [-6.0029e-01, 6.9277e-01], [-4.9559e-01, 7.7115e-01], [-3.8080e-01, 8.3383e-01], [-2.5825e-01, 8.7954e-01], [-1.3046e-01, 9.0734e-01], [0.0000e+00, 1.0000e+00], [1.3053e-01, 9.9144e-01], [2.5882e-01, 9.6593e-01], [3.8268e-01, 9.2388e-01], [5.0000e-01, 8.6603e-01], [6.0876e-01, 7.9335e-01], [7.0711e-01, 7.0711e-01], [7.9335e-01, 6.0876e-01], [8.6603e-01, 5.0000e-01], [9.2388e-01, 3.8268e-01], [9.6593e-01, 2.5882e-01], [9.9144e-01, 1.3053e-01], [1.0000e+00, 6.1232e-17], [9.9144e-01, -1.3053e-01], [9.6593e-01, -2.5882e-01], [9.2388e-01, -3.8268e-01], [8.6603e-01, -5.0000e-01], [7.9335e-01, -6.0876e-01], [7.0711e-01, -7.0711e-01], [6.0876e-01, -7.9335e-01], [5.0000e-01, -8.6603e-01], [3.8268e-01, -9.2388e-01], [2.5882e-01, -9.6593e-01], [1.3053e-01, -9.9144e-01], [1.2246e-16, -1.0000e+00], [-1.3053e-01, -9.9144e-01], [-2.5882e-01, -9.6593e-01], [-3.8268e-01, -9.2388e-01], [-5.0000e-01, -8.6603e-01], [-6.0876e-01, -7.9335e-01], [-7.0711e-01, -7.0711e-01], [-7.9335e-01, -6.0876e-01], [-8.6603e-01, -5.0000e-01], [-9.2388e-01, -3.8268e-01], [-9.6593e-01, -2.5882e-01], [-9.9144e-01, -1.3053e-01], [-1.0000e+00, -1.8370e-16], [-9.9144e-01, 1.3053e-01], [-9.6593e-01, 2.5882e-01], [-9.2388e-01, 3.8268e-01], [-8.6603e-01, 5.0000e-01], [-7.9335e-01, 6.0876e-01], [-7.0711e-01, 7.0711e-01], [-6.0876e-01, 7.9335e-01], [-5.0000e-01, 8.6603e-01], [-3.8268e-01, 9.2388e-01], [-2.5882e-01, 9.6593e-01], [-1.3053e-01, 9.9144e-01]]) # 三角剖分后每个有限元的中心点坐标 self.tripoints = np.loadtxt( r'D:\Proj\EIT\EIT-py\data\tripoints_mean_py.csv', delimiter=",") # 边界值柱状图ax1的横轴标签 self.index_ls = [ 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'CD', 'CE', 'CF', 'CG', 'CH', 'DE', 'DF', 'DG', 'DH', 'EF', 'EG', 'EH', 'FG', 'FH', 'GH' ] # 反投影图ax3的标注 self.notate = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] # 8个电极的圆心角 self.theta = [ 2 * np.pi / 4, 1 * np.pi / 4, 0 * np.pi / 4, 7 * np.pi / 4, 6 * np.pi / 4, 5 * np.pi / 4, 4 * np.pi / 4, 3 * np.pi / 4 ] # 生成三角剖分对象 self.triang = Triangulation(self.nodes[:, 0], self.nodes[:, 1]) self.trifinder = self.triang.get_trifinder() # 图像显示色谱 self.cmap = plt.cm.RdBu_r self.norm = matplotlib.colors.Normalize(vmin=-1, vmax=1) # ax1,ax2,ax3初始化 self.ax1_setup() self.ax2_setup() self.ax3_setup() def ax1_setup(self): """ax1:边界数据柱状图""" self.ax1 = self.fig.add_subplot(self.gs[0, :], aspect='auto') plt.xticks(range(28), self.index_ls) #设置横坐标标签 plt.yticks([-10, 0, 10], c='none') #设置纵坐标刻度范围 plt.gca().xaxis.set_ticks_position('bottom') plt.gca().spines['bottom'].set_position(('data', 0)) plt.gca().spines['right'].set_color('none') plt.gca().spines['left'].set_color('none') plt.gca().spines['top'].set_color('none') def ax2_setup(self): """ax2:图像重建显示""" self.ax2 = self.fig.add_subplot(self.gs[1:3, 0], aspect=1) self.ax2.triplot(self.triang, linewidth=0.6, color='black') #绘制三角剖分网格 self.ax2.set_xticks([-1, 1]) self.ax2.set_yticks([-1, 1]) plt.axis('off') self.cb = self.fig.colorbar(matplotlib.cm.ScalarMappable( norm=self.norm, cmap="RdBu_r"), ax=self.ax2) # 绘制色度条 self.cb.set_ticks([-1, 1]) def ax3_setup(self): """ax3:反投影路径""" self.ax3 = self.fig.add_subplot(self.gs[1:3, 1], projection='polar', aspect=1) for i in np.arange(8): plt.annotate(self.notate[i], xy=(self.theta[i], 1), xytext=(self.theta[i], 1.1), xycoords='data') plt.axis('off') def fe(self, elem_data): """以有限元填充方式显示重构图像""" self.ax2.tripcolor(self.triang, elem_data, cmap="RdBu_r", norm=self.norm, shading='flat') self.ax2.set_xticks([-1, 1]) self.ax2.set_yticks([-1, 1]) self.ax2.set_xticks([]) self.ax2.set_yticks([]) def interp(self, elem_data): """以矩阵插值方式显示重构图像""" self.ax2.tripcolor(self.tripoints[:, 0], self.tripoints[:, 1], elem_data, cmap="RdBu_r", norm=self.norm, shading='gouraud') self.ax2.set_xticks([-1, 1]) self.ax2.set_yticks([-1, 1]) self.ax2.set_xticks([]) self.ax2.set_yticks([]) def contour(self, elem_data): """以轮廓图填充方式显示重构图像""" self.ax2.tricontour(self.tripoints[:, 0], self.tripoints[:, 1], elem_data, levels=5, linewidths=0.5, colors='k') self.ax2.tricontourf(self.tripoints[:, 0], self.tripoints[:, 1], elem_data, cmap="RdBu_r", norm=self.norm, levels=5) self.ax2.set_xticks([-1, 1]) self.ax2.set_yticks([-1, 1]) self.ax2.set_xticks([]) self.ax2.set_yticks([])
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(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', on_mouse_move) plt.show()