Matplotlib 简明教程
Matplotlib - Looking Glass
looking glass 是一个通常指代具有反射表面的物体(如镜子)的术语,通过它人们可以观察到自己的反射或周围环境。
就图形用户界面而言,术语“窥镜”有时用来描述一个功能,该功能提供对系统或应用程序特定方面的详细视图或见解。
Looking Glass in Matplotlib
在 Matplotlib 的上下文中,窥镜是 GUI 应用程序或示例,它实现了一个交互式圆形窗口,该窗口会显示或隐藏 Matplotlib 绘图的某些部分。此窥镜示例使用 Matplotlib 的 patches 模块来创建交互式圆形窗口。凭借这项交互性,用户可以动态地浏览底层数据。
本教程演示如何创建交互式圆形窗口,类似于可以移动以显示或隐藏下方绘图部分的观景镜。
Defining and Visualizing initial plot
首先使用 patches.Circle() 类对象定义预定义的观景镜。
以下是初始绘图外观的设置:
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 类实现。此类捕获鼠标事件,允许用户单击、拖动和重新定位观景镜。
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()
Example
让我们看看 Matplotlib 观景镜示例的完整代码。
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()
执行以上程序,您将得到以下图形,左键单击鼠标并将观景镜拖动到观察此示例的工作中:
观看下面的视频来观察此示例的工作。