Matplotlib 简明教程

Matplotlib - Mouse Move

在一般的计算机编程和软件设计中,术语 mouse move 指的是将计算机鼠标设备在其表面上移动来创建相应的屏幕上光标或指针移动的动作。

In general computer programming and software design, the term mouse move refers to an action of moving a computer mouse device across a surface to create the corresponding cursor or pointer movement on-screen.

Mouse Move in Matplotlib

Matplotlib 中的鼠标移动事件允许用户捕获作图中光标所在的位置。该功能能创建交互式特性,例如在光标所在位置显示信息或根据光标移动实时更新可视化。

The mouse move event in Matplotlib allows users to capture the cursor’s position over a plotted figure. This capability enables the creation of interactive features, such as displaying information at the cursor location or updating visualizations in real time based on the cursor’s movements.

在本教程中,我们将探索如何在 Matplotlib 中使用鼠标移动事件来增强交互式绘图。您能通过连接 motion_notify_event ,捕获光标位置并实施各种动作,向用户提供直观的方式来探索并分析所绘的数据。

In this tutorial, we will explore how to use Mouse movement events in Matplotlib to enhance interactive plots. This is done by connecting to the motion_notify_event, you can capture the cursor’s position and implement various actions, providing users with an intuitive way to explore and analyze plotted data.

Example

让我们开始一个简单的示例,当鼠标移动到输出图上时,它将打印数据和像素坐标。

Let’s start with a simple example that prints the data and pixel coordinates as the mouse moves over output figure.

import matplotlib.pyplot as plt
import numpy as np

# Input data for ploting a circle
angs = np.linspace(0, 2 * np.pi, 10**6)
rs = np.zeros_like(angs) + 1
xs = rs * np.cos(angs)
ys = rs * np.sin(angs)

# Create a figure
fig, ax = plt.subplots(figsize=(7, 4))

# Plot the data
plt.plot(xs, ys)

# Function to handle the event
def on_move(event):
   if event.inaxes:
      print(f'data coords {event.xdata}, {event.ydata},',
         f'pixel coords {event.x}, {event.y}')

# connect the event with the callable function
binding_id = plt.connect('motion_notify_event', on_move)

# Display the plot
plt.show()

在执行上述程序后,你将获得以下输出 -

On executing the above program you will get the following output −

mouse move ex1

从上图中,当鼠标移动时,您可以持续读取鼠标坐标 −

From the above figure, you can continuously read the coordinates of the mouse when it is moved −

data coords 0.22000000000192466, 0.8999999999988899, pixel coords 413, 324
data coords 0.21188940092360364, 0.9071428571417381, pixel coords 411, 325
data coords 0.20377880184528263, 0.9142857142845864, pixel coords 409, 326
data coords 0.19972350230612212, 0.9214285714274346, pixel coords 408, 327
data coords 0.1916129032278011, 0.9214285714274346, pixel coords 406, 327
data coords 0.1916129032278011, 0.9285714285702829, pixel coords 406, 328
data coords 0.18755760368864083, 0.9285714285702829, pixel coords 405, 328
data coords 0.18350230414948032, 0.9357142857131315, pixel coords 404, 329
data coords 0.17944700461031982, 0.9357142857131315, pixel coords 403, 329
data coords 0.1753917050711593, 0.9357142857131315, pixel coords 402, 329
data coords 0.1753917050711593, 0.9428571428559798, pixel coords 402, 330
data coords 0.1713364055319988, 0.9428571428559798, pixel coords 401, 330
data coords 0.1672811059928383, 0.949999999998828, pixel coords 400, 331
data coords 0.1632258064536778, 0.949999999998828, pixel coords 399, 331

观看下面的视频来观察鼠标移动事件特性在此处如何工作。

Watch the video below to observe how the mouse move event feature works here.

mouse move ex1

Real-Time Plotting of Mouse Movement

使用 matplotlib 中的 motion_notify_event 可以实现近乎实时地绘制鼠标移动。该事件允许您捕获鼠标光标的动态移动,从而能够创建交互式且响应式可视化。

It is possible to achieve near real-time plotting of mouse movement by using the motion_notify_event in matplotlib. This event allows you to capture the dynamic movement of the mouse cursor, enabling the creation of interactive and responsive visualizations.

