網站首頁 編程語言 正文
思路:
- 使用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軸。
總結
原文鏈接:https://blog.csdn.net/dcyywin8/article/details/84101319
相關推薦
- 2022-10-03 利用正則表達式校驗金額最多保留兩位小數實例代碼_正則表達式
- 2023-02-01 C語言結構體嵌套與對齊超詳細講解_C 語言
- 2022-12-14 C++?Boost?shared_ptr共享指針詳細講解_C 語言
- 2022-11-25 Python中日期和時間的用法超強總結_python
- 2022-10-05 python?slack桌面自動化開發工具_python
- 2022-06-17 C#使用讀寫鎖解決多線程并發問題_C#教程
- 2023-03-28 基于Python開發云主機類型管理腳本分享_python
- 2021-12-15 vscode?ssh安裝librosa處理音頻的解決方法_其它綜合
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支