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

查看:112
本文介绍了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.

在此期间,有一种方法可以执行此操作,但这有点round回. URL是为PathCollection对象绘制的,因此可以从文本中创建路径,然后从该路径中创建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天全站免登陆