Example

以下示例演示如何使用 Matplotlib 近乎实时地绘制鼠标路径。

The following example demonstrates how to plot the mouse path in near real-time using Matplotlib.

import matplotlib.pyplot as plt

# Create the plot
fig, ax = plt.subplots(figsize=(7, 4))

# Set the limits of the plot
ax.set_xlim(0, 1920-1)
ax.set_ylim(0, 1080-1)

# Initialize lists to store mouse coordinates
x,y = [0], [0]

# create empty plot
points, = ax.plot([], [], '-', color='green')

# cache the background
background = fig.canvas.copy_from_bbox(ax.bbox)

def on_move(event):
   # Append the current mouse coordinates
   x.append(event.xdata)
   y.append(event.ydata)

   # Update the plot data
   points.set_data(x,y)

   # Restore the background
   fig.canvas.restore_region(background)

   # Redraw the points
   ax.draw_artist(points)

   # Fill in the axes rectangle
   fig.canvas.blit(ax.bbox)

# Connect the on_move function to the motion_notify_event
fig.canvas.mpl_connect("motion_notify_event", on_move)

# Display the plot
plt.show()

在执行上述程序后,你将获得以下输出 -

On executing the above program you will get the following output −

mouse move ex2

观看下面的视频来观察鼠标移动事件特性在此处如何工作。

Watch the video below to observe how the mouse move event feature works here.

mouse move ex2

Highlighting Triangles with TriFinder

使用 matplotlib.tri 模块中的 TriFinder 类来识别三角剖分内的三角形。通过将它与 motion_notify_event 结合使用,您可以动态高亮显示鼠标在三角剖分图上移动的三角形。

Using the TriFinder class from the matplotlib.tri module to identify triangles within a Triangulation. By combining it with the motion_notify_event, you can dynamically highlight the triangle where the mouse moved over the triangulated plot.

Example

该示例演示了在 Matplotlib 中使用 TriFinder 对象。当鼠标移动到三角剖分上时,光标下的三角形将被高亮显示,并且三角形的索引将显示在图的标题中。

This example demonstrates the use of a TriFinder object in Matplotlib. As the mouse is moved over a 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
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.tri import Triangulation

def update_highlighted_triangle(triangle_index):
   if triangle_index == -1:
      points = [0, 0, 0]
   else:
      points = triangulation.triangles[triangle_index]
   xs = triangulation.x[points]
   ys = triangulation.y[points]
   highlighted_polygon.set_xy(np.column_stack([xs, ys]))

def on_mouse_move(event):
   if event.inaxes is None:
      triangle_index = -1
   else:
      triangle_index = tri_finder(event.xdata, event.ydata)
   update_highlighted_triangle(triangle_index)
   ax.set_title(f'In triangle {triangle_index}')
   event.canvas.draw()

# Create a Triangulation.
num_angles = 16
num_radii = 5
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, num_radii)
angles = np.linspace(0, 2 * np.pi, num_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], num_radii, axis=1)
angles[:, 1::2] += np.pi / num_angles
x_values = (radii*np.cos(angles)).flatten()
y_values = (radii*np.sin(angles)).flatten()
triangulation = Triangulation(x_values, y_values)
triangulation.set_mask(np.hypot(x_values[triangulation.triangles].mean(axis=1),
   y_values[triangulation.triangles].mean(axis=1))
   < min_radius)

# Use the triangulation's default TriFinder object.
tri_finder = triangulation.get_trifinder()

# Setup plot and callbacks.
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'}, figsize=(7, 4))
ax.triplot(triangulation, 'bo-')
highlighted_polygon = Polygon([[0, 0], [0, 0]], facecolor='y')  # dummy data for (xs, ys)
update_highlighted_triangle(-1)
ax.add_patch(highlighted_polygon)
fig.canvas.mpl_connect('motion_notify_event', on_mouse_move)
plt.show()

在执行上述程序后,你将获得以下输出 -

On executing the above program you will get the following output −

mouse move ex3

观看下面的视频来观察鼠标移动事件特性在此处如何工作。

Watch the video below to observe how the mouse move event feature works here.

mouse move ex3