Python?matplotlib繪圖時使用鼠標(biāo)滾輪放大/縮小圖像
思路:
- 使用fig.canvas.mpl_connect()函數(shù)來綁定相關(guān)fig的滾輪事件
- 利用事件event的inaxes屬性獲取當(dāng)前鼠標(biāo)所在坐標(biāo)系ax
- 使用get_xlim()函數(shù)獲取坐標(biāo)系ax的x/y軸坐標(biāo)刻度范圍
- 使用set()函數(shù)對坐標(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() # 繪圖動作實時反映在圖像上
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)滾動鼠標(biāo)滾輪即可放大/縮小x軸。
總結(jié)
到此這篇關(guān)于Python matplotlib繪圖時使用鼠標(biāo)滾輪放大/縮小圖像的文章就介紹到這了,更多相關(guān)Python matplotlib放大縮小圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python如何精確控制asyncio并發(fā)運行多個任務(wù)
這篇文章主要為大家詳細(xì)介紹了Python如何精確控制asyncio并發(fā)運行多個任務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
Python對口紅進(jìn)行數(shù)據(jù)分析來選定情人節(jié)禮物
情人節(jié)送小仙女什么禮物?讓我們來用Python對口紅進(jìn)行數(shù)據(jù)分析,那個女孩子會拒絕這樣精心挑選的禮物,感興趣的小伙伴快來看看吧2022-02-02
Using Django with GAE Python 后臺抓取多個網(wǎng)站的頁面全文
這篇文章主要介紹了Using Django with GAE Python 后臺抓取多個網(wǎng)站的頁面全文,需要的朋友可以參考下2016-02-02

