這篇文章給大家介紹怎么在matplotlib中實現(xiàn)一個交互式數(shù)據(jù)光標(biāo)效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
為吉首等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及吉首網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、吉首網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!mplcursors
包也可以為matplotlib
提供交互式的數(shù)據(jù)光標(biāo)(彈出式注釋框),它的靈感來源于mpldatacursor
包,可以認(rèn)為是基于mpldatacursor
包的二次開發(fā)。
相對于mpldatacursor
包,mplcursors
包較大的特點就是提供了一些相對底層的API,這樣功能實現(xiàn)更加靈活。
pip install mplcursors
mplcursors
包的基本應(yīng)用方法與mpldatacursor
包類似,直接應(yīng)用cursor
函數(shù)即可。
鼠標(biāo)左鍵單擊圖表數(shù)據(jù)元素時會彈出文本框顯示最近的數(shù)據(jù)元素的坐標(biāo)值。
鼠標(biāo)右鍵單擊文本框取消顯示數(shù)據(jù)光標(biāo)。
按d鍵時切換顯示\關(guān)閉數(shù)據(jù)光標(biāo)。
import matplotlib.pyplot as plt import numpy as np import mplcursors data = np.outer(range(10), range(1, 5)) fig, ax = plt.subplots() lines = ax.plot(data) ax.set_title("Click somewhere on a line.\nRight-click to deselect.\n" "Annotations can be dragged.") mplcursors.cursor(lines) # or just mplcursors.cursor() plt.show()
mpldatacursor
包中自定義功能主要通過向datacursor
函數(shù)傳遞實參實現(xiàn)。mplcursors
包中的cursor
函數(shù)對標(biāo)mpldatacursor
包中的datacursor
函數(shù),但是在參數(shù)上發(fā)生了變化,保留了artists
、hover
、bindings
、multiple
、highlight
等類似參數(shù)。mplcursors
包增加Selection
對象(底層為namedtuple
)表示選擇的數(shù)據(jù)元素的屬性。
當(dāng)選中某個數(shù)據(jù)點時,可以通過添加(add
)或刪除(remove
)事件觸發(fā)、注冊回調(diào)函數(shù)實現(xiàn)功能,回調(diào)函數(shù)只有一個參數(shù),及選擇的數(shù)據(jù)點。
在注冊回調(diào)函數(shù)時,mplcursors
包支持使用裝飾器。
下面以修改顯示文本信息為例對比下mpldatacursor
與mplcursors
的不同實現(xiàn)方式。
import matplotlib.pyplot as plt import numpy as np from mpldatacursor import datacursor ax=plt.gca() labels = ["a", "b", "c"] for i in range(3): ax.plot(i, i,'o', label=labels[i]) datacursor(formatter='{label}'.format) plt.show()
mplcursors
實現(xiàn)方式一
import matplotlib.pyplot as plt import numpy as np import mplcursors ax=plt.gca() lines = ax.plot(range(3), range(3), "o") labels = ["a", "b", "c"] cursor = mplcursors.cursor(lines) cursor.connect( "add", lambda sel: sel.annotation.set_text(labels[sel.target.index])) plt.show()
mplcursors
實現(xiàn)方式二
import matplotlib.pyplot as plt import numpy as np import mplcursors ax=plt.gca() lines = ax.plot(range(3), range(3), "o") labels = ["a", "b", "c"] cursor = mplcursors.cursor(lines) @cursor.connect("add") def on_add(sel): sel.annotation.set_text(labels[sel.target.index]) plt.show()
mplcursors
包實現(xiàn)的功能與mpldatacursor
包非常相似。相對而言mplcursors
包的API更加靈活,通過connect
函數(shù)或者裝飾器自定義屬性耦合性更弱,便于實現(xiàn)繪圖與數(shù)據(jù)光標(biāo)實現(xiàn)的分離。
關(guān)于怎么在matplotlib中實現(xiàn)一個交互式數(shù)據(jù)光標(biāo)效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:怎么在matplotlib中實現(xiàn)一個交互式數(shù)據(jù)光標(biāo)效果-創(chuàng)新互聯(lián)
文章路徑:http://aaarwkj.com/article10/cogedo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、小程序開發(fā)、ChatGPT、網(wǎng)站營銷、服務(wù)器托管、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容