python可视化鼠标 移动到折线图的数据标记的点会显示坐标数据
时间: 2024-03-10 14:47:45 浏览: 140
你可以使用matplotlib库来绘制折线图,并结合pyautogui库来实现鼠标移动到数据标记点时显示坐标数据的功能。具体的代码如下:
```python
import matplotlib.pyplot as plt
import pyautogui
# 绘制折线图
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
# 添加数据标记点
plt.scatter(x, y)
# 显示坐标数据的函数
def show_coordinates(event):
x = event.xdata
y = event.ydata
if x is not None and y is not None:
print(f"当前坐标为({x:.2f}, {y:.2f})")
pyautogui.alert(f"当前坐标为({x:.2f}, {y:.2f})")
# 绑定鼠标移动事件
plt.gcf().canvas.mpl_connect('motion_notify_event', show_coordinates)
# 显示图形
plt.show()
```
这样,当你将鼠标移动到数据标记点上时,会在控制台和弹出的对话框中显示当前坐标。其中,pyautogui库的alert()函数用于弹出一个对话框来显示坐标数据。你也可以根据需要自定义显示方式。
阅读全文
相关推荐















