散点图中的超链接点 - matplotlib

问题描述 投票:0回答:2

我看到 matplotlib 的艺术家能够将超链接嵌入到图形对象中,如文档中所示,并在此处中简要提到。

我想尝试将超链接嵌入到散点图的点中,但对于如何在我的图中集成或访问艺术家有一些普遍的困惑。

上面链接的 Stackoverflow 示例让我相信,如果我在散点图上绘制空白文本元素,并且文本元素包含超链接,这会更容易。

除了我分享的两个资源之外,信息很少,任何额外的见解将不胜感激。

python matplotlib svg
2个回答
1
投票

这是一种在单击散点图的点时自动打开网络浏览器并转到特定 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()

输出看起来像这样:

enter image description here


0
投票

@jylls 的解决方案可以工作,但不适用于 svg。如果你想保存为 svg,正确的方法是这样的:

fig = plt.figure()
s = plt.scatter([1, 2, 3], [4, 5, 6])
s.set_urls(['https://www.bbc.com/news', 'https://www.google.com/', None])
© www.soinside.com 2019 - 2024. All rights reserved.