Python matlplotlib 为文本添加超链接 [英] Python matlplotlib add hyperlink to text

查看:29
本文介绍了Python matlplotlib 为文本添加超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 matplotlib 在 Python 中创建了一个绘图.在注释每一行之后,我想让标签成为一个超链接(或者,让该行本身成为一个超链接).文本项有一个名为url"的属性,但我已经尝试过,但我不知道它有什么作用,如果有的话.

I've created a plot in Python using matplotlib. After annotating each line, I'd like to make the label a hyperlink (or alternatively, make the line itself a hyperlink). The text item has a property called 'url', but I've tried it and I can't figure out what, if anything, it does.

是否可以将文本或线条对象制作成超链接?

Is it possible to make text or line objects into hyperlinks?

推荐答案

这个例子展示了如何在输出 SVG 时设置超链接.请注意,这仅对 SVG 有意义.如果情节只是一个图像,它只是一个图像,图像中不能有超链接.

This example shows how to set hyperlinks if you're outputting an SVG. Note that this only makes sense for SVG. If the plot is just an image, it's just an image, and images can't have hyperlinks in them.

如果您希望能够在交互式绘图窗口中单击对象并使其充当超链接,您可以创建一个事件处理程序来处理pick"事件,然后打开浏览器或其他任何东西.请参阅此示例了解如何选择事件.Matplotlib 绘图不是网页,甚至不是真正的文档,它们只是显示图形的窗口,因此它们不支持超链接;使用选择事件,您可以通过在单击对象时打开 Web 浏览器来模拟超链接.

If you want to be able to click on the object in the interactive plotting window and have that act like a hyperlink, you could create an event handler to handle the "pick" event, and have that open a browser or whatever. See this example for how to do pick events. Matplotlib plots aren't web pages or even really documents, they're just windows with graphics displayed in them, so they don't support hyperlinks as such; using a pick event you can emulate a hyperlink by opening a web browser when an object is clicked.

你是对的,它不起作用.似乎 URL 属性只能读取并用于某些类型的对象.谷歌搜索,我看到一些关于它的旧 matplotlib 邮件列表讨论,其中的想法似乎是逐渐为不同的艺术家类型添加 URL 支持,但我猜他们从来没有考虑过.我建议您在 matplotlib 错误跟踪器上提出一个关于此的错误.

You are right, it doesn't work. It seems that the URL property is only read and used for certain types of objects. Googling, I see some old matplotlib mailing list discussion of it, where it seems the idea was to gradually add URL support to different artist types, but I guess they never got around to it. I would suggest you raise a bug about this on the matplotlib bug tracker.

与此同时,有一种方法可以做到这一点,但它有点迂回.URL 是为 PathCollection 对象绘制的,因此您可以从文本中创建一个 Path,然后从该路径中创建一个 PathCollection,然后将该 PathCollection 添加到您的绘图中.举个例子:

In the meantime, there is a way to do it, but it is somewhat roundabout. The URL is drawn for PathCollection objects, so you could make a Path out of your text, then make a PathCollection out of that path, and then add that PathCollection to your plot. Here's an example:

pyplot.scatter([1, 2, 3], [4, 5, 6])
t = mpl.text.TextPath((2, 4), 'This is text', size=0.1)
pc = mpl.collections.PathCollection([t])
pc.set_urls(['http://www.google.com'])
ax = pyplot.gca()
ax.add_collection(pc)
pyplot.draw()
f = pyplot.gcf()
f.canvas.print_figure('fig.svg')

请注意,您必须使用 set_urls 而不是 set_url.此方法生成带有可点击文本的 SVG,但它有一些缺点.最值得注意的是,您似乎必须在数据坐标中手动设置文本大小,因此可能需要费些功夫才能找到相对于绘制数据的大小而言不是太大或太小的文本大小.

Note that you must use set_urls and not set_url. This method produces an SVG with clickable text, but it has some drawbacks. Most notably, it seems you have to set the text size manually in data coordinates, so it may take some fiddling to find a text size that isn't too ridiculously huge or tiny relative to the magnitude of your plotted data.

这篇关于Python matlplotlib 为文本添加超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