Matplotlib 简明教程
Matplotlib - Looking Glass
looking glass 是一个通常指代具有反射表面的物体(如镜子)的术语,通过它人们可以观察到自己的反射或周围环境。
A looking glass is a term often refers to an object with a reflective surface, such as a mirror, through which one can observe their own reflection or the surrounding environment.
就图形用户界面而言,术语“窥镜”有时用来描述一个功能,该功能提供对系统或应用程序特定方面的详细视图或见解。
In terms of the graphical user interfaces, the term "looking glass" is sometimes used to describe a feature that provides a detailed view or insight into a specific aspect of a system or application.
Looking Glass in Matplotlib
在 Matplotlib 的上下文中,窥镜是 GUI 应用程序或示例,它实现了一个交互式圆形窗口,该窗口会显示或隐藏 Matplotlib 绘图的某些部分。此窥镜示例使用 Matplotlib 的 patches 模块来创建交互式圆形窗口。凭借这项交互性,用户可以动态地浏览底层数据。
In the context of Matplotlib, looking glass is a GUI application or example that implements an interactive circular window that reveals or hides portions of a Matplotlib plot. This looking glass example uses the Matplotlib’s patches module to create a interactive circular window. With this interactive nature allows users to explore the underlying data dynamically.
本教程演示如何创建交互式圆形窗口,类似于可以移动以显示或隐藏下方绘图部分的观景镜。
This tutorial demonstrates how to create an interactive circular window, which is similar to a looking glass, that can be moved to reveal or hide portions of a plot beneath it.
Defining and Visualizing initial plot
首先使用 patches.Circle() 类对象定义预定义的观景镜。
Start by defining a predefined looking glass using the patches.Circle() class object.
以下是初始绘图外观的设置:
Following is the set up for the Initial plot appearance −
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches
np.random.seed(19680801)
x, y = np.random.rand(2, 200)
fig, ax = plt.subplots(figsize=(7, 4))
circle_= patches.Circle((0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circle_)
ax.plot(x, y, alpha=0.2)
line, = ax.plot(x, y, alpha=1.0, clip_path=circle_)
ax.set_title("Left click and drag to move looking glass")
Implementing the Looking Glass Interaction
让我们看看用于创建交互式观景镜的 EventHandler 类实现。此类捕获鼠标事件,允许用户单击、拖动和重新定位观景镜。
Let’s see the implementation of the EventHandler class used for creating the interactive looking glass. This class captures the mouse events, allowing users to click, drag, and reposition the looking glass.
class EventHandler:
def __init__(self):
# Connect event handlers to the figure canvas
fig.canvas.mpl_connect('button_press_event', self.on_press)
fig.canvas.mpl_connect('button_release_event', self.on_release)
fig.canvas.mpl_connect('motion_notify_event', self.on_move)
# Initialize the center coordinates of the circular window
self.x0, self.y0 = circle_.center
self.pressevent = None
def on_press(self, event):
# Check if the event occurred inside the plot area
if event.inaxes != ax:
return
# Check if the click is inside the circular window
if not circle_.contains(event)[0]:
return
# Store the press event
self.pressevent = event
def on_release(self, event):
# Reset the press event and update the center coordinates
self.pressevent = None
self.x0, self.y0 = circle_.center
def on_move(self, event):
# Check if a press event has occurred and if the mouse is still inside the plot
if self.pressevent is None or event.inaxes != self.pressevent.inaxes:
return
# Calculate the change in coordinates
dx = event.xdata - self.pressevent.xdata
dy = event.ydata - self.pressevent.ydata
# Update the center coordinates of the circle_ular window
circle_.center = self.x0 + dx, self.y0 + dy
# Update the clip path and redraw the plot
line.set_clip_path(circle_)
fig.canvas.draw()
Running the implementation
创建 EventHandler 类的一个实例以在绘图上创建观景镜。
Create an instance of the EventHandler class to create the looking glass on a plot.
handler = EventHandler()
Example
让我们看看 Matplotlib 观景镜示例的完整代码。
Let’s see the complete code of the Matplotlib Looking Glass example.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches
np.random.seed(19680801)
# Generate random data for plot
x, y = np.random.rand(2, 200)
# Create a Matplotlib figure and axis
fig, ax = plt.subplots(figsize=(7, 4))
# Create a circular window (looking glass) and add it to the plot
circle_= patches.Circle((0.5, 0.5), 0.25, alpha=0.8, fc='yellow')
ax.add_patch(circle_)
# Plot the random data with transparency
ax.plot(x, y, alpha=0.2)
# Plot the same data again, but clip it to the circular window
line, = ax.plot(x, y, alpha=1.0, clip_path=circle_)
# Set the plot title
ax.set_title("Left click and drag to move looking glass")
class EventHandler:
def __init__(self):
# Connect event handlers to the figure canvas
fig.canvas.mpl_connect('button_press_event', self.on_press)
fig.canvas.mpl_connect('button_release_event', self.on_release)
fig.canvas.mpl_connect('motion_notify_event', self.on_move)
# Initialize the center coordinates of the circular window
self.x0, self.y0 = circle_.center
self.pressevent = None
def on_press(self, event):
# Check if the event occurred inside the plot area
if event.inaxes != ax:
return
# Check if the click is inside the circular window
if not circle_.contains(event)[0]:
return
# Store the press event
self.pressevent = event
def on_release(self, event):
# Reset the press event and update the center coordinates
self.pressevent = None
self.x0, self.y0 = circle_.center
def on_move(self, event):
# Check if a press event has occurred and if the mouse is still inside the plot
if self.pressevent is None or event.inaxes != self.pressevent.inaxes:
return
# Calculate the change in coordinates
dx = event.xdata - self.pressevent.xdata
dy = event.ydata - self.pressevent.ydata
# Update the center coordinates of the circle_ular window
circle_.center = self.x0 + dx, self.y0 + dy
# Update the clip path and redraw the plot
line.set_clip_path(circle_)
fig.canvas.draw()
# Create an instance of the EventHandler class
handler = EventHandler()
# Display the plot
plt.show()
执行以上程序,您将得到以下图形,左键单击鼠标并将观景镜拖动到观察此示例的工作中:
On executing the above program you will get the following figure left click on mouse and drag the looking glass to observe the working of this example −
观看下面的视频来观察此示例的工作。
Watch the video below to observe the works of this example.