matplotlib交互式數(shù)據(jù)光標(biāo)實(shí)現(xiàn)(mplcursors)
簡(jiǎn)介
mplcursors包也可以為matplotlib提供交互式的數(shù)據(jù)光標(biāo)(彈出式注釋框),它的靈感來(lái)源于mpldatacursor包,可以認(rèn)為是基于mpldatacursor包的二次開(kāi)發(fā)。
相對(duì)于mpldatacursor包,mplcursors包最大的特點(diǎn)就是提供了一些相對(duì)底層的API,這樣功能實(shí)現(xiàn)更加靈活。
安裝
pip install mplcursors
基本應(yīng)用
mplcursors包的基本應(yīng)用方法與mpldatacursor包類似,直接應(yīng)用cursor函數(shù)即可。
基本操作方法
- 鼠標(biāo)左鍵單擊圖表數(shù)據(jù)元素時(shí)會(huì)彈出文本框顯示最近的數(shù)據(jù)元素的坐標(biāo)值。
- 鼠標(biāo)右鍵單擊文本框取消顯示數(shù)據(jù)光標(biāo)。
- 按d鍵時(shí)切換顯示\關(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()
mplcursors自定義應(yīng)用
mpldatacursor包中自定義功能主要通過(guò)向datacursor函數(shù)傳遞實(shí)參實(shí)現(xiàn)。
mplcursors包中的cursor函數(shù)對(duì)標(biāo)mpldatacursor包中的datacursor函數(shù),但是在參數(shù)上發(fā)生了變化,保留了artists、hover、bindings、multiple、highlight等類似參數(shù)。
mplcursors包增加Selection對(duì)象(底層為namedtuple)表示選擇的數(shù)據(jù)元素的屬性。
當(dāng)選中某個(gè)數(shù)據(jù)點(diǎn)時(shí),可以通過(guò)添加(add)或刪除(remove)事件觸發(fā)、注冊(cè)回調(diào)函數(shù)實(shí)現(xiàn)功能,回調(diào)函數(shù)只有一個(gè)參數(shù),及選擇的數(shù)據(jù)點(diǎn)。
在注冊(cè)回調(diào)函數(shù)時(shí),mplcursors包支持使用裝飾器。
mpldatacursor與mplcursors API對(duì)比
下面以修改顯示文本信息為例對(duì)比下mpldatacursor與mplcursors的不同實(shí)現(xiàn)方式。

mpldatacursor實(shí)現(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實(shí)現(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實(shí)現(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()
結(jié)論
mplcursors包實(shí)現(xiàn)的功能與mpldatacursor包非常相似。相對(duì)而言mplcursors包的API更加靈活,通過(guò)connect函數(shù)或者裝飾器自定義屬性耦合性更弱,便于實(shí)現(xiàn)繪圖與數(shù)據(jù)光標(biāo)實(shí)現(xiàn)的分離。
參考
https://mplcursors.readthedocs.io/en/stable/
https://github.com/anntzer/mplcursors
到此這篇關(guān)于matplotlib交互式數(shù)據(jù)光標(biāo)實(shí)現(xiàn)(mplcursors)的文章就介紹到這了,更多相關(guān)matplotlib交互式光標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 支持向量機(jī)分類器的實(shí)現(xiàn)
這篇文章主要介紹了Python 支持向量機(jī)分類器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Python實(shí)現(xiàn)PPT/PPTX批量轉(zhuǎn)換成PDF
這篇文章主要為大家詳細(xì)介紹了如何使用Python將PowerPoint演示文稿(PPT、PPTX等)轉(zhuǎn)換為PDF文件,使演示內(nèi)容能夠在更多的設(shè)備上展示,感興趣的小伙伴可以了解下2024-01-01
Python之Matlibplot畫(huà)圖功能演示過(guò)程
這篇文章主要介紹了Python之Matlibplot畫(huà)圖功能演示過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python Django form 組件動(dòng)態(tài)從數(shù)據(jù)庫(kù)取choices數(shù)據(jù)實(shí)例
這篇文章主要介紹了Python Django form 組件動(dòng)態(tài)從數(shù)據(jù)庫(kù)取choices數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
python實(shí)現(xiàn)Pyecharts實(shí)現(xiàn)動(dòng)態(tài)地圖(Map、Geo)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Pyecharts實(shí)現(xiàn)動(dòng)態(tài)地圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
簡(jiǎn)單總結(jié)Python中序列與字典的相同和不同之處
這篇文章主要介紹了Python中序列與字典的相同和不同之處,序列這里講到Python中最常用的列表和元組以及字典三種,需要的朋友可以參考下2016-01-01
python selenium 執(zhí)行完畢關(guān)閉chromedriver進(jìn)程示例
今天小編就為大家分享一篇python selenium 執(zhí)行完畢關(guān)閉chromedriver進(jìn)程示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
python opencv 實(shí)現(xiàn)對(duì)圖像邊緣擴(kuò)充
今天小編就為大家分享一篇python opencv 實(shí)現(xiàn)對(duì)圖像邊緣擴(kuò)充,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

