博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python matplotlib绘图,使用鼠标滚轮放大/缩小图像
阅读量:2638 次
发布时间:2019-05-11

本文共 1054 字,大约阅读时间需要 3 分钟。

思路:

  1. 使用fig.canvas.mpl_connect()函数来绑定相关fig的滚轮事件
  2. 利用事件event的inaxes属性获取当前鼠标所在坐标系ax
  3. 使用get_xlim()函数获取坐标系ax的x/y轴坐标刻度范围
  4. 使用set()函数对坐标系ax进行放大/缩小

示例:

import matplotlib.pyplot as pltimport numpy as npfig = plt.figure()def call_back(event):    axtemp=event.inaxes    x_min, x_max = axtemp.get_xlim()    fanwei = (x_max - x_min) / 10    if event.button == 'up':        axtemp.set(xlim=(x_min + fanwei, x_max - fanwei))        print('up')    elif event.button == 'down':        axtemp.set(xlim=(x_min - fanwei, x_max + fanwei))        print('down')    fig.canvas.draw_idle()  # 绘图动作实时反映在图像上fig.canvas.mpl_connect('scroll_event', call_back)fig.canvas.mpl_connect('button_press_event', call_back)ax1 = plt.subplot(3,1,1)#截取幕布的一部分ax1.xaxis.set_major_formatter(plt.NullFormatter())  # 取消x轴坐标x = np.linspace(-5, 5, 10)y = x ** 2 + 1plt.ylabel('first')plt.plot(x, y)plt.grid()ax2 = plt.subplot(3,1,2)ax2.xaxis.set_major_formatter(plt.NullFormatter())  # 取消x轴坐标y1=-x**2+1plt.plot(x, y1)ax3 = plt.subplot(3,1,3)y3=-x*2+1plt.plot(x, y3)plt.show()

输出效果:

PS:在相应坐标系内滚动鼠标滚轮即可放大/缩小x轴。

转载地址:http://oszfd.baihongyu.com/

你可能感兴趣的文章
Kotlin - 属性代理
查看>>
Kotlin - 枚举与密封类
查看>>
Kotlin - 作用域函数
查看>>
Kotlin - 尾递归优化
查看>>
RePlugin集成AndroidAutoSize
查看>>
解决WebView内存泄漏【最干货】
查看>>
RePlugin集成ARouter
查看>>
Flutter - ListView与GridView
查看>>
主Activity双击退出程序
查看>>
电话&手机的正则表达式
查看>>
卸载程序
查看>>
显示popupWindow
查看>>
手势识别器
查看>>
拦截短信
查看>>
监听edittext的文字变
查看>>
获取系统的定位服务
查看>>
获取sim卡信息
查看>>
发送短信
查看>>
读取联系人
查看>>
代码设置控件的背景
查看>>