首页 > 其他分享 >Matplotlib系列-使用鼠标滚轮对matplotlib进行缩放

Matplotlib系列-使用鼠标滚轮对matplotlib进行缩放

时间:2024-08-04 09:24:43浏览次数:10  
标签:滚轮 min max 缩放 Matplotlib matplotlib event

一  问题说明

     使用matplotlib进行数据可视化时,我们经常需要调整图表的缩放级别以便更清晰地查看数据细节。除了使用工具栏中的缩放按钮外,我们还可以利用鼠标滚轮来实现更便捷的缩放操作。

     要实现这一功能,我们需要结合matplotlib的事件处理机制。具体来说,就是监听鼠标滚轮事件('scroll_event'),然后根据滚轮的方向来调整图表的显示范围。

二 全部源代码

以下是一个简单的示例代码,展示了如何使用鼠标滚轮对matplotlib图表进行X方向上的缩放,可以直接复制到py文件双击运行:

import numpy as np  
import matplotlib.pyplot as plt  
  
# 初始X轴范围  
x_min, x_max = 0, 2 * np.pi  
  
def on_scroll(event):  
    global x_min, x_max  
    # 检查滚轮方向  
    if event.button == 'up':  
        # 向前滚动,缩小范围  
        scale_factor = 0.9  
    elif event.button == 'down':  
        # 向后滚动,扩大范围  
        scale_factor = 1.1  
    else:  
        # 忽略其他滚轮事件  
        return  
      
    # 计算新的X轴范围  
    current_range = x_max - x_min  
    midpoint = (x_max + x_min) / 2  
    new_range = current_range * scale_factor  
    x_min = midpoint - new_range / 2  
    x_max = midpoint + new_range / 2  
      
    # 更新X轴范围并重新绘制图形  
    ax.set_xlim(x_min, x_max)  
    fig.canvas.draw()  
  
# 创建图形和轴  
fig, ax = plt.subplots()  
t = np.linspace(x_min, x_max, 256)  
s = np.sin(t)  
ax.plot(t, s)  
  
# 连接滚轮事件处理函数  
fig.canvas.mpl_connect('scroll_event', on_scroll)  
  
plt.show()

标签:滚轮,min,max,缩放,Matplotlib,matplotlib,event
From: https://blog.csdn.net/schuberta/article/details/140901868

相关文章

  • Vue 使用 vue-drag-resize 实现拖拽和随意缩放大小及安装报错处理
    一、vue-drag-resize的安装yarnaddvue-drag-resize 下面是错误解决方案:TypeError:Cannotreadpropertiesofundefined(reading‘_c’) 解决方案:在引入时加上“/src”: importVueDragResizefrom"vue-drag-resize";改成importVueDragResizefrom"vue-d......
  • 使用 matplotlib 对簇柱形图和折线图进行动画处理
    我正在尝试制作一个逐帧更新的动画图表,以使最终结果的每个x轴条目有两列(在年份或年份和Q之间确实有所不同,但无论哪种方式都是字符串)和一条线。|||我有两个数据框,在这种情况下我读到了帮助。df和df_line{'GDP':{'2013Q1':6.2,'2013Q2':6.1,'2013Q3':6.63,......
  • 如何旋转辅助 y 轴标签,使其不与 y 刻度、matplotlib 重叠
    我正在尝试将辅助y标签旋转到270degrees,但是当我通过rotate=270参数执行此操作时,它会与我的y刻度文本重叠。任何想法如何解决这一问题?fig,ax=plt.subplots()ax.plot(df.index,df.tripTime,label='FishingEffort',marker='D')ax......
  • Android 自定义图片拖动、缩放、旋转
    Android图片拖动、缩放、旋转图片拖动定义一个类,继承AppCompatImageViewpublicclassMyImageViewextendsAppCompatImageView{//实现方法publicMyImageView(Contextcontext){this(context,null);}//实现方法publicMyImageView(Cont......
  • 【C#】WPF实现HaIcon图像缩放、移动
    1.HaIcon实现的C#代码////FilegeneratedbyHDevelopforHALCON/DOTNET(C#)Version12.0////ThisfileisintendedtobeusedwiththeHDevelopTemplateor//HDevelopTemplateWPFprojectslocatedunder%HALCONEXAMPLES%\c#usingSystem;usingSystem.Thre......
  • Python 进行数据可视化(Matplotlib, Seaborn)
    数据可视化是数据科学和分析中的重要工具,它通过图形表示数据,使得复杂的数据变得易于理解和分析。在Python中,最常用的两个数据可视化库是Matplotlib和Seaborn。Matplotlib1.简介Matplotlib是一个用于生成二维图形的Python库。它提供了类似于Matlab的绘图接口,使用户能够轻松......
  • 【C#】WPF自定义Image实现图像缩放、平移
    1.xaml实现<UserControlx:Class="HalconDemo.ImageUserControl"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://sche......
  • WInform 控件大小随窗体大小等比例缩放
    winform控件自动大小usingSystem;usingSystem.Drawing;usingSystem.Windows.Forms;usingUCControl=System.Windows.Forms.Control;///<summary>///控件自动适应///使用直接继承FormAutoSize即可///示例如下:///***示例代码***:publicForm1()///***示例代......
  • matplotlib 光标在图表上的错误位置
    在下面的程序中,我想将2个垂直光标放置在x轴上的2个定义位置处。问题在于,正如您在屏幕截图中看到的那样,光标与“curseur1”和“curseur2”字段中选择的值不匹配此外,当放置光标时,x轴(比例)的值会消失。当删除光标时,通过取消选择“Curseurs”按钮,光标的值保留在axx上.........
  • 有没有办法获得最后的 Matplotlib 图?
    我有一个前端调用一个名为streamlit的包,如果我调用该函数,它将生成最新的Matplotlib对象st.pyplot()但是,我想获得实际的fig对象,因为我想单独存储。我面临的问题是fig是由另一个名为Pandasai的包创建的。(即,我从来不需要导入matplotlib包)。所......