- Matplotlib 基础
- Matplotlib - 首页
- Matplotlib - 简介
- Matplotlib - 与 Seaborn 的比较
- Matplotlib - 环境设置
- Matplotlib - Anaconda 发行版
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - 简单绘图
- Matplotlib - 保存图形
- Matplotlib - 标记
- Matplotlib - 图形
- Matplotlib - 样式
- Matplotlib - 图例
- Matplotlib - 颜色
- Matplotlib - 色图
- Matplotlib - 色图归一化
- Matplotlib - 选择色图
- Matplotlib - 色标
- Matplotlib - 文本
- Matplotlib - 文本属性
- Matplotlib - 子图标题
- Matplotlib - 图像
- Matplotlib - 图像蒙版
- Matplotlib - 注释
- Matplotlib - 箭头
- Matplotlib - 字体
- Matplotlib - 什么是字体?
- 全局设置字体属性
- Matplotlib - 字体索引
- Matplotlib - 字体属性
- Matplotlib - 刻度
- Matplotlib - 线性和对数刻度
- Matplotlib - 对称对数和 Logit 刻度
- Matplotlib - LaTeX
- Matplotlib - 什么是 LaTeX?
- Matplotlib - LaTeX 用于数学表达式
- Matplotlib - LaTeX 在注释中的文本格式
- Matplotlib - PostScript
- 在注释中启用 LaTeX 渲染
- Matplotlib - 数学表达式
- Matplotlib - 动画
- Matplotlib - 艺术家
- Matplotlib - 使用 Cycler 进行样式设置
- Matplotlib - 路径
- Matplotlib - 路径效果
- Matplotlib - 变换
- Matplotlib - 刻度和刻度标签
- Matplotlib - 弧度刻度
- Matplotlib - 日期刻度
- Matplotlib - 刻度格式化程序
- Matplotlib - 刻度定位器
- Matplotlib - 基本单位
- Matplotlib - 自动缩放
- Matplotlib - 反转轴
- Matplotlib - 对数轴
- Matplotlib - Symlog
- Matplotlib - 单位处理
- Matplotlib - 带有单位的椭圆
- Matplotlib - 脊柱
- Matplotlib - 轴范围
- Matplotlib - 轴刻度
- Matplotlib - 轴刻度
- Matplotlib - 格式化轴
- Matplotlib - Axes 类
- Matplotlib - 双轴
- Matplotlib - Figure 类
- Matplotlib - 多图
- Matplotlib - 网格
- Matplotlib - 面向对象接口
- Matplotlib - PyLab 模块
- Matplotlib - Subplots() 函数
- Matplotlib - Subplot2grid() 函数
- Matplotlib - 锚定艺术家
- Matplotlib - 手动等值线
- Matplotlib - 坐标报告
- Matplotlib - AGG 过滤器
- Matplotlib - 带状框
- Matplotlib - 填充螺旋
- Matplotlib - Findobj 演示
- Matplotlib - 超链接
- Matplotlib - 图像缩略图
- Matplotlib - 使用关键字绘图
- Matplotlib - 创建徽标
- Matplotlib - 多页 PDF
- Matplotlib - 多处理
- Matplotlib - 打印标准输出
- Matplotlib - 复合路径
- Matplotlib - Sankey 类
- Matplotlib - MRI 与 EEG
- Matplotlib - 样式表
- Matplotlib - 背景颜色
- Matplotlib - Basemap
- Matplotlib 事件处理
- Matplotlib - 事件处理
- Matplotlib - 关闭事件
- Matplotlib - 鼠标移动
- Matplotlib - 点击事件
- Matplotlib - 滚动事件
- Matplotlib - 按键事件
- Matplotlib - 选择事件
- Matplotlib - 放大镜
- Matplotlib - 路径编辑器
- Matplotlib - 多边形编辑器
- Matplotlib - 定时器
- Matplotlib - Viewlims
- Matplotlib - 缩放窗口
- Matplotlib 小部件
- Matplotlib - 光标小部件
- Matplotlib - 带注释的光标
- Matplotlib - 按钮小部件
- Matplotlib - 复选框
- Matplotlib - 套索选择器
- Matplotlib - 菜单小部件
- Matplotlib - 鼠标光标
- Matplotlib - 多光标
- Matplotlib - 多边形选择器
- Matplotlib - 单选按钮
- Matplotlib - 范围滑块
- Matplotlib - 矩形选择器
- Matplotlib - 椭圆选择器
- Matplotlib - 滑块小部件
- Matplotlib - 跨度选择器
- Matplotlib - 文本框
- Matplotlib 绘图
- Matplotlib - 条形图
- Matplotlib - 直方图
- Matplotlib - 饼图
- Matplotlib - 散点图
- Matplotlib - 箱线图
- Matplotlib - 小提琴图
- Matplotlib - 等值线图
- Matplotlib - 3D 绘图
- Matplotlib - 3D 等值线
- Matplotlib - 3D 线框图
- Matplotlib - 3D 曲面图
- Matplotlib - Quiver 图
- Matplotlib 有用资源
- Matplotlib - 快速指南
- Matplotlib - 有用资源
- Matplotlib - 讨论
Matplotlib - 缩放窗口
在数据可视化/绘图中,缩放窗口是指调整视图以使特定对象或区域更大或更小的过程。此交互式功能在探索图形、图表或任何视觉表示时特别有用,使用户能够专注于感兴趣的特定区域或全面查看整个内容。
Matplotlib 中的缩放窗口
Matplotlib 的关键功能之一是其对事件处理的支持,它允许用户将鼠标点击等事件连接到绘图中的特定操作。在本教程中,我们将探索 Matplotlib 中的缩放窗口事件处理,重点关注button_press_event以及如何使用它来创建可缩放窗口。
示例 1
此示例创建两个图形(源和缩放)。源图形显示散点图,缩放图形显示初始的放大视图。当在源图形中单击一个点时,on_press函数通过使用button_press_event触发。此 on_press 函数调整可缩放图形的限制,以在所单击的点为中心创建缩放效果。
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()
输出
执行上述程序后,您将获得以下输出:
观看下面的视频以观察缩放窗口功能在此处的运作方式。
示例 2
让我们再创建一个使用 Matplotlib 创建缩放窗口的示例。在此示例中,一个简单的正弦波绘制在主轴上,并使用plt.axes()方法创建了一个较小的可缩放窗口。当您单击主图中的一个点时,on_press 函数将被触发,调整缩放窗口的限制,以在所单击的点为中心创建缩放效果。
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()
输出
执行上述程序后,您将获得以下输出:
观看下面的视频以观察缩放窗口功能在此处的运作方式。
广告