matplotlib
默認在圖像Windows窗口中顯示當前鼠標光標所在位置的坐標,格式為x=xx, y=xx
。
鼠標光標的坐標格式由子圖模塊Axes
中的format_coord
函數控制。
通過重寫format_coord
函數即可實現坐標的自定義格式。
注意:調用format_coord
函數的對象是子圖對象,常見的錯誤主要在沒有正確的獲取當前子圖對象。
format_coord
函數源碼
- 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)
自定義坐標格式實現
- 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自定義鼠標光標坐標格式的實現的文章就介紹到這了,更多相關matplotlib自定義鼠標光標坐標內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/mighty13/article/details/112149799