简体   繁体   English

散点图中的超链接点 - matplotlib

[英]Hyperlink points in a scatter plot - matplotlib

I see that matplotlib's artist has the ability to embed hyperlinks into graph objects as seen in the documentation and briefly mentioned here .我看到 matplotlib 的艺术家有能力将超链接嵌入到图形对象中,如文档中所见并在此处简要提及。

I would like to attempt to embed hyperlinks into the points of a scatter plot but have some general confusion about how to integrate or access the artist in my plot.我想尝试将超链接嵌入到散点图的点中,但对如何在我的图中集成或访问艺术家有一些普遍的困惑。

The Stackoverflow examples linked above lead me to believe that this is easier if I plot blank text elements over the scatter plot, and the text elements contain the hyperlinks.上面链接的 Stackoverflow 示例让我相信,如果我在散点图上绘制空白文本元素,并且文本元素包含超链接,这会更容易。

There is very little information beyond the two resources I shared, any additional insight would be greatly appreciated.除了我共享的两个资源之外,几乎没有信息,任何额外的见解将不胜感激。

Here is a way to automatically open a web browser and go to a specific URL when clicking on the points of a scatter plot.这是一种在单击散点图时自动打开 Web 浏览器并转到特定 URL 的方法。 Parts of the code below come from here .下面的部分代码来自这里

Here is the code:这是代码:

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()

And the output looks like that:输出如下所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM