matplotlib自定義鼠標光標坐標格式的實現(xiàn)
matplotlib默認在圖像Windows窗口中顯示當前鼠標光標所在位置的坐標,格式為x=xx, y=xx。
鼠標光標的坐標格式由子圖模塊Axes中的format_coord函數(shù)控制。
通過重寫format_coord函數(shù)即可實現(xiàn)坐標的自定義格式。
注意:調用format_coord函數(shù)的對象是子圖對象,常見的錯誤主要在沒有正確的獲取當前子圖對象。

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)
自定義坐標格式實現(xiàn)
import matplotlib.pyplot as plt def format_coord(x, y): return 'x坐標為%1.4f, y坐標為%1.4f' % (x, y) #獲取當前子圖 ax=plt.gca() ax.format_coord = format_coord plt.show()

到此這篇關于matplotlib自定義鼠標光標坐標格式的實現(xiàn)的文章就介紹到這了,更多相關matplotlib自定義鼠標光標坐標內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python入門之三角函數(shù)sin()函數(shù)實例詳解
這篇文章主要介紹了Python入門之三角函數(shù)sin()函數(shù)實例詳解,分享了相關實例,具有一定參考價值,需要的朋友可以了解下。2017-11-11

