这是一种在单击散点图的点时自动打开 Web 浏览器并转到特定 URL 的方法。以下部分代码来自here。
代码如下:
import matplotlib.pyplot as plt
import webbrowser
#set_matplotlib_formats("svg")
class custom_objects_to_plot:
def __init__(self, x, y, name):
self.x = x
self.y = y
self.name = name
a = custom_objects_to_plot(10, 20, "a")
b = custom_objects_to_plot(30, 5, "b")
c = custom_objects_to_plot(40, 30, "c")
d = custom_objects_to_plot(120, 10, "d")
def on_pick(event):
webbrowser.open('https://stackoverflow.com')
fig, ax = plt.subplots()
for obj in [a, b, c, d]:
artist = ax.plot(obj.x, obj.y, 'ro', picker=5)[0]
artist.obj = obj
fig.canvas.callbacks.connect('pick_event', on_pick)
plt.show()
输出看起来像这样: