Python?matplotlib繪圖時使用鼠標滾輪放大/縮小圖像
更新時間:2022年05月09日 12:44:09 作者:井蓋上的青蛙
Matplotlib是Python程序員可用的事實上的繪圖庫,雖然它比交互式繪圖庫在圖形上更簡單,但它仍然可以一個強大的工具,下面這篇文章主要給大家介紹了關于Python?matplotlib繪圖時使用鼠標滾輪放大/縮小圖像的相關資料,需要的朋友可以參考下
思路:
- 使用fig.canvas.mpl_connect()函數來綁定相關fig的滾輪事件
- 利用事件event的inaxes屬性獲取當前鼠標所在坐標系ax
- 使用get_xlim()函數獲取坐標系ax的x/y軸坐標刻度范圍
- 使用set()函數對坐標系ax進行放大/縮小
示例:
import matplotlib.pyplot as plt import numpy as np fig = 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 + 1 plt.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+1 plt.plot(x, y1) ax3 = plt.subplot(3,1,3) y3=-x*2+1 plt.plot(x, y3) plt.show()
輸出效果:
PS:在相應坐標系內滾動鼠標滾輪即可放大/縮小x軸。
總結
到此這篇關于Python matplotlib繪圖時使用鼠標滾輪放大/縮小圖像的文章就介紹到這了,更多相關Python matplotlib放大縮小圖像內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Python如何精確控制asyncio并發(fā)運行多個任務
這篇文章主要為大家詳細介紹了Python如何精確控制asyncio并發(fā)運行多個任務,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-10-10Using Django with GAE Python 后臺抓取多個網站的頁面全文
這篇文章主要介紹了Using Django with GAE Python 后臺抓取多個網站的頁面全文,需要的朋友可以參考下2016-02-02