網站首頁 編程語言 正文
思路:
- 使用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
相關推薦
- 2023-02-09 Flask如何接收前端ajax傳來的表單(包含文件)_python
- 2022-09-23 Go語言fmt.Sprintf格式化輸出的語法與實例_Golang
- 2022-09-18 iOS開發探索多線程GCD隊列示例詳解_IOS
- 2023-01-26 Redis中如何設置日志_Redis
- 2022-12-21 OpenHarmony實現屏幕亮度動態調節方法詳解_Android
- 2022-10-22 Python基礎Lists和tuple實例詳解_python
- 2024-04-05 @Version樂觀鎖配置mybatis-plus使用(version)
- 2022-10-03 Python實現自動整理文件的示例代碼_python
- 最近更新
-
- 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同步修改后的遠程分支