日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:井蓋上的青蛙 ? 更新時間: 2022-07-02 編程語言

思路:

  1. 使用fig.canvas.mpl_connect()函數來綁定相關fig的滾輪事件
  2. 利用事件event的inaxes屬性獲取當前鼠標所在坐標系ax
  3. 使用get_xlim()函數獲取坐標系ax的x/y軸坐標刻度范圍
  4. 使用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軸。

總結

原文鏈接:https://blog.csdn.net/dcyywin8/article/details/84101319

欄目分類
最近更新