matplotlib
默認(rèn)在圖像Windows窗口中顯示當(dāng)前鼠標(biāo)光標(biāo)所在位置的坐標(biāo),格式為x=xx, y=xx
。
鼠標(biāo)光標(biāo)的坐標(biāo)格式由子圖模塊Axes
中的format_coord
函數(shù)控制。
通過(guò)重寫(xiě)format_coord
函數(shù)即可實(shí)現(xiàn)坐標(biāo)的自定義格式。
注意:調(diào)用format_coord
函數(shù)的對(duì)象是子圖對(duì)象,常見(jiàn)的錯(cuò)誤主要在沒(méi)有正確的獲取當(dāng)前子圖對(duì)象。
format_coord
函數(shù)源碼
- matplotlib.axes.Axes.format_coord
- def format_coord(self, x, y):
- """Return a format string formatting the *x*, *y* coordinates."""
- if x is None:
- xs = '???'
- else:
- xs = self.format_xdata(x)
- if y is None:
- ys = '???'
- else:
- ys = self.format_ydata(y)
- return 'x=%s y=%s' % (xs, ys)
自定義坐標(biāo)格式實(shí)現(xiàn)
- import matplotlib.pyplot as plt
- def format_coord(x, y):
- return 'x坐標(biāo)為%1.4f, y坐標(biāo)為%1.4f' % (x, y)
- #獲取當(dāng)前子圖
- ax=plt.gca()
- ax.format_coord = format_coord
- plt.show()
到此這篇關(guān)于matplotlib自定義鼠標(biāo)光標(biāo)坐標(biāo)格式的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)matplotlib自定義鼠標(biāo)光標(biāo)坐標(biāo)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/mighty13/article/details/112149799