Matplotlib 简明教程

Matplotlib - Zoom Window

数据可视化/绘图中的 Zooming a window 指调整视图以使特定的对象或区域变大或变小的过程。这一交互式功能在探索图表、图形或任何视觉表示时特别有用,它使用户可以关注特定区域或全面查看整个内容。

Zooming a window in data visualizations/plotings refers to the process of adjusting the view to make specific objects or regions larger or smaller. This interactive feature is particularly useful when exploring graphs, charts, or any visual representation, allowing users to focus on specific areas of interest or get a comprehensive view of the entire content.

Zoom Window in Matplotlib

Matplotlib 的主要功能之一是对事件处理的支持,这使得用户可以将事件(例如鼠标点击)连接到绘图中的特定操作。在本教程中,我们将重点探索 Matplotlib 中的缩放窗口事件处理 button_press_event ,以及如何使用它来创建可缩放窗口。

One of the key features of Matplotlib is its support for event handling, which allows users to connect events such as mouse clicks to specific actions in the plot. In this tutorial, we will explore the Zoom Window Event Handling in Matplotlib with a focus on the button_press_event and how it can be used to create zoomable windows.

Example 1

本示例创建两个图形(源和缩放)。源图形显示散点图,缩放图形显示初始的放大视图。当在源图形中单击一个点时, button_press_event 函数使用 on_press 触发。此 on_press 函数调整可缩放图形的限制,以在点击点处创建缩放效果。

This example creates two figures (source and Zoom). The source figure displays a scatter plot, and the zoom figure displays an initial zoomed-in view. When a point is clicked in the source figure, the on_press function is triggered by using the button_press_event. This on_press function adjusts the limits of the zoomable figure to create a zoom effect centered at the clicked point.

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19601)

# Create source and zoomable figures and axes
figsrc, axsrc = plt.subplots(figsize=(3.7, 3.7))
figzoom, axzoom = plt.subplots(figsize=(3.7, 3.7))

# Set initial limits and titles for both axes
axsrc.set(xlim=(0, 1), ylim=(0, 1), autoscale_on=False, title='Click to zoom')
axzoom.set(xlim=(0.45, 0.55), ylim=(0.4, 0.6), autoscale_on=False, title='Zoom window')

# Generate random data for scatter plots
x, y, s, c = np.random.rand(4, 100)
s *= 200

# Plot the scatter plots on both axes
axsrc.scatter(x, y, s, c)
axzoom.scatter(x, y, s, c)

# Define the event handling function
def on_press(event):
   if event.button != 1:
      return
   x, y = event.xdata, event.ydata
   axzoom.set_xlim(x - 0.1, x + 0.1)
   axzoom.set_ylim(y - 0.1, y + 0.1)
   figzoom.canvas.draw()

# Connect the event handler to the source figure
figsrc.canvas.mpl_connect('button_press_event', on_press)

# Show the plots
plt.show()

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

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

zoom window ex1

观看下面的视频,了解缩放窗口功能在此处如何工作。

Watch the video below to observe how the zoom window feature works here.

zoom window

Example 2

让我们再创建一个使用 Matplotlib 创建缩放窗口的示例。在此示例中,绘制在主轴上的简单正弦波,并使用 button_press_event 方法创建较小的可缩放窗口。在主绘图中单击一个点时,on_press 函数触发,调整缩放窗口的限制,以在点击点处创建缩放效果。

Let’s create another example of creating Zoom Window using Matplotlib. In this example, a simple sine wave plotted on the main axis, and a smaller zoomable window created using the plt.axes() method. When you click on a point in the main plot, the on_press function is triggered, adjusting the limits of the zoomed window to create a zoom effect centered at the clicked point.

import matplotlib.pyplot as plt
import numpy as np

# Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create a figure and axis
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(x, y, label='Sin(x)')
ax.set_title('Zoom Window Example')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.legend()

# Create a zoomable window
zoomed_ax = plt.axes([0.55, 0.25, 0.3, 0.3], facecolor='lightgoldenrodyellow')
zoomed_ax.plot(x, y, label='Sin(x)')
zoomed_ax.set_title('Zoomed Window')
zoomed_ax.set_xlim(2, 4)
zoomed_ax.set_ylim(0.5, 1)

def on_press(event):
   if event.button != 1:
      return
   x, y = event.xdata, event.ydata
   zoomed_ax.set_xlim(x - 1, x + 1)
   zoomed_ax.set_ylim(y - 0.2, y + 0.2)
   fig.canvas.draw()

# Connect the event handler to the figure
fig.canvas.mpl_connect('button_press_event', on_press)

plt.show()

执行上述程序时,你将得到以下输出:

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

zoom window ex2

观看下面的视频,了解缩放窗口功能在此处如何工作。

Watch the video below to observe how the zoom window feature works here.

zoom window ex2