欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?matplotlib繪圖時(shí)使用鼠標(biāo)滾輪放大/縮小圖像

 更新時(shí)間:2022年05月09日 12:44:09   作者:井蓋上的青蛙  
Matplotlib是Python程序員可用的事實(shí)上的繪圖庫(kù),雖然它比交互式繪圖庫(kù)在圖形上更簡(jiǎn)單,但它仍然可以一個(gè)強(qiáng)大的工具,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib繪圖時(shí)使用鼠標(biāo)滾輪放大/縮小圖像的相關(guān)資料,需要的朋友可以參考下

思路:

  1. 使用fig.canvas.mpl_connect()函數(shù)來(lái)綁定相關(guān)fig的滾輪事件
  2. 利用事件event的inaxes屬性獲取當(dāng)前鼠標(biāo)所在坐標(biāo)系ax
  3. 使用get_xlim()函數(shù)獲取坐標(biāo)系ax的x/y軸坐標(biāo)刻度范圍
  4. 使用set()函數(shù)對(duì)坐標(biāo)系ax進(jìn)行放大/縮小

示例:

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()  # 繪圖動(dòng)作實(shí)時(shí)反映在圖像上
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軸坐標(biāo)
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軸坐標(biāo)
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:在相應(yīng)坐標(biāo)系內(nèi)滾動(dòng)鼠標(biāo)滾輪即可放大/縮小x軸。

總結(jié)

到此這篇關(guān)于Python matplotlib繪圖時(shí)使用鼠標(biāo)滾輪放大/縮小圖像的文章就介紹到這了,更多相關(guān)Python matplotlib放大縮小圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論