经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python » 查看文章
Python?matplotlib绘图时使用鼠标滚轮放大/缩小图像
来源:jb51  时间:2022/5/9 14:22:19  对本文有异议

思路:

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

示例:

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig = plt.figure()
  4. def call_back(event):
  5. axtemp=event.inaxes
  6. x_min, x_max = axtemp.get_xlim()
  7. fanwei = (x_max - x_min) / 10
  8. if event.button == 'up':
  9. axtemp.set(xlim=(x_min + fanwei, x_max - fanwei))
  10. print('up')
  11. elif event.button == 'down':
  12. axtemp.set(xlim=(x_min - fanwei, x_max + fanwei))
  13. print('down')
  14. fig.canvas.draw_idle() # 绘图动作实时反映在图像上
  15. fig.canvas.mpl_connect('scroll_event', call_back)
  16. fig.canvas.mpl_connect('button_press_event', call_back)
  17. ax1 = plt.subplot(3,1,1)#截取幕布的一部分
  18. ax1.xaxis.set_major_formatter(plt.NullFormatter()) # 取消x轴坐标
  19. x = np.linspace(-5, 5, 10)
  20. y = x ** 2 + 1
  21. plt.ylabel('first')
  22. plt.plot(x, y)
  23. plt.grid()
  24. ax2 = plt.subplot(3,1,2)
  25. ax2.xaxis.set_major_formatter(plt.NullFormatter()) # 取消x轴坐标
  26. y1=-x**2+1
  27. plt.plot(x, y1)
  28. ax3 = plt.subplot(3,1,3)
  29. y3=-x*2+1
  30. plt.plot(x, y3)
  31. plt.show()

输出效果:

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

总结

到此这篇关于Python matplotlib绘图时使用鼠标滚轮放大/缩小图像的文章就介绍到这了,更多相关Python matplotlib放大缩小图像内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号