亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python?matplotlib繪圖時使用鼠標滾輪放大/縮小圖像

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

思路:

  1. 使用fig.canvas.mpl_connect()函數(shù)來綁定相關fig的滾輪事件
  2. 利用事件event的inaxes屬性獲取當前鼠標所在坐標系ax
  3. 使用get_xlim()函數(shù)獲取坐標系ax的x/y軸坐標刻度范圍
  4. 使用set()函數(shù)對坐標系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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論